Zig Test
detect_test.zig
$ zig test detect_test.zig
Test 1/1 builtin.is_test...OK
All tests passed.
Zig has lazy top level declaration analysis, which means that if a function is not called, or otherwise used, it is not analyzed. This means that there may be an undiscovered compile error in a function because it is never called.
unused_fn.zig
fn unused() i32 {
return "wrong return type";
}
test "unused function" { }
pub fn assert(ok: bool) void {
This means that when testing in ReleaseFast or ReleaseSmall mode, assert
is not sufficient to check the result of a computation:
assert.zig
const std = @import("std");
const assert = std.debug.assert;
test "assert in release fast mode" {
assert(false);
}
Note that although the above example shows the test passing, this is invoking unchecked . This documentation is showing only one possible outcome of this test.
test.zig
const std = @import("std");
expect(false);
}
$ zig test test.zig --release-fast
Test 1/1 assert in release fast mode...test failure
Tests failed. Use the following command to reproduce the failure:
See the rest of the std.testing
namespace for more available functions.
zig test
has a few command line parameters which affect the compilation. See zig --help
for a full list. The most interesting one is --test-filter [text]
. This makes the test build only include tests whose name contains the supplied filter text. Again, thanks to lazy analysis, this can allow you to narrow a build to only a few functions in isolation.