关于 zig-testing
这项技能指导 Agent 使用 Zig 内置的测试工具与模式,涵盖测试运行和筛选、内存泄漏检测、comptime 测试,以及 Zig 0.14 引入的内置模糊测试器。
主要功能
- 01使用 zig test 和 zig build test 运行测试
- 02在 build.zig 中配置测试步骤
- 03筛选测试运行
- 04使用测试分配器检测泄漏
- 05编写 comptime 测试
- 06使用 Zig 0.14+ 内置模糊测试器
适用场景
- 01测试 Zig 源文件或库
- 02检查内存分配是否泄漏
- 03验证编译期函数和类型
- 04对 Zig 输入解析器进行模糊测试
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ mohitmishra786/ low-level-dev-skills --skill zig-testing安装后在 Claude Code 中输入/zig-testing确认已加载。
详细介绍
Zig 测试
用途
指导 Agent 使用 Zig 的测试系统:zig build test 和 zig test、comptime 测试模式、测试筛选器、用于检测内存泄漏的测试分配器,以及 Zig 在 0.14 中引入的内置模糊测试。
触发条件
- “如何在 Zig 中编写和运行测试?”
- “如何筛选要运行的 Zig 测试?”
- “如何检测 Zig 测试中的内存泄漏?”
- “如何在 Zig 中编写 comptime 测试?”
- “如何使用 Zig 的内置模糊测试器?”
- “如何测试 Zig 库?”
工作流
1. 编写和运行测试
// src/math.zig
const std = @import("std");
const testing = std.testing;
pub fn add(a: i32, b: i32) i32 {
return a + b;
}
pub fn divide(a: f64, b: f64) !f64 {
if (b == 0.0) return error.DivisionByZero;
return a / b;
}
// Tests live in the same file or a dedicated test file
test "add: basic addition" {
try testing.expectEqual(@as(i32, 5), add(2, 3));
try testing.expectEqual(@as(i32, -1), add(2, -3));
}
test "add: identity" {
try testing.expectEqual(@as(i32, 42), add(42, 0));
}
test "divide: normal case" {
const result = try divide(10.0, 2.0);
try testing.expectApproxEqAbs(result, 5.0, 1e-9);
}
test "divide: by zero returns error" {
try testing.expectError(error.DivisionByZero, divide(1.0, 0.0));
}
# Run all tests in a single file
zig test src/math.zig
# Run all tests via build system
zig build test
# Verbose output
zig build test -- --verbose
# Run specific test by name (substring match)
zig build test -- --test-filter "add"
2. build.zig 测试配置
// build.zig
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// Unit test step
const unit_tests = b.addTest(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
const run_unit_tests = b.addRunArtifact(unit_tests);
// Integration tests (separate executable)
const integration_tests = b.addTest(.{
.root_source_file = b.path("tests/integration.zig"),
.target = target,
.optimize = optimize,
});
const run_integration = b.addRunArtifact(integration_tests);
// `zig build test` runs both
const test_step = b.step("test", "Run all tests");
test_step.dependOn(&run_unit_tests.step);
test_step.dependOn(&run_integration.step);
// `zig build test-unit` runs only unit tests
const unit_step = b.step("test-unit", "Run unit tests");
unit_step.dependOn(&run_unit_tests.step);
}
3. 测试分配器——内存泄漏检测
std.testing.allocator 在测试模式下包装了一个 GeneralPurposeAllocator,并会在每项测试结束时报告内存泄漏:
const std = @import("std");
const testing = std.testing;
test "ArrayList: no leaks" {
// testing.allocator detects leaks and reports them
var list = std.ArrayList(u32).init(testing.allocator);
defer list.deinit(); // MUST defer to return memory
try list.append(1);
try list.append(2);
try list.append(3);
try testing.expectEqual(@as(usize, 3), list.items.len);
// If you forget defer list.deinit(), test reports a leak
}
test "custom allocation" {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
const leaked = gpa.deinit();
// .ok means no leaks; .leak means memory was not freed
testing.expect(leaked == .ok) catch @panic("memory leaked!");
}
const allocator = gpa.allocator();
const buf = try allocator.alloc(u8, 1024);
defer allocator.free(buf); // leak if forgotten
}
4. 测试断言
const testing = std.testing;
// Equality
try testing.expectEqual(expected, actual);
try testing.expectEqualStrings("hello", result_str);
try testing.expectEqualSlices(u8, expected_slice, actual_slice);
// Approximate equality (for floats)
try testing.expectApproxEqAbs(expected, actual, tolerance);
try testing.expectApproxEqRel(expected, actual, tolerance);
// Errors
try testing.expectError(error.MyError, might_fail());
try testing.expect(condition); // basic boolean assertion
// Comparison
try testing.expect(a < b);
try testing.expectStringStartsWith(str, "prefix");
try testing.expectStringEndsWith(str, "suffix");
5. Comptime 测试
Zig 可以在 comptime 阶段运行测试,适用于编译期常量和类型级检查:
const std = @import("std");
const testing = std.testing;
// Test comptime functions
fn isPowerOfTwo(n: comptime_int) bool {
return n > 0 and (n & (n - 1)) == 0;
}
// Comptime assert (compile error if false)
comptime {
std.debug.assert(isPowerOfTwo(16));
std.debug.assert(!isPowerOfTwo(15));
std.debug.assert(isPowerOfTwo(1024));
}
// Test with comptime-known values (runs at comptime in test mode)
test "isPowerOfTwo: comptime" {
comptime {
try testing.expect(isPowerOfTwo(8));
try testing.expect(!isPowerOfTwo(7));
}
}
// Type-level testing
test "type properties" {
// Verify alignment and size at comptime
comptime {
try testing.expectEqual(8, @alignOf(u64));
try testing.expectEqual(4, @sizeOf(u32));
try testing.expectEqual(true, @typeInfo(u8).Int.signedness == .unsigned);
}
}
6. 模糊测试(Zig 0.14+)
Zig 0.14 引入了使用覆盖率引导模糊测试的内置模糊测试器:
// fuzz_target.zig
const std = @import("std");
// Fuzz entry point: receives arbitrary bytes
export fn fuzz(input: []const u8) void {
// Call the function under test with fuzz input
parseInput(input) catch {};
}
fn parseInput(data: []const u8) !void {
if (data.len < 4) return error.TooShort;
const magic = std.mem.readInt(u32, data[0..4], .little);
if (magic != 0xDEADBEEF) return error.BadMagic;
// ... more parsing
}
# Run the fuzzer
zig build fuzz -Dfuzz=fuzz_target
# With corpus directory
zig build fuzz -Dfuzz=fuzz_target -- corpus/
# The fuzzer generates and saves interesting inputs to corpus/
# Crashes are saved as artifacts
# Reproduce a specific crash
zig build test-fuzz -- corpus/crash-xxxx
build.zig 模糊测试配置:
// build.zig addition
const fuzz_exe = b.addExecutable(.{
.name = "fuzz",
.root_source_file = b.path("src/fuzz_target.zig"),
.target = target,
.optimize = .ReleaseSafe,
});
fuzz_exe.root_module.fuzz = true; // enable fuzzing instrumentation
const fuzz_step = b.step("fuzz", "Run fuzzer");
fuzz_step.dependOn(&b.addRunArtifact(fuzz_exe).step);
相关技能
- 使用
skills/zig/zig-build-system配置 build.zig 并设置测试步骤 - 使用
skills/zig/zig-comptime了解通过 comptime 断言测试的 comptime 求值模式 - 使用
skills/runtimes/fuzzing了解作为替代方案的 libFuzzer/AFL 模糊测试框架 - 使用
skills/runtimes/sanitizers了解如何在 Zig 测试中使用 AddressSanitizer
评论
支持 Markdown