Is there any good way to create helper functions in a test block?

@csmyx please read this:


Personally I think one of the better options is to introduce a local namespace like this:

const std = @import("std");

test "example" {
    const local = struct {
        const Bla = struct {
            foo: u32,

            fn doSomething(self: Bla) u32 {
                return self.foo + 5;
            }
        };
    };

    const b: local.Bla = .{ .foo = 5 };
    try std.testing.expectEqual(10, b.doSomething());
}

That way you don’t have to use @This() and only use that builtin where it is actually required, for example when writing generics.

2 Likes