mity / acutest

Simple header-only C/C++ unit testing facility.
MIT License
346 stars 96 forks source link

Call cleanup handler after assert fail #53

Closed rweickelt closed 4 years ago

rweickelt commented 4 years ago

When a testcase fails, I want to abort it immediately (TEST_ASSERT), but I also want to clean up resources. Here is my current workaround:

if (!TEST_CHECK(condition)) {
   do_cleanup();
   return;
}

I was expecting that the cleanup handler is executed regardless of the test result, but that is apparently not the case. A solution that allows parameterized cleanup would be nice to have.

mity commented 4 years ago

Thanks for the report.

I've just improved TEST_FINI machinery to be honored even when TEST_ASSERT is used and fails. (It seems it was not when the test as executed as a child process.)

However from your report I'm not really sure whether that's what you're asking for: TEST_INIT and TEST_FINI allows the suite to provide only a common initializer and cleaner respectively shared by all the unit tests implemented in the suite.

To allow a per-unit specific cleanup code would require to add a new API for it and I'm not sure how that could look like or whether it wouldn't be too cumbersome to use. But feel free to argue with me and propose something if that's what you need.

rweickelt commented 4 years ago

Thanks for fixing TEST_FINI so quickly. You are of course right. TEST_FINI is only executed once per suite while my code example above is about cleanup per testcase which is something else.

I've used acutest for the first time and if my testsuite would have had more than 1 testcase, then I would have noticed my mistake immediately. Per-testcase init/fini handlers are out of scope of this ticket and I agree with you that it would probably just complicate the API.

Thanks again.

mity commented 4 years ago

TEST_FINI is only executed once per suite

No. TEST_INIT and TEST_FINI are excuted for every running unit test. But it's always the same code. You cannot specify different cleanups for every tests.

mity commented 4 years ago

Not sure, what else to do, so closing.