libcheck / check

A unit testing framework for C
GNU Lesser General Public License v2.1
1.08k stars 210 forks source link

Check is resetting my globals between tests #304

Closed awsdert closed 4 years ago

awsdert commented 4 years ago

To optimize my tests I've been using globals that point to allocated memory, the test that allocates that memory passes just fine but on the following test the memory allocated appears to no longer exist as the global that should be pointing to it has been reset to 0 at that point, I even went as far as putting the #include <check.h> part after the declaration of those globals, I even tried putting the object inside main() like this:

alu_t _alu = {0};
alu = &_alu;

but it still got reset, why is check interfering with globals it should know nothing about?

brarcher commented 4 years ago

Are you using fork mode to run the tests?

https://libcheck.github.io/check/doc/check_html/check_4.html#No-Fork-Mode

Depending on the platform by default Check forks a process to run each unit test. This prevents one unit test for affecting another unit test's memory.

The forking can be disabled. Alternatively, there is a way to run a setup function before the fork would happen:

https://libcheck.github.io/check/doc/check_html/check_4.html#Test-Fixtures

awsdert commented 4 years ago

Thank you, srunner_set_fork_status(sr,CK_NOFORK); fixed it