Filter tests using zig build?

I can do this

zig test --test-filter "foo" src/main.zig

but I can’t do this

zig build test --test-filter "foo"

Is there a way with zig build to filter tests?

I don’t have a direct answer for you unfortunately, but as an indirect one, all that build.zig does is call into test, so it either offers a way to wire that flag to the build CLI, or it’s a missing feature that can be implemented without too much fuss.

1 Like

In case someone else ends up here looking around for the solution:

As of at least 0.12.0-dev.3563+129de47a7 (possibly earlier) you can add an option and pass that along in build.zig:

    const test_filters = b.option([]const []const u8, "test-filter", "Skip tests that do not match any filter") orelse &[0][]const u8{};
    const exe_unit_tests = b.addTest(.{
        .root_source_file = .{ .path = "src/main.zig" },
        .target = target,
        .optimize = optimize,
        .filters = test_filters,
    });
    const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
    const test_step = b.step("test", "Run unit tests");
    test_step.dependOn(&run_exe_unit_tests.step);
3 Likes