2 * CppU - C++ unit testing framework
3 * ---------------------------------
4 * Copyright (c)2007 Daniel Fiser <danfis@danfis.cz>
7 * This file is part of CppU.
9 * CppU is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation; either version 3 of
12 * the License, or (at your option) any later version.
14 * CppU is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * Header file of CppU.
36 * TODO: Dopsat vsechny aserce (zachytavani vyjimek -
37 * assertException(vyraz, ExceptionType);
42 * Struct describing state after ran test.
45 std::string test_name
; //!< name of test
46 int fails
; //!< num of failed assertations
47 int successes
; //!< num of succeed assertations
49 inline void clean(void){fails
=0;successes
=0;}
53 * Abstract base class.
55 * This defines the base methods which must have all classes which handle
56 * any tests, test cases and so on.\n
57 * Because of this class it's possible to register test suite into other
62 const std::string _name
;
64 TestBase(const std::string n
) : _name(n
){}
69 * This runs all tests (and subtests) and print output to out.
70 * Parameter level specifies on which level is this test running.
72 virtual void run(std::ostream
&out
, const int level
= 0) = 0;
77 * This two functions determines whether class is TestCase or
80 virtual bool isTestCase() const = 0;
81 virtual bool isTestSuite() const = 0;
88 * Methods which returns number of assertations, tests etc.\
89 * Inheriting class must redefine only methods which make sense
92 virtual int assertations() const { return 0;}
93 virtual int assertationsFailed() const { return 0;}
94 virtual int assertationsSucceed() const { return 0;}
95 virtual int testSuites() const { return 0;}
96 virtual int testSuitesFailed() const { return 0;}
97 virtual int testSuitesSucceed() const { return 0;}
98 virtual int testCases() const { return 0;}
99 virtual int testCasesFailed() const { return 0;}
100 virtual int testCasesSucceed() const { return 0;}
101 virtual int tests() const { return 0;}
102 virtual int testsFailed() const { return 0;}
103 virtual int testsSucceed() const { return 0;}
107 * This method prints summary independently on type of class (TestCase
109 * Class which inherits from TestBase must redefine only methods which
110 * count assertations, tests etc...
112 virtual void printSummary(std::ostream
&out
= std::cout
) const;
118 * Main test case class.
119 * This class collects all individual tests in one test case.
121 class TestCase
: public TestBase
{
123 int _tests_failed
; //!< number of failed tests
124 int _tests_succeed
; //!< number of succeed tests
125 int _assertations_failed
; //!< number of failed assertations
126 int _assertations_succeed
; //!< number of succeed assertations
129 * State of currently running test.
130 * This struct must be empty before running next test method.
132 test_state_t _current_test
;
135 * Which output stream has to be used.
141 * Returns formated error message
143 std::string
_getErrorMessage(const std::string file
, const int line
,
144 const std::string test_method
,
145 const std::string error_message
) const;
153 * _prepareTest("nameOfTheTestMethod");
158 void _prepareTest(std::string test_name
);
163 * Record assertation as succeed
165 void _successAssertation();
168 * Record assertations as failed and print error message to stdout.
170 void _failAssertation(std::string file
, int line
,
171 std::string error_message
);
174 * Constructor is protected because new test case (which inherits from
175 * TestCase class) defines its own constructor which only call
176 * this one with its name as argument.
178 explicit TestCase(const std::string
& n
) : TestBase(n
),
179 _tests_failed(0), _tests_succeed(0),
180 _assertations_failed(0), _assertations_succeed(0){}
182 virtual ~TestCase(){}
184 bool isTestCase() const { return true;}
185 bool isTestSuite() const { return false;}
187 virtual void setUp(){}
188 virtual void tearDown(){}
191 * Method describing tests.\
192 * This must be redefined in extended class.
194 virtual void runTests() = 0;
197 * This run the test case.
198 * If verbose is set to true, errors will be printed out during
201 void run(std::ostream
&out
= std::cout
, const int level
= 0);
204 int assertations() const { return _assertations_failed
+
205 _assertations_succeed
;}
206 int assertationsFailed() const { return _assertations_failed
;}
207 int assertationsSucceed() const { return _assertations_succeed
;}
208 int tests() const {return _tests_failed
+ _tests_succeed
;}
209 int testsFailed() const {return _tests_failed
;}
210 int testsSucceed() const {return _tests_succeed
;}
216 * Main test suite class.
217 * This class collect all test cases and run them together.\n
218 * TestSuite can collect and mix test cases and other test suites together,
219 * so it's possible to create hirearchy of tests.\n
220 * Test cases and test suites are run in order they are registered.
222 class TestSuite
: public TestBase
{
225 * List of registered tests.
227 std::vector
<TestBase
*> _tests
;
230 * Register test cases and test suites for later run.
232 void _reg(TestBase
*tc
){ _tests
.push_back(tc
); }
235 * Erase all tests from vector.
240 * Inheriting class must redefine this method.\
241 * In this method should be only list of _reg() methods calls.\
242 * This method is ran in run() method as first.
244 virtual void _registerAll() = 0;
246 TestSuite(const std::string
& n
) : TestBase(n
){}
247 virtual ~TestSuite();
249 bool isTestCase() const { return false;}
250 bool isTestSuite() const { return true;}
253 * This run the tests.
254 * If verbose is set to true, errors will be printed out during
257 void run(std::ostream
&out
= std::cout
, const int level
= 0);
260 int assertations() const;
261 int assertationsFailed() const;
262 int assertationsSucceed() const;
263 int testSuites() const;
264 int testSuitesFailed() const;
265 int testSuitesSucceed() const;
266 int testCases() const;
267 int testCasesFailed() const;
268 int testCasesSucceed() const;
270 int testsFailed() const;
271 int testsSucceed() const;
276 * Function which runs test (given in argument) and all
277 * outputs go into out.
279 void runTest(TestBase
* test
, std::ostream
&out
= std::cout
);
281 /************** MACROS ****************/
282 /********** TestCase **********/
284 * Create test case.\ This macro must be enclosed by TEST_CASE_END macro.
286 #define TEST_CASE(name) \
287 class name : public TestCase{ \
289 name() : TestCase(#name){}
291 * Close definition of test case.
293 #define TEST_CASE_END }
296 * Introduce block where has to be all test methods registered.
298 #define TESTS void runTests()
301 * Register method in TESTS block.
303 #define REG_TEST(test_name) \
304 _prepareTest(#test_name); \
309 /********** TestSuite **********/
311 * Create test suite.\ This macro has to be enclosed by TEST_SUITE_END
314 #define TEST_SUITE(name) \
315 class name : public TestSuite{ \
317 name() : TestSuite(#name){} \
318 void _registerAll() \
321 * Close definition of test suite.
323 #define TEST_SUITE_END \
327 * Register test case or test suite into test suite.\ This macro has to be
328 * between TEST_SUITE and TEST_SUITE_END macro.
336 * Runs test suite or test case.\ Output goes to stdout.
341 * Runs test suite or test case.\ Output goes to specified out.
343 #define RUN_OUT(name, out) \
344 runTest(new name(), out);
346 /********** assertations **********/
350 * Assertations with suffix 'M' (e.g.\ assertTrueM) is variation of macro
351 * where is possible to specify error message.
353 #define assertTrueM(a, message) \
355 _successAssertation(); \
357 _failAssertation(__FILE__, __LINE__, message); \
359 #define assertTrue(a) \
360 assertTrueM((a), #a " is not true")
362 #define assertFalseM(a, message) \
363 assertTrueM(!(a), message)
364 #define assertFalse(a) \
365 assertFalseM((a), #a " is not false")
367 #define assertEqualsM(a,b,message) \
368 assertTrueM((a) == (b), message)
369 #define assertEquals(a,b) \
370 assertEqualsM((a), (b), #a " not equals " #b)
372 #define assertNotEqualsM(a,b,message) \
373 assertTrueM((a) != (b), message)
374 #define assertNotEquals(a,b) \
375 assertNotEqualsM((a), (b), #a " equals " #b)
377 #define assertThrow(expr, except) \
380 assertTrueM(false, #except " is not throwed - nothing throwed"); \
382 }catch(std::exception &e){ \
383 assertTrueM(false, #except " is not throwed, but std::exception throwed"); \
384 }catch(std::string &e){ \
385 assertTrueM(false, #except " is not throwed, but std::string throwed"); \
389 /* vim: set sw=4 ts=4 et ft=cpp tw=75 cindent: */