Adding tests
Last updated on 2024-11-15 | Edit this page
Overview
Questions
- “What are unit tests?”
- “How are tests organized in Julia?”
Objectives
- “Learn to create unit tests and test-sets using the
Teststandard library”
Unit tests
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
src folder. This should contain a runtests.jl
file.
The standard library Test provides the functionality for
writing tests: namely, the macros @test and
@testset.
@test can be used to test a single equality, such as
OUTPUT
Test Passed
Several tests can be grouped in a test-set with a descriptive name
OUTPUT
Test.DefaultTestSet("Test arithmetic equalities", Any[], 1, false, false, true, 1.731669987513481e9, 1.731669987543832e9, false)
With this Melissa can run her test using the pkg mode of the REPL:
Test specific dependencies
Melissa needed to add Test to her package in order to
run the code above, but actually 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:
Check out the sample project file for a complete example.
Challenge
Create a test for MelissasModule Create a test that ensures that
shoot_distance returns a value that is between
target - ε and target + ε.
JULIA
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
Keypoints
- “Tests are important”