If you want the names to show up in the error message the only way I can think of is to make the name a comptime/generic parameter of a parent namespace and have the test namespaced underneath that, that way the name will show up in error messages.
const std = @import("std");
pub fn Test(comptime name: []const u8, Def: type) type {
return struct {
const reference = name;
const def = Def;
const Self = @This();
test Self {
try Def.impl();
}
};
}
comptime {
const files = .{ "hay", "hey", "hooi", "needle" };
for (files) |file| {
_ = Test(file, struct {
fn impl() !void {
try std.testing.expect(std.mem.eql(u8, file, "needle"));
}
});
}
}
Output:
1/4 testnames.Test("hay"[0..3],testnames.comptime__struct_163).decltest.Self...FAIL (TestUnexpectedResult)
/home/sze/development/workspace/zig/active/zig-x86_64-linux-0.15.2/lib/std/testing.zig:607:14: 0x11a1639 in expect (std.zig)
if (!ok) return error.TestUnexpectedResult;
^
/home/sze/development/workspace/zig/learn/examples/testnames.zig:21:17: 0x11a16bf in impl (testnames.zig)
try std.testing.expect(std.mem.eql(u8, file, "needle"));
^
/home/sze/development/workspace/zig/learn/examples/testnames.zig:11:13: 0x11a1736 in decltest.Self (testnames.zig)
try Def.impl();
^
2/4 testnames.Test("hey"[0..3],testnames.comptime__struct_168).decltest.Self...FAIL (TestUnexpectedResult)
/home/sze/development/workspace/zig/active/zig-x86_64-linux-0.15.2/lib/std/testing.zig:607:14: 0x11a1639 in expect (std.zig)
if (!ok) return error.TestUnexpectedResult;
^
/home/sze/development/workspace/zig/learn/examples/testnames.zig:21:17: 0x11a17bf in impl (testnames.zig)
try std.testing.expect(std.mem.eql(u8, file, "needle"));
^
/home/sze/development/workspace/zig/learn/examples/testnames.zig:11:13: 0x11a1836 in decltest.Self (testnames.zig)
try Def.impl();
^
3/4 testnames.Test("hooi"[0..4],testnames.comptime__struct_174).decltest.Self...FAIL (TestUnexpectedResult)
/home/sze/development/workspace/zig/active/zig-x86_64-linux-0.15.2/lib/std/testing.zig:607:14: 0x11a1639 in expect (std.zig)
if (!ok) return error.TestUnexpectedResult;
^
/home/sze/development/workspace/zig/learn/examples/testnames.zig:21:17: 0x11a18bf in impl (testnames.zig)
try std.testing.expect(std.mem.eql(u8, file, "needle"));
^
/home/sze/development/workspace/zig/learn/examples/testnames.zig:11:13: 0x11a1936 in decltest.Self (testnames.zig)
try Def.impl();
^
1 passed; 0 skipped; 3 failed.
error: the following test command failed with exit code 1:
/home/sze/.cache/zig/o/09d1e4dd150f78b6175abfec5c775efa/test --seed=0xf1eaa028