Julia v1.5 Testing: Best Practices

Do you think running tests are too slow? Cannot figure out how to do Test Driven Development (TDD) in Julia? Here is a guide.

Infrequent and Correct Testing

$ cd Foobar
$ julia
julia> ]
(@v1.5) pkg>
(@v1.5) pkg> activate .
(Foobar) pkg>
(Foobar) pkg> test
Testing Foobar
Status `/private/var/folders/qb/zc__4nlj4_z4w24t_trvx1cm0000gn/T/jl_K7IPCe/Project.toml`
[9a3f8284] Random
[9e88b42a] Serialization
[6462fe0b] Sockets
[8dfed614] Test
Test Summary: | Pass Total
All Tests | 57 57
Testing Foobar tests passed
(Foobar) pkg> test

Regular Testing

shell> julia test/runtests.jl
Test Summary: | Pass Total
All Tests | 57 57
@testset "All Foobar tests" begin

include("foo-tests.jl")
include("bar-tests.jl")
include("baz-tests.jl")

end
tests = ["foo", "bar", "baz"]
if !isempty(ARGS)
tests = ARGS # Set list to same as command line args
end

@testset "All Foobar tests" begin

for t in tests
include("$t-tests.jl")
end

end
$ julia test/runtests.jl foo baz

Testing Environment

$ julia --project=test test/runtests.jl

Rapid Iteration Testing

julia> include("test/runtests.jl")
Test Summary: | Pass Total
All Tests | 57 57

Single Tests

include("setup.jl")

@testset "All Foobar tests" begin

include("foo-tests.jl")
include("bar-tests.jl")
include("baz-tests.jl")

end
@testset "Without operands" begin
@test disassemble(901) == "INP"
@test disassemble(902) == "OUT"
@test disassemble(000) == "HLT"
end

@testset "With operands" begin
@test disassemble(105) == "ADD 5"
@test disassemble(112) == "ADD 12"
@test disassemble(243) == "SUB 43"
@test disassemble(399) == "STA 99"
@test disassemble(510) == "LDA 10"
end
julia> include("test/setup.jl")

Geek dad, living in Oslo, Norway with passion for UX, Julia programming, science, teaching, reading and writing.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store