Running tests with my own wrapper?

Hello @motiejus
welcome to ziggit :slight_smile:

zig test command line accepts the following options:

-femit-bin[=path]  Specifies the path and name of the output binary
--test-no-exec     Compiles test binary without running it

You can call:

zig test src/lib.zig -femit-bin=zig-out/bin/tester --test-no-exec
sudo setcap cap_net_admin=+ep zig-out/bin/tester
zig-out/bin/tester

For build.zig you can execute a command that depends on addTest and make addRunArtifact depends on that command execution step.

The initial build.zig:

    const unit_tests = b.addTest(.{
        .root_source_file = b.path("src/lib.zig"),
        .target = target,
        .optimize = optimize,
    });
    const run_unit_tests = b.addRunArtifact(unit_tests);

becomes:

    const unit_tests = b.addTest(.{
        .root_source_file = b.path("src/lib.zig"),
        .target = target,
        .optimize = optimize,
    });

    // command that runs setcap
    const set_cap = b.addSystemCommand(&.{
        "setcap",
        "cap_net_admin=+ep",
    });
    set_cap.addArtifactArg(unit_tests);

    const run_unit_tests = b.addRunArtifact(unit_tests);

    // tester run, depends on set_cap command
    run_unit_tests.step.dependsOn(&set_cap.step);
3 Likes