4
// $Id: //poco/1.2/CppUnit/include/CppUnit/TestCase.h#1 $
8
#ifndef CppUnit_TestCase_INCLUDED
9
#define CppUnit_TestCase_INCLUDED
12
#include "CppUnit/CppUnit.h"
13
#include "CppUnit/Guards.h"
14
#include "CppUnit/Test.h"
15
#include "CppUnit/CppUnitException.h"
27
* A test case defines the fixture to run multiple tests. To define a test case
28
* 1) implement a subclass of TestCase
29
* 2) define instance variables that store the state of the fixture
30
* 3) initialize the fixture state by overriding setUp
31
* 4) clean-up after a test by overriding tearDown.
33
* Each test runs in its own fixture so there
34
* can be no side effects among test runs.
37
* class MathTest : public TestCase {
38
* protected: int m_value1;
39
* protected: int m_value2;
41
* public: MathTest (std::string name)
45
* protected: void setUp () {
52
* For each test implement a method which interacts
53
* with the fixture. Verify the expected results with assertions specified
54
* by calling assert on the expression you want to test:
56
* protected: void testAdd () {
57
* int result = value1 + value2;
58
* assert (result == 5);
61
* Once the methods are defined you can run them. To do this, use
64
* Test *test = new TestCaller<MathTest>("testAdd", MathTest::testAdd);
68
* The tests to be run can be collected into a TestSuite. CppUnit provides
69
* different test runners which can run a test suite and collect the results.
70
* The test runners expect a static method suite as the entry
71
* point to get a test to run.
73
* public: static MathTest::suite () {
74
* TestSuite *suiteOfTests = new TestSuite;
75
* suiteOfTests->addTest(new TestCaller<MathTest>("testAdd", testAdd));
76
* suiteOfTests->addTest(new TestCaller<MathTest>("testDivideByZero", testDivideByZero));
77
* return suiteOfTests;
80
* Note that the caller of suite assumes lifetime control
81
* for the returned suite.
83
* see TestResult, TestSuite and TestCaller
86
class CppUnit_API TestCase: public Test
88
REFERENCEOBJECT (TestCase)
91
TestCase(const std::string& Name);
94
virtual void run(TestResult* result);
95
virtual TestResult* run();
96
virtual int countTestCases();
97
const std::string& name() const;
98
std::string toString();
100
virtual void setUp();
101
virtual void tearDown();
104
virtual void runTest();
105
TestResult* defaultResult();
107
void assertImplementation(bool condition,
108
const std::string& conditionExpression = "",
109
long lineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER,
110
const std::string& fileName = CppUnitException::CPPUNIT_UNKNOWNFILENAME);
112
void loop1assertImplementation(bool condition,
113
const std::string& conditionExpression = "",
114
long lineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER,
115
long dataLineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER,
116
const std::string& fileName = CppUnitException::CPPUNIT_UNKNOWNFILENAME);
118
void loop2assertImplementation(bool condition,
119
const std::string& conditionExpression = "",
120
long lineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER,
121
long data1LineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER,
122
long data2LineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER,
123
const std::string& fileName = CppUnitException::CPPUNIT_UNKNOWNFILENAME);
125
void assertEquals(long expected,
127
long lineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER,
128
const std::string& fileName = CppUnitException::CPPUNIT_UNKNOWNFILENAME);
130
void assertEquals(double expected,
133
long lineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER,
134
const std::string& fileName = CppUnitException::CPPUNIT_UNKNOWNFILENAME);
136
void assertEquals(const std::string& expected,
137
const std::string& actual,
138
long lineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER,
139
const std::string& fileName = CppUnitException::CPPUNIT_UNKNOWNFILENAME);
141
void assertEquals(const void* expected,
143
long lineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER,
144
const std::string& fileName = CppUnitException::CPPUNIT_UNKNOWNFILENAME);
146
std::string notEqualsMessage(long expected, long actual);
147
std::string notEqualsMessage(double expected, double actual);
148
std::string notEqualsMessage(const void* expected, const void* actual);
149
std::string notEqualsMessage(const std::string& expected, const std::string& actual);
151
void assertNotNull(const void* pointer,
152
const std::string& pointerExpression = "",
153
long lineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER,
154
const std::string& fileName = CppUnitException::CPPUNIT_UNKNOWNFILENAME);
156
void assertNull(const void* pointer,
157
const std::string& pointerExpression = "",
158
long lineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER,
159
const std::string& fileName = CppUnitException::CPPUNIT_UNKNOWNFILENAME);
161
void fail(const std::string&message = "",
162
long lineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER,
163
const std::string& fileName = CppUnitException::CPPUNIT_UNKNOWNFILENAME);
166
const std::string _name;
170
// Constructs a test case
171
inline TestCase::TestCase(const std::string& name): _name (name)
176
// Destructs a test case
177
inline TestCase::~TestCase()
182
// Returns a count of all the tests executed
183
inline int TestCase::countTestCases()
189
// Returns the name of the test case
190
inline const std::string& TestCase::name() const
196
// A hook for fixture set up
197
inline void TestCase::setUp()
202
// A hook for fixture tear down
203
inline void TestCase::tearDown()
208
// Returns the name of the test case instance
209
inline std::string TestCase::toString()
211
const std::type_info& thisClass = typeid(*this);
212
return std::string(thisClass.name()) + "." + name();
216
// A set of macros which allow us to get the line number
217
// and file name at the point of an error.
218
// Just goes to show that preprocessors do have some
219
// redeeming qualities.
221
#define assert(condition) \
222
(this->assertImplementation((condition), (#condition), __LINE__, __FILE__))
224
#define loop_1_assert(data1line, condition) \
225
(this->loop1assertImplementation((condition), (#condition), __LINE__, data1line, __FILE__))
227
#define loop_2_assert(data1line, data2line, condition) \
228
(this->loop2assertImplementation((condition), (#condition), __LINE__, data1line, data2line, __FILE__))
230
#define assertEqualDelta(expected, actual, delta) \
231
(this->assertEquals((expected), (actual), (delta), __LINE__, __FILE__))
233
#define assertEqual(expected, actual) \
234
(this->assertEquals((expected), (actual), __LINE__, __FILE__))
236
#define assertNullPtr(ptr) \
237
(this->assertNull((ptr), #ptr, __LINE__, __FILE__))
239
#define assertNotNullPtr(ptr) \
240
(this->assertNotNull((ptr), #ptr, __LINE__, __FILE__))
242
#define failmsg(msg) \
243
(this->fail(msg, __LINE__, __FILE__))
246
} // namespace CppUnit
249
#endif // CppUnit_TestCase_INCLUDED