SafeTestsets - how to share test fixture

Using SafeTestsets is a best practice for testing. 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

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

1 Like

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

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").