To demonstrate testing functions in Julia, two functions are defined below. A small program that uses the functions is also shown.
# adder.jl
function adder(x, y)
a = x + y
return a
end
# divider.jl
function divider(s, t)
d = s / t
return d
end
# main.jl
include("adder.jl")
include("divider.jl")
a = adder(2, 5)
println("a is $a")
d = divider(10, 3.5)
println("d is $d")
Testing the functions can be done with the Test package.
# tests.jl
using Test
include("adder.jl")
include("divider.jl")
@testset "All the tests" begin
@test adder(2, 5) == 7
@test divider(10, 3) ≈ 3.333333333
end
Running the tests from the command line produces the following results:
$ julia tests.jl
Test Summary: | Pass Total
All the tests | 2 2
Gavin Wiggins © 2024.
Made on a Mac with Genja. Hosted on GitHub Pages.