Is there a way to debug tests when run as a build step?

I have a build step in my build.zig which runs all available tests:

const lib_tests = b.addTest(.{
    .root_source_file = b.path("src/lib.zig"),
    .target = target,
    .optimize = optimize,
});
lib_tests.addIncludePath(b.path("src/wayland"));
lib_tests.addCSourceFiles(.{
    .files = &protocol_sources,
});
lib_tests.linkLibC();
lib_tests.linkSystemLibrary("wayland-client");

const run_lib_unit_tests = b.addRunArtifact(lib_tests);

const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);

I need to debug this test run, but as there is no binary emitted, I don’t know if that’s possible. I know there is a emit-bin parameter for zig test, but because of the extra linking steps it complains if I try to run the test that way.

Is there perhaps something I can add to my build.zig to get it to write a binary for the tests being run? Or is there another option for debugging these tests?

1 Like

Hi, you can install an artifact in a couple of ways here:

  1. With zig build:
b.installArtifact(lib_tests);
  1. With zig build test:
const ins_lib_unit_tests = b.addInstallArtifact(lib_tests, .{});
test_step.dependOn(&ins_lib_unit_tests.step);

You can make it depend on zig build as well, if you add this line at the end:

b.default_step.dependOn(test_step);
4 Likes

If you just want a temporary solution without modifying build.zig, you can get the path to the test binary in the cache if you pass --verbose to zig build test

5 Likes

Two great solutions, thanks! The --verbose flag gets me up and running, but I’ll have to look more into those build.zig improvements for the future.

1 Like

As a third approach, if you happen to use VSCode, you can install the Zig Language Extras plugin, and launch debugging of a single test from within the file. It has some limitations, mainly that the test name has to be “in quotes” for the regex to find it, but it’s great to have, I rely on it extensively.

1 Like