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 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.
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);
In case somebody is here and the filtering is not working when the main.zig have named function that have the refAllDeclsRecursive just remove the name so it is not filtered with your filter
Before fix the test have name like: test “main tests referenced” { … }
After fix:
zig
//this test cannot have name as when we are filtering tests it will filter this test also
//and because this test is registering other tests they will not be registered and will be filtered out
test {
std.debug.print("Registering all tests to be run\n", .{});
//optional control what level to print in all registered tests
std.testing.log_level = constants.test_log_level;
std.testing.refAllDeclsRecursive(@This());
}
So the command below will work as expected when correclty registered build option: test-filter is present:
zig build test -Dtest-filter="test1" -Dtest-filter="test2"
I am just posting it here as I spend couple of hours chasing why the filtering is not working for me so hopefully it will help somebody with similar issue.