2 /* Copyright (C) 2003, 2010 TSUTSUMI Kikuo.
3 This file is part of the CCUnit Library.
5 The CCUnit Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License
7 as published by the Free Software Foundation; either version 2.1 of
8 the License, or (at your option) any later version.
10 The CCUnit Library is distributed in the hope that it will be
11 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the CCUnit Library; see the file COPYING.LESSER.
17 If not, write to the Free Software Foundation, Inc., 59 Temple
18 Place - Suite 330, Boston, MA 02111-1307, USA.
29 #ifndef CCUNITTESTFIXTURE_H
30 #define CCUNITTESTFIXTURE_H
32 #include <ccunit/CCUnitConfig.h>
33 #include <ccunit/CCUnitList.h>
34 #include <ccunit/CCUnitTest.h>
35 #include <ccunit/CCUnitTestCase.h>
36 #include <ccunit/CCUnitTestResult.h>
40 * @defgroup CCUnitTestFixture TestFixture
41 * A test fixture defines the fixture to run multiple tests.
47 * Wraps a test case with setUp and tearDown function.
49 * A TestCase is used to provide a common environment for a set
52 * To define a test case, do the following:
53 * - the case is defined by static variables
54 * - initialize the case state by setUp function
55 * - clean-up after a test by tearDown function
57 * Each test runs in its own case so there
58 * can be no side effects among test runs.
62 * static int value1, value2;
64 * void setUp_MathTest ()
72 * CCUnitTestFixture* MathTest_newTestFixture ()
74 * return ccunit_newTestFixture ("MathTest", setUp_MathTest, NULL);
78 * For each test implement a function which interacts with the
79 * case. Verify the expected results with assertions specified by
80 * calling CCUNIT_ASSERT on the expression you want to test:
85 * int result = value1 + value2;
86 * CCUNIT_ASSERT (result == 5);
91 * void MathTest_newTestCase_testAdd ()
93 * return ccunit_newTestCase ("testAdd", "add test", testAdd);
97 * The tests to be run can be collected into a TestSuite.
100 * CCUintTestSuite* MathTest_suite ()
102 * CCUnitTestSuite* suite = ccunit_newTestSuite ("MathTest");
103 * CCUnitTestFixture* fixture = MathTest_newTestFixture ();
104 * ccunit_addTestFixture (suite, fixture);
105 * ccunit_addTestCase (fixture, MathTest_newTestCase_testAdd ());
106 * ccunit_addTestCase (fixture, MathTest_newTestCase_testDivZero ())
111 * Once the functions are defined you can run them. To do this, use a
115 * CCUnitTestRunner *runner = ccunit_newTestRunner (stdout);
116 * CCUnitTestSuite *suite = MathTest_suite ();
117 * runner->run (runner, suite);
120 * A command line tool have been created for convenience. It is located
121 * in src/tools/ccunit_makeSuite.c.
123 * @see CCUnitTestResult, CCUnitTestCase, CCUnitTestSuite, CCUnitMakeSuite,
125 * @ingroup WritingTestFixture
127 typedef struct CCUnitTestFixture
129 CCUnitTest test; /**< super class */
130 const char* name; /**< test fixture name */
131 CCUnitTestFunc* setup_setUp; /**< setUp for a fixture */
132 CCUnitTestFunc* setup_tearDown; /**< tearDown for a fixture */
133 CCUnitTestFunc* setUp; /**< setUp for each cases */
134 CCUnitTestFunc* tearDown; /**< tearDown for each cases */
135 CCUnitList testCases; /**< test cases */
139 * TestFixture definition structure
141 typedef struct CCUnitTestFixtureDfn
143 CCUnitTestDfn test; /**< super class */
144 const char* name; /**< test fixture name */
145 CCUnitTestFunc setup_setUp; /**< setUp for a fixture */
146 CCUnitTestFunc setup_tearDown; /**< tearDown for a fixture */
147 CCUnitTestFunc setUp; /**< setUp for each cases */
148 CCUnitTestFunc tearDown; /**< tearDown for each cases */
149 CCUnitTestFunc* testCases; /**< test cases */
150 } CCUnitTestFixtureDfn;
153 * create new test fixture.
155 * @param name test fixture name.
156 * @param setUp test case setUp function.
157 * @param tearDown test case tearDown function.
158 * @return new test fixture.
159 * @ingroup WritingTestFixture
161 extern CCUnitTestFixture* ccunit_newTestFixture (const char* name,
162 CCUnitTestFunc* setUp,
163 CCUnitTestFunc* tearDown);
166 * set test fixture setup functions.
168 * @param name test fixture.
169 * @param setup_setUp test fixture setUp function.
170 * @param setup_tearDown test fixture tearDown function.
171 * @ingroup WritingTestFixture
173 extern inline void ccunit_setTestFixtureSetup (CCUnitTestFixture* fixture,
174 CCUnitTestFunc* setup_setUp,
175 CCUnitTestFunc* setup_tearDown);
178 * add test case to test fixture.
180 * @param fixture test fixture.
181 * @param testCase test case
182 * @ingroup WritingTestFixture
184 extern inline void ccunit_addTestCase (CCUnitTestFixture* fixture,
185 CCUnitTestCase* testCase);
188 * add new test case to test fixture.
190 * @param fixture test fixture.
191 * @param name test case name.
192 * @param desc test case description.
193 * @param runTest run test function.
194 * @return new test case
195 * @ingroup WritingTestFixture
197 extern CCUnitTestCase* ccunit_addNewTestCase (CCUnitTestFixture* fixture,
203 * run test cases and collect its results.
204 * @param f test fixture.
205 * @return test result.
206 * @ingroup ExecutingTest
208 extern inline struct CCUnitTestResult* ccunit_runTestFixture (CCUnitTestFixture* f);