Reputation: 5847
Seems functions can't be declared inside Zig's test
blocks. Are there any guidelines for how to deal with functions used exclusively in test
blocks, i.e., for cleanup?
test "messy test that requires cleanup" {
createManyFiles();
defer deleteTestFiles(); // Used only for testing
expect(something);
}
Upvotes: 2
Views: 204
Reputation: 3577
The standard library makes them private and puts them above the test (example)
fn createManyFiles() {
... only used in a test
}
fn deleteTestFiles() {
... only used in a test
}
test "messy test that requires cleanup" {
createManyFiles();
defer deleteTestFiles();
try std.testing.expect(something);
}
Zig only compiles functions that are used, so your regular build will not have any test functions in it.
If test functions are needed in multiple files, they can be made public or moved into their own file. They still won't be included in a regular build unless they are used outside of a test.
Seems functions can't be declared inside Zig's test blocks
You technically can do this, by embedding a function in a struct.
test "function in test" {
const MyStruct = struct{
fn myFunction() void {}
};
MyStruct.myFunction();
}
It's best to avoid this when you can.
Upvotes: 3