#How does one run all tests in all files ?
1 messages · Page 1 of 1 (latest)
you have to reference them from a central test, for example I put something like this in main.zig and then run zig test main.zig
test {
_ = @import("./x.zig");
_ = @import("./y.zig");
_ = @import("./z/a.zig");
_ = @import("./z/b.zig");
}
there is no simpler way that i know of
i should say, you have to reference each file containing tests
You can adjust the example from zig init-exe by adding all files for which zig build test will be run.
Yes! That helps immensely. It is clear and concise, I appreciate your input here.
there's a helper function called std.testing.refAllDecls (and refAllDeclsRecursive) that will do this for you for any pub decls in the file, you can see it used in lib/std/std.zig: https://github.com/ziglang/zig/blob/f3a1b5c481646ee35813cbe8bb2b0a3979df3ab8/lib/std/std.zig#L133
yes, but doesn't that only work well in that situation (std.zig) because that file already imports all those other modules?
i guess the same thing would work if you happen to already have a file that imports most of the modules in the app
yeah it's a somewhat common/useful pattern for libraries, see e.g. https://github.com/jecolon/ziglyph/blob/main/src/ziglyph.zig which then allows users to just add ziglyph.zig as a package and then @import("ziglyph") and get everything from there
Great! This is a new idea for me. I am not familiar with this at all, must look into that and learn about it. Thanks!
I would prehaps instead do
comptime {
if (@import("builtin").is_test) {
// import statements
}
}
to avoid fake test numbers
i don't understand, why would what i posted cause fake test numbers?
'cause you have a test whose only purpose is to reference other tests
if you do that enough in enough files, you end up with a few dozen non-test tests
i do it in exactly one file
sure, that's fine ig
I'm just sayin
I personally prefer the number of tests to reflect the exact number of actual number of tests
part of my original question had been motivated by the fact that I presently use a loop (and a nested build function), to be able to build serveral executables with 1 invocation of "zig build". But, trying to extend this to build multiple tests ... failed and failed.
So, I was happy to see that a number of "smooth" solutions exist. Moreoever, this quest lead me to read "Mitchell Hasimoto" in depth article on the Zig Build System Internals. I learned much about build from this and recommend it to others. ... sigh ... that is my long-winded "Thank you" to the group here.