Introduction
Good programming practices put an enormous emphasis on testing. This page contains a basic example of the mechanics of setting up a Julia project with tests.
For convenience, Julia ships with the Test
package, which automates much of the work required to define and run tests.
Example Project
For conciseness, let’s start a dummy project. Our example code will perform a basic computation:
where
by resorting to the formula
So, after creating a new project as detailed in the previous section, let’s modify the MyProject.jl
file:
# src/MyProject.jl
module MyProject
export ArithmeticSum
ArithmeticSum(a₁,Δ,n) = return (n+1)*(a₁ + (a₁+n*Δ))/2
end
Writing Tests
Now let’s write some tests. A straightfoward way to test this simple routine is to sum the corresponding numbers one by one, for specific values of and . We’ll implement this straightforward routine in a SlowSum
function.
SlowSum(a₁,Δ,n) = sum([a₁+Δ*i for i ∈ 0:n])
Without further ado, let’s write our basic tests. Will resort to our SlowSum
function, and the @test
macro to evaluate that ArithmeticSum
coincides with SlowSum
in several test cases.
# test/runtests.jl
using MyProject
using Test
SlowSum(a₁,Δ,n) = sum([a₁+Δ*i for i ∈ 0:n])
@testset "ArithmeticSum" begin
@test ArithmeticSum(1,1,14) == SlowSum(1,1,14)
@test ArithmeticSum(5,1,10) == SlowSum(5,1,10)
@test ArithmeticSum(2,3,14) == SlowSum(2,3,14)
end
Running the Tests
Importantly, running the tests is performed from the pkg
environment. To access the @test
macro, we also need to add the Test
package as a dependency of our project.
pkg > activate .
pkg > add Test
pkg > test MyProject
After which you should get a message saying that all 3 tests have passed.
Other Types of Tests
There are many types of tests we might want to conduct.
For example, as floating-point arithmetic introduces some errors, we might want to test for reasonably approximate computations. This can be done with the isaprox
function, or by using the unicode character ≈
(\approx
).
@test π ≈ 3.14 atol=0.01
# Test Passed
If you want to learn more about testing, you can check the official documentation here.