Tests runner customization

Hello there,

I would like to implement a custom test runner to ignore prints on stderr and always having a summary of runned tests.

The easiest way as I played around implementing a runner from scratch, surprisingly enough seems to be “only” changing the runner mode from .server to .simple but I don’t know how
Ideally something like:
exe_tests.test_runner.?.mode = .simple;
but:

  • of course it does not work as I did not explicitly define the test runner
  • I have no idea of the true implications of .simple Vs .server, maybe this is just a bad idea

The sources say:

/// Test runners can either be “simple”, running tests when spawned and terminating when the
/// tests are complete, or they can use std.zig.Server over stdio to interact more closely
/// with the build system.

A broken workaround I can come up with so far is to copy-paste lib/compiler/test_runner.zig sources and define a TestRunner in build.zig using it,
but if I can avoid bringing in such a dependency which I do not understand yet, it would be great

Maybe an example would be clearer:

diff after a zig init

--- a/src/main.zig
+++ b/src/main.zig
@@ -36,6 +36,7 @@ test “simple test” {
var list: std.ArrayList(i32) = .empty;
defer list.deinit(gpa); // Try commenting this out and see if zig detects the memory leak!
try list.append(gpa, 42);
+ std.debug.print(“debug print\n”, .{});
try std.testing.expectEqual(@as(i32, 42), list.pop());
}

Output:

$> zig build test
test
└─ run test w
debug print
failed command: ./.zig-cache/o/4c8eb4a9251f590fadd269953201f667/test --cache-dir=./.zig-cache --seed=0x514716e6 --listen=-

$> echo $?
0

Note that I understand the choice of making debug prints / prints on stderr an error and no summary on default.
Only “failed command” and return 0 have almost always opposite meaning to me, but it’s here off topic.

Bonus question: Is there in the sources a nice entry point to better understand the test runner mechanism (so far poking around at random files brings something, but maybe there is an easier path)

Thanks!

I’m not sure if you know how to integrate a custom test_runner, so here’s the way: Testing for freestanding - #4 by dimdin