OverviewTeaching: 10 min
Exercises: 30 minQuestions
What are unit tests?
How are tests organized in Julia?Objectives
Learn to create unit tests and test-sets using the
Now that Melissa has released her first package she fears that future changes will impact the existing functionality of her package. This can be prevented by adding tests to her package.
Looking at the structure of other packages Melissa figured out that tests
usually go in a separate
test folder next to the
This should contain a
The standard library
Test provides the functionality for writing tests:
namely, the macros
@test can be used to test a single equality, such as
using Test @test 1 + 1 == 2
Several tests can be grouped in a test-set with a descriptive name
using Test @testset "Test arithmetic equalities" begin @test 1 + 1 == 2 end
With this Melissa can run her test using the pkg mode of the REPL:
(MelissasModule) pkg> test
Test specific dependencies
Melissa needed to add
Test to her package in order to run the code above, but
Test is not needed for her package other than testing.
Thus it is possible to move the
Test entry in the
Project.toml file from
[deps] to an
[extras] section and then add another entry:
[targets] test = ["Test"]
Check out the sample project file for a complete example.
Create a test for MelissasModule
Create a test that ensures that
shoot_distancereturns a value that is between
target - εand
target + ε.
using MelissasModule using Test @testset "Test hitting target" begin imprecise_trebuchet = Trebuchet(500.0, 0.25pi) environment = Environment(5, 100) precise_trebuchet = aim(imprecise_trebuchet, environment) @test 100 - 0.1 <= shoot_distance(precise_trebuchet, environment) <= 100 + 0.1 # default ε is 0.1 end
Tests are important