I’m trying to run a test in my project with --test-filter but it fails with this error
src/root.zig:19:15: error: C import failed
pub const c = @cImport({
^~~~~~~~
src/root.zig:19:15: note: libc headers not available; compilation does not link against libc
referenced by:
c: src/apu/buffer.zig:2:33
apu.buffer.SampleBuffer: src/apu/buffer.zig:11:13
14 reference(s) hidden; use '-freference-trace=16' to see all references
.zig-cache/o/ecb56e828e2cd32e919574aae0b45d08/cimport.h:1:10: error: 'SDL3/SDL.h' file not found
#include <SDL3/SDL.h>
I’ve tried using this code, but it didn’t work:
const mod_tests = b.addTest(.{
.root_module = mod,
});
const sdl_lib_tests = sdl_dep.artifact("SDL3_test");
mod_tests.linkLibrary(sdl_lib_tests);
// A run step that will run the test executable.
const run_mod_tests = b.addRunArtifact(mod_tests);
// Creates an executable that will run `test` blocks from the executable's
// root module. Note that test executables only test one module at a time,
// hence why we have to create two separate ones.
const exe_tests = b.addTest(.{
.root_module = exe.root_module,
});
exe_tests.linkLibrary(sdl_lib_tests);
// A run step that will run the second test executable.
const run_exe_tests = b.addRunArtifact(exe_tests);
I’ve already used .link_libc = true on the mod creation.
I’m using this for the SDL dependency. The full build.zig can be found here.
That worked thanks! By the way, do you know how to configure launch.json in VSCode so that can debug individual tests? I think it uses the zig test command by default so it’s going to throw the same error.
For debugging tests as far as I know you can only debug the generated test executable, so specify it as any normal program. I assume filtering tests will prevent those tests from being included in the final executable and if not you can always set a breakpoint.