Instruct Julia to skip unit testing?

Hi everyone,

I am new to Julia and few days ago had a moment of delight when I learned that Test package offers a very neat way to include unit testing in your code :slight_smile: Since then, I was putting @testset and @test wherever I though useful in my code for computational fluid dynamic simulations (which I am translating from my old Fortran code).

Once all the tests were in place, and were passing, I decided to run a simulation. To my disappointment, the parts of the code enclosed in @testset begin ... end constructs are still being executed when I run productive simulations. I don’t want that, when I test everything I want to simulate and used CPU to crunch numbers, not to test what was tested already.

I wonder if there is a global way to instruct Julia to skip unit testings when performing productive runs?

Cheers

Welcome!

In Julia, tests are placed in a specific file in a specific place and are run only when explicitly testing a package.

Take a look at the Example package to see what that file and location look like: https://github.com/JuliaLang/Example.jl/tree/master/test

and of course, there’s tons of info in the docs: Unit Testing · The Julia Language

2 Likes

If you want to test some conditions at runtime, you can use the @assert macro.

Unit testing is best done in a file located at yourproject/test/runtests.jl. That way you can use Pkg.test() to run the test script in runtests.jl (often this will be a file with include statements of other test files in the test directory).

2 Likes

Thanks.

Thanks Henri. I did put all the tests in the file runtests.jl. It is just so that this file calls all the other functions I wrote for productive runs, which do have embedded @testset constructs.

Hi Niceno! Typically all of the @test and @testset macros are found within runtests.jl (and any included files), and the functions within the package do not contain the macros. This means the tests only run when doing Pkg.test and not when the individual functions are run.

You can, however, use ReTest, which uses InlineTest, to define tests in the same files as the code (rather than runtests.jl), and they are not run when normal functions execute.

See this example (similar to the one from the ReTest docs)

module MyPackage

using InlineTest

function greet()
    @testset "1" begin
        @info "Testing ..."
        @test true
    end
    "Hello World"
end

end
ulia> using ReTest, MyPackage

julia> MyPackage.greet()
"Hello World"

julia> MyPackage.runtests()
[ Info: Testing ...
      Pass  
1 |      1  

Disclaimer: I haven’t used ReTest that much, so check out the Caveats section of the docs and have a play around!

3 Likes

Wow man - this look marvelous, exactly what I needed :slightly_smiling_face:

1 Like

If you come to this issue like me some years later, it is good to know that there is also
TestItemRunner.jl from the Julia-VSCode team (which also works without VSCode :wink: )

It is very similar to ReTest

1 Like