Skip to content

Julia Testing Guide — Test.jl, Unit Tests, and Test-Driven Development

DodaTech Updated 2026-06-28 2 min read

In this tutorial, you will learn about Julia Testing Guide. We cover key concepts, practical examples, and best practices to help you master this topic.

Julia Test.jl provides testing macros (@test, @testset, @test_throws, @test_broken) for Unit Testing, with @inferred for type stability checks and @test_logs for log message verification -- all runnable through Pkg.test.

Basic Tests

using Test

# Simple assertions
@test 1 + 1 == 2
@test 2 + 2 != 5

# Approximate equality
@test 0.1 + 0.2  0.3
@test isapprox(0.1 + 0.2, 0.3, atol=1e-10)

# Type assertions
@test typeof(1 + 2) == Int

Test Sets

@testset "Math Operations" begin
    @test 1 + 1 == 2
    @test 2 * 3 == 6
    @test 10 / 2 == 5
end

@testset "String Operations" begin
    @test uppercase("hello") == "HELLO"
    @test length("hello") == 5
end

Expected Errors

# Test that an error is thrown
@test_throws DivideError 1 / 0
@test_throws DomainError sqrt(-1)
@test_throws MethodError "hello" + 1

# Custom error messages
@test 1 + 1 == 2 "Basic arithmetic failed"

# Test for no exception
@test_nowarn sleep(0.1)

Advanced Testing

# Type stability check
@inferred sqrt(4.0)          # should return Float64
@inferred my_function(x)     # check type stability

# Broken tests (known failures)
@test_broken 1 + 1 == 3

# Log testing
@test_logs (:warn, "deprecated") deprecated_function()

# Property-based testing
@test forall(x -> x^2 >= 0, 1:100)

Running Tests

# Run all test files
julia --project=. -e 'using Pkg; Pkg.test()'

# Run specific file
julia -e 'include("test/runtests.jl")'

# With test coverage
julia --code-coverage=user -e 'include("test/runtests.jl")'

Common Mistakes

1. Not rounding approximate comparisons

0.1 + 0.2 == 0.3 is false due to floating point. Use (isapprox) for float comparisons.

2. Tests depending on order

Tests should be independent. Shared mutable state causes order-dependent failures.

3. Not testing edge cases

Test empty collections, boundary values, negative numbers, and error cases.

Practice Questions

1. How do you assert equality? @test a == b. For floats, use @test a ≈ b (approx).

2. How do you group related tests? @testset "Name" begin ... end groups tests and reports results together.

3. How do you verify an error is thrown? @test_throws ErrorType expression passes if expression throws the specified error type.

FAQ

{{< faq question="How do I run all tests in a package?" >}} pkg> test or using Pkg; Pkg.test(). This runs test/runtests.jl.

{{< faq question="What does @inferred check?" >}} @inferred checks that a function call returns a value whose type can be inferred by the compiler. Fails if type-unstable.

{{< faq question="Can I write property-based tests?" >}} Yes. Use @test forall(property, generator) or use the PropCheck.jl package for more advanced property-based testing. {{< /faq >}}

What's Next

Now learn about creating Julia packages.

Topic Description Link
Packages Creating Julia packages {{< ref "20-packages" >}}
Clojure Start learning Clojure Clojure

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro