My tests need a global initialization and finalization.
When running one test everything is fine.
When running more tests during finalization - a global hashmap is freed - it crashes.
(I am setting my global allocator to the testing allocator during initialization).
I think you have two options either initialize (re-)creates the global hashmap or finalize doesn’t free it.
Basically finalize shouldn’t free stuff that initialize hasn’t initialized.
If this hashmap is something that always exists then it might make sense to “leak” it and just let it be cleaned up by the os on program exit, but I don’t know enough about your program to be sure.
Sure but after your first test ran it gets deinit-ed in finalize so it makes sense to re-initialize it explicitly in initialize so that it is set to empty by the second test.
But it’s only for the sake of testing this problem pops up…
Normally it’s an exe just executed once and then the global declaration as .empty should be enough.
I had a similar problem trying to use a global arena to use for all tests, which made tests crash (arena initialized with std.testing.allocator). Initializing the arena with DebugAllocator stopped the crashes, so maybe it has to do with the testing allocator not being a good fit for something that should survive single tests. (I don’t use a global arena anymore because I prefer to stick with std.testing.allocator).
Anyway in your case I’d try something like
// in main.zig or whatever your tests use as entry point
comptime {
if (builtin.is_test) {
// initialize globals here, but not with testing allocator
_ = @import("foo.zig"); // and other modules with tests
// deinitialize globals
}
}