#Testing cleanup

1 messages · Page 1 of 1 (latest)

woeful copper
#

Is there a way for me to have global data in tests which gets cleaned up when all tests have finished?

viral smelt
#

what do you mean by cleaned up

woeful copper
#

I mean if I have something like ```foo_test.zig
const std = @import("std");
const foo = @import("foo");

const my_foo: foo.Foo = undefined;

test "init" {
my_foo = foo.init();
}

test "other_stuff" {
try std.testing.expectEqual(.bar, my_foo.other_stuff());
}

test "deinit" {
my_foo.deinit();
}```

Is there a way that I can make it so that I can defer the deinit test?

viral smelt
#

you should create a foo for each test

#

the point of unit tests is to be individually reproducible

woeful copper
#

alr

viral smelt
#

there’s no order guarantee for the tests, for the aformentioned reason

woeful copper
#

Ok, then can I have ```
const std = @import("std");
const allocator = std.testing.allocator

test {
...
}

or should I write allocator in each test
viral smelt
#

the testing allocator is initialized and deinited outside of running the tests

#

writing it each time wouldn’t change anything

woeful copper
#

alr