# SetUp and TearDown in Test module?

**URL:** <https://discourse.julialang.org/t/setup-and-teardown-in-test-module/68589>\
**Category:** General Usage\
**Tags:** question, testing, testjl-unittesting\
**Created:** [September 22, 2021, 3:29pm UTC](https://discourse.julialang.org/t/setup-and-teardown-in-test-module/68589 "2021-09-22T15:29:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fspreck-indiscale](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fspreck-indiscale/32/29408_2.png) [@fspreck-indiscale](https://discourse.julialang.org/u/fspreck-indiscale)\
**Post date:** [September 22, 2021, 3:29pm UTC](https://discourse.julialang.org/t/setup-and-teardown-in-test-module/68589/1 "2021-09-22T15:29:20Z")

</div>

Hello everyone,

I’m still quite new to Julia, so please excuse me if I have missed something obvious here. Is there a way in Test.jl to declare functions that are called before/after every `@testset` similar to SetUp/TearDown methods in other languages like C++ or Python? If there isn’t, are there best practices on implementing something similar for a test suit?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 22, 2021, 3:52pm UTC](https://discourse.julialang.org/t/setup-and-teardown-in-test-module/68589/2 "2021-09-22T15:52:08Z")

</div>

The easiest way is to just put the `@testests` in functions, and then call them from a loop. For a very simple example

```julia
function test1(x)
     @test x[1]=1
end

function test2(x)
     @test x[2]==2
     x .= 0 #thing that requires cleanup
end

for f in [test1,test2]
    x = [1,2] # setup
    f(x)
    #add teardown here if needed
end

```

The reason Julia doesn’t need Setup and Teardown is that unlike other languages, it is very idiomatic to treat functions as objects and do higher order programming. (most testing interfaces traditionally were inspired by Java which makes higher order functions a total pain).

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [September 22, 2021, 6:33pm UTC](https://discourse.julialang.org/t/setup-and-teardown-in-test-module/68589/3 "2021-09-22T18:33:44Z")

</div>

You can use a `do` block.

```julia
function test()
    setupteardown() do 
    end
end

```

---

<div class="post-metadata">

**Author:** ![adolgert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adolgert/32/20286_2.png) [@adolgert](https://discourse.julialang.org/u/adolgert)\
**Post date:** [September 23, 2021, 1:40am UTC](https://discourse.julialang.org/t/setup-and-teardown-in-test-module/68589/4 "2021-09-23T01:40:41Z")

</div>

@jzr offers a good suggestion, so let’s expand to make it more explicit. The [do-notation](https://docs.julialang.org/en/v1/manual/functions/#Do-Block-Syntax-for-Function-Arguments) does make setup and teardown pretty. If you want to isolate tests, the [SafeTestsets.jl](https://github.com/YingboMa/SafeTestsets.jl) package defines a different macro for the unit test, to create an automatic module for it. This can make it hard to load a package macro in the test, but it usually works fine. It does mean you have to define helper functions inside a module so that they can be loaded. It’s not difficult in practice and looks like this.

```julia
module TestHelpers
  function setupteardown(f)
    resources = ...setup...
    try
      f(resources)
    finally
      ...teardown...
    end
  end
end
@safetestset "this invariant is true" begin
  using ..TestHelpers
  setupteardown() do resources
    @test any(resources)
  end
end

```

You can put your setup and teardown helpers into another file, as long as you include it before the tests in the runtests.jl file.

---

<div class="post-metadata">

**Author:** ![fspreck-indiscale](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fspreck-indiscale/32/29408_2.png) [@fspreck-indiscale](https://discourse.julialang.org/u/fspreck-indiscale)\
**Post date:** [September 23, 2021, 7:34am UTC](https://discourse.julialang.org/t/setup-and-teardown-in-test-module/68589/5 "2021-09-23T07:34:26Z")

</div>

Wow, thanks for the quick answers and clarifications! I’ll try and implement it in do notation then.
