Testing And Benchmarking in Julia

Unit testing is an important part of writing principled Julia programs. The Test module provides the tools we need to write unit tests, and we can run tests using the @test macro or the @testset macro for grouping tests.

For the sake of demonstration, this code is in a script, but it could be in any module. Testing code typically lives in the same module as the code it tests.

using Test

# We'll be testing this simple implementation of an
# integer minimum. Typically, the code we're testing
# would be in a source file named something like
# `intutils.jl`, and the test file for it would then
# be named `test_intutils.jl`.

function int_min(a, b)
    a < b ? a : b
end

# A test is created by using the @test macro.

@test int_min(2, -2) == -2

# Writing tests can be repetitive, so it's idiomatic to
# use a table-driven style, where test inputs and
# expected outputs are listed in a table and a single loop
# walks over them and performs the test logic.

@testset "Table-driven tests for int_min" begin
    test_cases = [
        (0, 1, 0),
        (1, 0, 0),
        (2, -2, -2),
        (0, -1, -1),
        (-1, 0, -1),
    ]

    for (a, b, expected) in test_cases
        @test int_min(a, b) == expected
    end
end

# Benchmark tests in Julia are typically done using the BenchmarkTools
# package. Here's how you might set up a benchmark:

using BenchmarkTools

@benchmark int_min(1, 2)

To run the tests, you can execute the script containing these tests. If you’ve saved this in a file called test_int_min.jl, you can run:

$ julia test_int_min.jl

This will run all the tests and display the results.

For benchmarking, the output will show detailed performance statistics, including the median time, memory allocation, and other metrics.

Remember that in Julia, it’s common to organize code into modules and use the built-in test framework, which can be invoked by running Julia with the --project flag and using the test command in the package REPL mode. This allows for more structured testing, especially in larger projects.

Julia’s testing ecosystem is rich and flexible, allowing for a variety of testing styles and approaches. The Test module provides many useful macros and functions for assertions, test set organization, and output customization.