Zig Test
detect_test.zig
$ zig test detect_test.zig
1/1 test "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.
$ zig test unused_fn.zig
1/1 test "unused function"...OK
Note that, while in and ReleaseSafe modes, emits a call to @panic, in and ReleaseSmall modes, it is really undefined behavior. The implementation of is as simple as:
This means that when testing in ReleaseFast or ReleaseSmall mode, assert
is not sufficient to check the result of a computation:
const std = @import("std");
const assert = std.debug.assert;
test "assert in release fast mode" {
assert(false);
Better practice for checking the output when testing is to use :
test.zig
$ zig test test.zig --release-fast
1/1 test "assert in release fast mode"...test failure
Tests failed. Use the following command to reproduce the failure:
/home/andy/dev/zig/docgen_tmp/test
zig test
has a few command line parameters which affect the compilation. See 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.