Proper way of running tests in all files in a project? (using refAllDecls(@This()))

Hello, I’m unable to figure out how to trigger tests to run on all files in a library. A minimal working example is as follows.

Create a sample library with zig init.

cocoa@TheTreehouse /t/sample_testing_issue> tree                                                                                  (self) 
.
├── build.zig
├── build.zig.zon
├── src
│   ├── another_file.zig
│   ├── main.zig
│   └── root.zig
└── zig-out
    ├── bin
    │   └── sample_testing_issue
    └── lib
        └── libsample_testing_issue.a

5 directories, 7 files

In root.zig’s test section, add the following

const std = @import("std");
const testing = std.testing;
const af = @import("another_file.zig");

export fn add(a: i32, b: i32) i32 {
    return a + b;
}

test "basic add functionality" {
    testing.refAllDecls(@This());
    try testing.expect(add(3, 7) == 10);
}

And in another_file.zig I have

const std = @import("std");
const testing = std.testing;

pub fn should_fail() !void {
    try testing.expect(false);
}

test "doesn't get called" {
    std.debug.print("please work", .{});
    should_fail();
    try testing.expect(false);
}

But this doesn’t get called if I run zig build test

cocoa@TheTreehouse /t/sample_testing_issue> zig build test
cocoa@TheTreehouse /t/sample_testing_issue>

If I change the test in root.zig to this,

test "basic add functionality" {
    _ = af;
    try testing.expect(add(3, 7) == 10);
}

Then it works

cocoa@TheTreehouse /t/sample_testing_issue> zig build test
test
└─ run test
   └─ zig test Debug native 1 errors
src/another_file.zig:10:16: error: error union is ignored
    should_fail();
    ~~~~~~~~~~~^~
src/another_file.zig:10:16: note: consider using 'try', 'catch', or 'if'
error: the following command failed with 1 compilation errors:
/home/cocoa/.local/bin/zig-linux-x86_64-0.13.0/zig test -ODebug -Mroot=/tmp/sample_testing_issue
/src/root.zig --cache-dir /tmp/sample_testing_issue/.zig-cache --global-cache-dir /home/cocoa/.c
ache/zig --name test --listen=- 
Build Summary: 2/5 steps succeeded; 1 failed (disable with --summary none)
test transitive failure
└─ run test transitive failure
   └─ zig test Debug native 1 errors
error: the following build command failed with exit code 1:
/tmp/sample_testing_issue/.zig-cache/o/bd092809749618577bec7d9912da8486/build /home/cocoa/.local
/bin/zig-linux-x86_64-0.13.0/zig /tmp/sample_testing_issue /tmp/sample_testing_issue/.zig-cache 
/home/cocoa/.cache/zig --seed 0xf0559a0f -Zeac226467d631c7b test

Why doesn’t std.testing.refAllDecls call another_file.zig’s tests even though it’s imported?

refAllDecls only works on pub decls. This will work:

pub const af = @import("another_file.zig");

Ahh makes sense. I also just learned I can do this with the build system by adding the following to the build.zig

pub fn build(b: *std.Build) void {
//...
    const another_file_tests = b.addTest(.{
        .root_source_file = b.path("src/another_file.zig"),
        .target = target,
        .optimize = optimize,
    });

    const run_another_file_tests = b.addRunArtifact(another_file_tests);
//...
    const test_step = b.step("test", "Run unit tests");
    test_step.dependOn(&run_lib_unit_tests.step);
    test_step.dependOn(&run_exe_unit_tests.step);
    test_step.dependOn(&run_another_file_tests.step);
}

Thanks for the help! :heart: