测试框架 #

一、基准测试 #

1.1 BenchmarkTools #

julia
using Pkg
Pkg.add("BenchmarkTools")

using BenchmarkTools

@benchmark sum(rand(1000))
@btime sum(rand(1000))

1.2 基准测试参数 #

julia
using BenchmarkTools

@benchmark sum(rand(1000)) seconds=10
@benchmark sum(rand(1000)) samples=100

1.3 基准测试结果 #

julia
using BenchmarkTools

result = @benchmark sum(rand(1000))
result.times
result.memory
result.allocs

1.4 比较基准 #

julia
using BenchmarkTools

@btime sum($rand(1000))
@btime sum($rand(1000))

二、测试覆盖率 #

2.1 Coverage包 #

julia
using Pkg
Pkg.add("Coverage")

using Coverage

2.2 生成覆盖率报告 #

julia
using Coverage

coverage = process_folder()
covered_lines, total_lines = get_summary(coverage)

2.3 CI集成 #

在GitHub Actions中:

yaml
- name: Run tests with coverage
  run: julia --project=@. -e 'using Pkg; Pkg.test(coverage=true)'

三、模拟测试 #

3.1 Mocking包 #

julia
using Pkg
Pkg.add("Mocking")

using Mocking

3.2 基本模拟 #

julia
using Mocking

patch = @patch f(x) = 42

apply(patch) do
    @test f(100) == 42
end

3.3 模拟外部依赖 #

julia
using Mocking

patch = @patch read(file) = "mocked content"

apply(patch) do
    @test read("file.txt") == "mocked content"
end

四、测试工具 #

4.1 ReferenceTests #

julia
using Pkg
Pkg.add("ReferenceTests")

using ReferenceTests

@test_reference "references/output.txt" my_function()

4.2 Aqua #

自动质量检查:

julia
using Pkg
Pkg.add("Aqua")

using Aqua

Aqua.test_all(MyPackage)

4.3 JET #

静态分析:

julia
using Pkg
Pkg.add("JET")

using JET

@report_opt my_function()

五、实践练习 #

5.1 练习1:基准测试套件 #

julia
using BenchmarkTools

suite = BenchmarkGroup()

suite["sort"] = @benchmarkable sort(rand(1000))
suite["sum"] = @benchmarkable sum(rand(1000))
suite["find"] = @benchmarkable findfirst(x -> x > 0.5, rand(1000))

tune!(suite)
run(suite)

5.2 练习2:性能回归测试 #

julia
using BenchmarkTools

const BASELINE = Dict(
    "sum" => 100.0,
    "sort" => 1000.0
)

function check_performance()
    results = Dict(
        "sum" => @belapsed sum(rand(1000)) * 1e6,
        "sort" => @belapsed sort(rand(1000)) * 1e6
    )
    
    for (name, time) in results
        baseline = BASELINE[name]
        if time > baseline * 1.5
            @warn "Performance regression: $name took $time μs (baseline: $baseline μs)"
        end
    end
end

5.3 练习3:集成测试 #

julia
using Test

@testset "Integration tests" begin
    @testset "Database" begin
        db = setup_database()
        @test db !== nothing
        
        @testset "CRUD operations" begin
            id = create_record(db, "test")
            @test id > 0
            
            record = read_record(db, id)
            @test record.name == "test"
            
            update_record(db, id, "updated")
            record = read_record(db, id)
            @test record.name == "updated"
            
            delete_record(db, id)
            @test read_record(db, id) === nothing
        end
        
        cleanup_database(db)
    end
end

六、总结 #

本章我们学习了:

  1. 基准测试:BenchmarkTools
  2. 测试覆盖率:Coverage包
  3. 模拟测试:Mocking包
  4. 测试工具:ReferenceTests、Aqua、JET

接下来让我们学习Julia的性能优化!

最后更新:2026-03-27