How do I set up a custom path for the output test binay

I have this code in my build.zig to output the test binary, but when running zig build test the binary is not created. What am I doing wrong here?

    const test_filters = b.option([]const []const u8, "test-filter", "Skip tests that do not match any filter") orelse &[0][]const u8{};

    const mod_tests = b.addTest(.{
        .name = "mod-test",
        .root_module = mod,
        .filters = test_filters,
    });
    b.installArtifact(mod_tests);

    const run_mod_tests = b.addRunArtifact(mod_tests);

    const exe_tests = b.addTest(.{
        .name = "exe-test",
        .root_module = exe.root_module,
        .filters = test_filters,
    });
    b.installArtifact(exe_tests);

    const run_exe_tests = b.addRunArtifact(exe_tests);

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

installArtifact will depend on the default step “install” you get the binary by doing zig build install which is the same as zig build.

If you want it to depend on a different step use addInstallArtifact instead

const exe_tests_artifact = b.addInstallArtifact(exe_tests, .{});
test_step.dependOn(&exe_tests_artifact.step);
2 Likes

Nice, that worked. How can I set a different path, currently it outputs the binary to zig-out/bin, I want to be zig-out/tests/?

    const exe_tests_artifact = b.addInstallArtifact(exe_tests, .{
        .dest_dir = .{ .override = .{ .custom = "tests" } },
    });
4 Likes