Library with Tests
A library project with a test suite.
build.zig
zig
const std = @import("std");
const buildx = @import("buildx");
pub fn build(b: *std.Build) void {
_ = buildx.project(b, .{
.name = "math",
.root = "src/root.zig",
.kind = .library,
.tests = true,
});
}src/root.zig
zig
pub fn add(a: i32, b: i32) i32 {
return a + b;
}
test "add" {
try std.testing.expectEqual(@as(i32, 5), add(2, 3));
}Build & Test
bash
zig build testOutput:
All tests passed.Key Points
kind = .librarycreates a library instead of executabletests = trueadds ateststep that compiles and runs tests- The test step uses the same source file by default