When running zig test
for something like (example from here)
const std = @import("std");
fn failingFunction() error{Oops}!void {
return error.Oops;
}
test "returning an error" {
failingFunction() catch |err| {
try std.testing.expect(err == error.Oops);
std.debug.print("return from catched error: {}", .{err});
return;
};
}
The output to the console is messed up like
Test [3/6] test.returning an error...All 6 tests passed.
as both std.debug.print and the test runner print to stderr.
I can see why you wouldn't want to have print-outs in library tests, at least not in something you ship to others. However, my experience is that sometimes, this can be useful while you develop the library. According to this post on reddit and this issue on github the described behavior seems ...somehow intended? The docs just tell me that "The default test runner and the Zig Standard Library's testing namespace output messages to standard error.", not that I have to avoid print
in tests. So, is there a way to have "clean" printing to stdout while running tests?
zig version
0.12.0-dev.1298+da06269d7
Edit 2024-02-19: after diving deeper into Zig, here's a couple of thing I found useful:
print
calls in tested code and tests in general. Tests are run in parallel (multi-threaded), so this can cause crashes due to race conditions. If you can't hold back on printing, at least remove anything not absolutely needed ;-)