# SafeTestsets - how to share test fixture

**URL:** <https://discourse.julialang.org/t/safetestsets-how-to-share-test-fixture/55266>\
**Category:** General Usage\
**Tags:** testing\
**Created:** [February 14, 2021, 5:50pm UTC](https://discourse.julialang.org/t/safetestsets-how-to-share-test-fixture/55266 "2021-02-14T17:50:57Z")\
**Posts on this page:** 3\
**Page:** 1

<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:** [February 14, 2021, 5:50pm UTC](https://discourse.julialang.org/t/safetestsets-how-to-share-test-fixture/55266/1 "2021-02-14T17:50:57Z")

</div>

Using `SafeTestsets` is a [best practice for testing](https://discourse.julialang.org/t/best-practices-for-julia-unit-testing/30858/4). This kind of `TestSet` keeps tests from interfering with each other, so I couldn’t define a function `setup_test()` outside of `@safetestset` and use it inside the `TestSet`. I figured out a way to do it and wonder whether others are using different methods?

If I make a named module in the testing code, like `module TestSetup`, then I can refer to it within a `SafeTestSet` with `using ..TestSetup`. This works because each `SafeTestset` implicitly places its code within a module to guard it from other `TestSets`.

The other rule of the game is that `TestSets` can be nested, as long as `SafeTestset` is at the bottom of the hierarchy. At this point, I’m treating `SafeTestset` the way I’d treat each unit test in Python, R, or C++ and treating `@test` as a testing assertion.

Hope All Is Well - Drew

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 14, 2021, 10:18pm UTC](https://discourse.julialang.org/t/safetestsets-how-to-share-test-fixture/55266/2 "2021-02-14T22:18:02Z")

</div>

A SafeTestset defines a module, so you can also reference the variables from the returned module.

---

<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:** [February 14, 2021, 11:07pm UTC](https://discourse.julialang.org/t/safetestsets-how-to-share-test-fixture/55266/3 "2021-02-14T23:07:41Z")

</div>

That makes sense. So here’s what it looks like spelled out:

```julia
module TestHelp testhelp() = 9 end
helper() = 7

@safetestset another = "another one" begin
    using ..TestHelp: testhelp
    using ..Main: helper
    @test testhelp() == 9
    @test helper() == 7
end

```

The `SafeTestset`-generate module comes from `gensym()`, so it’s mangled. There’s no module called “another.” It will be `Symbol("##another#234")`.
