Issues with module tests compilation in Zig - can't test and missing symbols in executable

Hi,

I’m trying to test modules in my Zig project. In build.zig. Unfortunately, module tests are not working as expected.

Current setup and issues

  • Using fullTest in build.zig for test configuration
  • Tried both approaches to include module tests:
    refAllDecls(@This());
    
    and
    _ = @import("<module_name>");
    
  • When examining compiled test executable, only symbols from kernel_test.zig are present
  • Other module symbols are missing in the final test binary

Environment

  • Using latest nightly build of Zig

Could someone point out what might be wrong with this setup? Is there something specific about fullTest that I’m missing, or is there a different approach I should take to ensure module tests are properly included?

Rgrds,
Maciek

Your build.zig looks really messy. But I think you need to use your kernel.zig in the build and write into the file:

test {
    @import("std").testing.refAllDeclsRecursive(@This());
}

This worked for me, here in 0.13.0 (maybe something has changed):

Note that this will only reference public declarations of that file, and likewise recursively.

@removewingman @n0s4 Messy or not, I’ve already tried both recursive approaches and public declarations (tests are public functions by default). None of these solutions resolved the issue. I’ve already “tested” it with kernel.zig.

@removewingman @n0s4 OK, finally I understand the module testing approach in Zig (at least I hope so). Instead of using refAllDecls (which Andrew Kelley has indicated is unlikely to survive until version 1.0), each module should have its own separate test unit (addTest). I discovered this approach after running zig init on a vanilla project - while it was new to me, it’s both clean and effective. My build function.

1 Like