Julia Testing Tutorial
last modified November 18, 2026
A unit test is a small piece of code that checks one behaviour of a
program. Julia's Test standard library provides macros for writing
such checks and for grouping them into named sets. Running tests regularly
catches regressions — changes that break code which used to work.
This tutorial covers @test, @testset,
@test_throws, approximate comparisons and the standard layout for
testing a package with Pkg.test. All outputs below were verified
with Julia 1.13.4; the @testset summaries are deterministic for
these short examples.
The @test Macro
The @test macro evaluates an expression that should be
true. A passing test is silent, so a println at the
end makes it clear that the script ran to completion.
using Test
@test 1 + 1 == 2
@test "julia" == "julia"
@test 3 < 5
println("done")
Each line checks one expression. When a test fails, Test records
the expression and the values involved instead of stopping the whole script
immediately. The final message confirms that all three checks passed. Import
the Test library with using Test before using its
macros; it is part of every Julia installation.
$ julia main.jl done
Grouping Tests with @testset
A test set groups related checks and prints a summary when the group finishes. The first argument is a descriptive string that appears in the summary.
using Test
@testset "Arithmetic" begin
@test 1 + 2 == 3
@test 2 * 3 == 6
@test 10 - 4 == 6
end
The summary reports the number of passing tests, the total number of tests and the elapsed time. These three checks pass, so the table shows three passes out of three. Giving each group a meaningful name makes failures easy to locate when a project grows to hundreds of tests.
$ julia main.jl Test Summary: | Pass Total Time Arithmetic | 3 3 0.0s
Testing Your Own Functions
Tests become useful when they check code you wrote. Define a function, then assert its results for several inputs, including boundary cases.
using Test
double(x) = 2x
@testset "double" begin
@test double(2) == 4
@test double(0) == 0
@test double(-3) == -6
end
The shorthand 2x means 2 * x, a Julia feature called
numeric literal coefficients. Testing zero and a negative number covers more than
a single happy-path example. If someone later changes double, the
test set points to exactly which case stopped working.
$ julia main.jl Test Summary: | Pass Total Time double | 3 3 0.0s
Testing for Errors with @test_throws
Some functions are supposed to fail on invalid input. The
@test_throws macro checks that an expression raises a particular
kind of exception — an object that signals an error.
using Test
@testset "Errors" begin
@test_throws BoundsError [1, 2, 3][5]
@test_throws DivideError 1 ÷ 0
end
Indexing past the end of an array raises BoundsError, and integer
division by zero raises DivideError. Checking both the fact that an
error occurred and its type documents the contract of your code. If the
expression unexpectedly succeeds, or raises a different exception, the test
fails.
$ julia main.jl Test Summary: | Pass Total Time Errors | 2 2 0.0s
Approximate Comparisons
Floating-point arithmetic rounds results, so two values that are mathematically
equal may differ in their last bits. The ≈ operator, typed as
\approx in the REPL, tests approximate equality.
using Test
@testset "Approximate" begin
@test 0.1 + 0.2 ≈ 0.3
@test sqrt(2)^2 ≈ 2.0
end
In binary floating point, 0.1 + 0.2 is not exactly 0.3,
so an ordinary equality test would fail. The ≈ operator allows a
small relative difference. You can control the tolerance with
isapprox(a, b; atol=..., rtol=...) when a test needs stricter or
looser bounds.
$ julia main.jl Test Summary: | Pass Total Time Approximate | 2 2 0.0s
Nested Test Sets
Test sets can be nested to mirror the structure of a program. Julia prints a summary for the outer set that aggregates the counts of its children.
using Test
@testset "Math" begin
@testset "Addition" begin
@test 1 + 1 == 2
@test 2 + 2 == 4
end
@testset "Multiplication" begin
@test 2 * 2 == 4
end
end
The two inner groups contribute two and one tests respectively, and the outer
summary reports three passes in total. Nesting keeps each group small and
focused while still producing one compact report. If a test fails, its full
path — for example Math / Addition — identifies the failing group.
$ julia main.jl Test Summary: | Pass Total Time Math | 3 3 0.0s
Package Test Layout
A Julia package keeps its tests in a test directory next to
src and Project.toml. The file
test/runtests.jl is the entry point that Pkg.test
executes.
using Test
using MyPackage
@testset "double" begin
@test double(2) == 4
end
In a real project this code lives in test/runtests.jl, where
MyPackage is the package under test. Running
Pkg.test("MyPackage"), or pkg> test in Pkg's REPL
mode, creates a temporary environment and runs this file. Its printed output
includes temporary directory names and timings that change on every run, so no
fixed output block is shown here. The same @testset macros work in
a standalone script, as the earlier examples demonstrate.
Best Practices for Testing
Good tests are small, independent and quick to run.
- Test one behaviour per assertion: Failures become easier to diagnose.
- Name test sets meaningfully: The name appears in the summary and in failure reports.
- Cover boundaries: Include zero, empty inputs and error cases.
- Use approximate equality for floats: Avoid exact comparisons of rounded results.
- Keep tests independent: Each test should work regardless of the order in which tests run.
- Run tests before publishing: Use
Pkg.testso a package is checked in a clean environment.
Source
Pkg.jl package management documentation
This tutorial introduced the Test standard library:
@test, @testset, @test_throws,
approximate comparisons, nested groups and the test/runtests.jl
layout used by Pkg.test. Modules are described in
Julia modules, package management in
Julia Pkg, and assertions as a macro in
Julia macros.
Author
List all Julia tutorials.