From aee9d3674ff4c2d4e2a0e81bf7b2ea93f56b90c8 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Tue, 27 Oct 2020 10:19:00 +0100 Subject: Make sure teardown is always called, even if test or setup fails. --- uunit.h | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/uunit.h b/uunit.h index c9cb258..cdb6097 100644 --- a/uunit.h +++ b/uunit.h @@ -66,13 +66,22 @@ public: for(auto suite = uUnit::suite_list; suite; suite = suite->next_unit) { - for(auto test : suite->tests) + for(const auto& test : suite->tests) { ++test_num; try { - suite->setup(); - test.func(); + try + { + suite->setup(); + test.func(); + } + catch(...) + { + // call teardown and ignore exceptions + try { suite->teardown(); } catch(...) {} + throw; // rethrow setup/test.func exception + } suite->teardown(); } catch(test_result& result) -- cgit v1.2.3