Zig Test

    detect_test.zig

    1. $ zig test detect_test.zig
    2. Test [1/1] test "builtin.is_test"...
    3. All 1 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.

    1. $ zig test unused_fn.zig
    2. Test [1/1] test "unused function"...
    3. All 1 tests passed.

    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:

    1. const std = @import("std");
    2. const assert = std.debug.assert;
    3. test "assert in release fast mode" {
    4. assert(false);

    Better practice for checking the output when testing is to use std.testing.expect:

    test.zig

    1. $ zig test test.zig -O ReleaseFast
    2. Test [2/1]
    3. test "expect in release fast mode"... FAIL (TestUnexpectedResult)
    4. 0 passed; 0 skipped; 1 failed.
    5. docgen_tmp/zig-cache/o/5f772ba34b67337c067e5630990ef24e/test /home/andy/Downloads/zig/build-release/zig

    zig test has a few command line parameters which affect the compilation. See zig --help for a full list. The most interesting one is . 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.