Hi! I want to build my tests as a separate binary and put it somewhere, e.g. in “zig-out/bin”.
I know that there is “-femit-bin” option for “zig test”. But I want to build tests using my build.zig file (my tests use imports). I tried to do “zig build test -femit-bin=” but that option is missing
Here is my build.zig file:
const std = @import("std");
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const lib_mod = b.createModule(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
const exe_mod = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
const test_mod = b.createModule(.{
.root_source_file = b.path("src/test.zig"),
.target = target,
.optimize = optimize,
});
test_mod.addImport("lib", lib_mod);
exe_mod.addImport("lib", lib_mod);
const lib = b.addStaticLibrary(.{
.name = "aoc2024",
.root_module = lib_mod,
});
b.installArtifact(lib);
const exe = b.addExecutable(.{
.name = "aoc2024",
.root_module = exe_mod,
});
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
const unit_tests = b.addTest(.{ .root_module = test_mod });
const run_unit_tests = b.addRunArtifact(unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_unit_tests.step);
}