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: Example.jl/test at master · JuliaLang/Example.jl · GitHub

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

1 Like

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

1 Like

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!

2 Likes

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

1 Like