#Help to properly setup a test step in build.zig

1 messages · Page 1 of 1 (latest)

winter rain
#

Hi there, I am setting a test step in my build.zig according to this https://ziglang.org/learn/build-system/#testing, so I added this:

    const test_step = b.step("test", "Run unit tests");
    const unit_tests = b.addTest(.{
        .root_source_file = b.path("tests/test_sum.zig"),
        .target = b.resolveTargetQuery(target),
    });

And I have got this:

$ zig build test
<omitted>/build.zig:30:40: error: expected type 'Target.Query', found 'Build.ResolvedTarget'
        .target = b.resolveTargetQuery(target),
...

Can someone help me fix this error? As a second question, how can I run all the tests in the tests folder, not just one file? (Note: the reason for a dedicated tests folder is because my src is actually C code that I want to test with Zig)

serene pollen
#

You should be able to do .target = target without the b.resolveTargetQuery

winter rain
#

[EDIT] nvm it works with .target = target.

~~Like this? .target = .{ .target = target },

still fails:

<omitted>/build.zig:30:23: error: no field named 'target' in struct 'Build.ResolvedTarget'
        .target = .{ .target = target },
```~~
#

and how about the second question, is there a way to set target to tests/test_*.zig or something similar

serene pollen
#

2.:

you can try to create a file and put this in it:

comptime {
    _ = @import("testfile1.zig");
    _ = @import("testfile2.zig");
    [...]
}

then use this file as your test target. This includes testfile1.zig and testfile2.zig and executes their tests as well

winter rain
#

Nice it works like a charm! Thank you a lot @serene pollen ! This should be in the docs somewhere, I am happy to do so as my first contribution 🙂

serene pollen
#

do you use const target = b.standardTargetOptions(.{});?

#

if yes, then your target has type std.Build.ResolvedTarget, but in the docs you linked, target has type std.Target.Query. This is why the docs use b.resolveTargetQuery, but you can directly use target.

winter rain