8000 Introducing additional functionality to allow client code to stop a unit · unittest-cpp/unittest-cpp@e7b56d4 · GitHub
[go: up one dir, main page]

Skip to content

Commit e7b56d4

Browse files
ohz10pjohnmeyer
authored andcommitted
Introducing additional functionality to allow client code to stop a unit
test when an assert fails. The following macro has been added: REQUIRE An example of when these type of checks are useful: std::vector<int> v = foo(); REQUIRE(CHECK_EQUAL(3, v.size())); // test stops here on a fail // so we don't segfault looking at // v[0] below. CHECK_EQUAL(1, v[0]); CHECK_EQUAL(2, v[1]); CHECK_EQUAL(3, v[2]); Multiple checks are supported as follows: REQUIRE({ CHECK_EQUAL(1, 2); CHECK(true); }; In the multiple check scenario, all the checks in the REQUIRE block will be run. After which, if any failures were reported, the TEST case will be stopped. When UNITTEST_NO_EXCEPTIONS is defined, REQUIRE is a noop.
1 parent 5b34768 commit e7b56d4

File tree

3 files changed

+871
-0
lines changed

3 files changed

+871
-0
lines changed

UnitTest++/RequireMacros.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef UNITTEST_REQUIREMACROS_H
2+
#define UNITTEST_REQUIREMACROS_H
3+
4+
#include "HelperMacros.h"
5+
#include "ExceptionMacros.h"
6+
#include "CurrentTest.h"
7+
8+
#ifdef REQUIRE
9+
#error UnitTest++ redefines REQUIRE
10+
#endif
11+
12+
#ifndef UNITTEST_NO_EXCEPTIONS
13+
#define REQUIRE(test) \
14+
UNITTEST_MULTILINE_MACRO_BEGIN \
15+
int const failuresBeforeTest = UnitTest::CurrentTest::Results()->GetFailureCount(); \
16+
test; \
17+
int const failuresAfterTest = UnitTest::CurrentTest::Results()->GetFailureCount(); \
18+
if(failuresAfterTest > failuresBeforeTest) \
19+
{ \
20+
UT_THROW(UnitTest::AssertException()); \
21+
} \
22+
UNITTEST_MULTILINE_MACRO_END
23+
#endif
24+
#endif
25+
26+
#ifdef UNITTEST_NO_EXCEPTIONS
27+
#define REQUIRE(test) test;
28+
#endif

UnitTest++/UnitTestPP.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "Config.h"
55
#include "TestMacros.h"
66
#include "CheckMacros.h"
7+
#include "RequireMacros.h"
78
#include "TestRunner.h"
89
#include "TimeConstraint.h"
910
#include "ReportAssert.h"

0 commit comments

Comments
 (0)
0