Learning Goals
- Get to know a few common terms of testing
- Work with the Julia unit testing package
Test.jl
Material is taken and modified from the SSE lecture, which builds partly on the py-rse book, and from the Test.jl docs.
1. General Introduction to Testing
What is Testing?
- Smelling old milk before using it
- A way to determine if a software is not producing reliable results and if so, what is the reason
- Manual testing vs. automated testing
Why Should you Test your Software?
- Improve software reliability and reproducibility
- Make sure that changes (bugfixes, new features) do not affect other parts of software
- Generally all software is better off being tested regularly. Possible exceptions are very small codes with single users.
- Ensure that a released version of a software actually works.
Some Ways to Test Software
- Assertions
- Unit testing
- Integration testing
- Regression testing
Assertions
@assert condition "message"
- Principle of defensive programming
- Nothing happens when an assertion is true; throws error when false.
- Types of assertion statements:
- Precondition
- Postcondition
- Invariant
- A basic but powerful tool to test a software on-the-go
Unit Testing
- Catching errors with assertions is good but preventing them is better.
- A unit is a single function in one situation.
- A situation is one amongst many possible variations of input parameters.
- User creates the expected result manually.
- Actual result is compared to the expected result by
@test
.
Integration Testing
- Test whether several units work in conjunction.
- Integrate units and test them together in an integration test.
- Often more complicated than a unit test and gives higher test coverage.
Regression Testing
- Generating an expected result is not possible in some situations.
- Compare the current actual result with a previous actual result.
- No guarantee that the current actual result is correct.
- Risk of a bug being carried over indefinitely.
- Main purpose is to identify changes in the current state of the code with respect to a past state.
2. Unit Testing in Julia with Test.jl
Setup of Test.jl
Standardized folder structure:
βββ Manifest.toml βββ Project.toml βββ src/ βββ test βββ Manifest.toml βββ Project.toml βββ runtests.jl βββ setup.jl
Singular
test
vs pluralruntests.jl
setup.jl
for allusing XYZ
statements, included inruntests.jl
Additional packages in
[extra] section
of./Project.toml
or in new./test/Project.toml
- In case of the latter: Do not add the package itself to the
./test/Project.toml
- In case of the latter: Do not add the package itself to the
Run:
]test
when root project is activated
Implement and Structure Tests
@test expr
: Test whether expressionexpr
is true@test expr broken=true
: Explicitly mark test as broken@test_throws exception expr
: Test whether expressionexpr
throwsexception
(test unhappy path)> @test_throws DimensionMismatch [1, 2, 3] + [1, 2] juliaTest Passed : DimensionMismatch Thrown
@testset
: Structure tests@testset "trigonometric identities" begin = 2/3*Ο ΞΈ @test sin(-ΞΈ) β -sin(ΞΈ) @test cos(-ΞΈ) β cos(ΞΈ) end;
@testset for ... end
: Test in loop
Further Reading and Watching
- Research Software Engineering with Python - Chapter 11: Testing Software
- HiRSE-Summer of Testing Part 2b: βTesting with Juliaβ by Nils Niggemann
- Official documentation of Test.jl
3. Test.jl Demo
We use MyTestPackage
, which looks as follows:
βββ Manifest.toml
βββ Project.toml
βββ src
β βββ find.jl
β βββ MyTestPackage.jl
βββ test
βββ find.jl
βββ Manifest.toml
βββ Project.toml
βββ runtests.jl
βββ setup.jl
Look at
MyTestPackage.jl
andfind.jl
: We have two functionsfind_max
andfind_mean
, which calculate the maximum and mean of all elements of a::AbstractVector
.- Assertions were added to check for
NaN
values
- Assertions were added to check for
Look at
runtests.jl
:- Why do we need
using MyTestPackage
? - We include dependencies via
setup.jl
:Test
andStableRNG
. - Testset βfindβ
- Why do we need
Look at
find.jl
- Unit tests for
find_max
andfind_mean
test_throws
to test unhappy path- Test with absolute tolerance
- Integration test, which tests combination of both methods
- Unit tests for
Run tests:
]activate . ]test
4. Exercise
Write tests for your own statistics package π