单元测试 #

一、测试基础 #

1.1 @test宏 #

julia
using Test

@test 1 + 1 == 2
@test 2 * 3 == 6
@test sqrt(16) == 4

1.2 测试比较 #

julia
using Test

@test 1 + 1 ≈ 2.0
@test 0.1 + 0.2 ≈ 0.3
@test pi ≈ 3.14159 atol=0.0001

1.3 测试异常 #

julia
using Test

@test_throws ErrorException error("test")
@test_throws DivideError 1 / 0
@test_throws BoundsError [1, 2, 3][10]

1.4 测试日志 #

julia
using Test

@test_logs (:info, "info message") @info "info message"
@test_logs (:warn, "warning") @warn "warning"

二、测试集 #

2.1 @testset #

julia
using Test

@testset "Math operations" begin
    @test 1 + 1 == 2
    @test 2 - 1 == 1
    @test 2 * 3 == 6
    @test 6 / 2 == 3
end

2.2 嵌套测试集 #

julia
using Test

@testset "All tests" begin
    @testset "Addition" begin
        @test 1 + 1 == 2
        @test 2 + 2 == 4
    end
    
    @testset "Multiplication" begin
        @test 2 * 3 == 6
        @test 3 * 4 == 12
    end
end

2.3 参数化测试集 #

julia
using Test

@testset "Square of $x" for x in [1, 2, 3, 4, 5]
    @test x^2 == x * x
end

2.4 自定义测试集名称 #

julia
using Test

for op in [:+, :-, :*]
    @testset "Operation: $op" begin
        @test 2 * 3 == 6
    end
end

三、测试断言 #

3.1 @test #

基本断言:

julia
@test true
@test 1 == 1
@test 1 < 2

3.2 @test_throws #

测试异常:

julia
@test_throws ErrorException error("test")
@test_throws ArgumentError parse(Int, "abc")

3.3 @test_logs #

测试日志:

julia
@test_logs (:info, "message") @info "message"
@test_logs min_level=Logging.Warn @info "ignored"

3.4 @test_deprecated #

测试弃用警告:

julia
@test_deprecated old_function()

3.5 @test_skip #

跳过测试:

julia
@test_skip broken_function()

3.6 @test_broken #

标记已知失败:

julia
@test_broken 1 == 2

四、测试组织 #

4.1 测试文件结构 #

text
test/
├── runtests.jl
├── test_basic.jl
├── test_advanced.jl
└── test_utils.jl

4.2 runtests.jl #

julia
using Test

@testset "All tests" begin
    include("test_basic.jl")
    include("test_advanced.jl")
end

4.3 测试函数 #

julia
using Test

function test_addition()
    @test 1 + 1 == 2
    @test 2 + 2 == 4
end

function test_multiplication()
    @test 2 * 3 == 6
    @test 3 * 4 == 12
end

@testset "All tests" begin
    test_addition()
    test_multiplication()
end

五、实践练习 #

5.1 练习1:测试数学函数 #

julia
using Test

@testset "Math functions" begin
    @testset "factorial" begin
        @test factorial(0) == 1
        @test factorial(1) == 1
        @test factorial(5) == 120
        @test_throws ErrorException factorial(-1)
    end
    
    @testset "fibonacci" begin
        @test fibonacci(0) == 0
        @test fibonacci(1) == 1
        @test fibonacci(10) == 55
    end
end

5.2 练习2:测试数据结构 #

julia
using Test

@testset "Stack" begin
    s = Stack{Int}()
    
    @testset "initial state" begin
        @test isempty(s)
        @test length(s) == 0
    end
    
    @testset "push and pop" begin
        push!(s, 1)
        @test !isempty(s)
        @test length(s) == 1
        
        push!(s, 2)
        @test length(s) == 2
        
        @test pop!(s) == 2
        @test length(s) == 1
    end
end

5.3 练习3:测试异常 #

julia
using Test

@testset "Error handling" begin
    @testset "division" begin
        @test safe_divide(10, 2) == 5.0
        @test safe_divide(10, 0) === nothing
    end
    
    @testset "parsing" begin
        @test safe_parse(Int, "123") == 123
        @test safe_parse(Int, "abc") === nothing
    end
end

六、总结 #

本章我们学习了:

  1. @test宏:基本测试断言
  2. 测试集:@testset组织测试
  3. 测试断言:各种测试宏
  4. 测试组织:文件和函数组织

接下来让我们学习Julia的测试框架!

最后更新:2026-03-27