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