[ CCUnit project page ] [ CCUnit home page ]
Data Structures | Typedefs | Functions

Writing test fixture

Collaboration diagram for Writing test fixture:

Data Structures

struct  CCUnitTestCase
 A single test object. More...
struct  CCUnitTestFixture
 Wraps a test case with setUp and tearDown methods. More...

Typedefs

typedef struct CCUnitTestCase CCUnitTestCase
 A single test object.
typedef struct CCUnitTestFixture CCUnitTestFixture
 Wraps a test case with setUp and tearDown methods.

Functions

CCUnitTestFixtureccunit_newTestFixture (const char *name, CCUnitTestFunc *setUp, CCUnitTestFunc *tearDown)
 create new test fixture.
void ccunit_addTestCase (CCUnitTestFixture *fixture, CCUnitTestCase *testCase)
 add test case to test fixture.
CCUnitTestCaseccunit_addNewTestCase (CCUnitTestFixture *fixture, const char *name, const char *desc, void(*runTest)())
 add new test case to test fixture.
CCUnitTestCaseccunit_newTestCase (const char *name, const char *desc, void(*runTest)())
 Create new test case.
CCUnitTestFuncccunit_newTestFunc (const char *name, const char *desc, void(*func)())
 Create new test function.
void ccunit_deleteTestCase (CCUnitTestCase *testCase)
 Delete test case.
void ccunit_deleteTestFunc (CCUnitTestFunc *testFunc)
 Delete test func.

Typedef Documentation

A single test object.

For each test implement a function which interacts with the case. Verify the expected results with assertions specified by calling CCUNIT_ASSERT on the expression you want to test:

 void testAdd ()
 {
   int result = value1 + value2;
   CCUNIT_ASSERT (result == 5);
 }

 ...

 void MathTest_newTestCase_testAdd ()
 {
   return ccunit_newTestCase ("addTest", "add test", addTest);
 }
See also:
CCUnitTestFixture, CCUnitTestSuite, MakeSuite

Wraps a test case with setUp and tearDown methods.

A TestCase is used to provide a common environment for a set of test cases.

To define a test case, do the following:

  • the case is defined by static variables
  • initialize the case state by setUp function
  • clean-up after a test by tearDown function

Each test runs in its own case so there can be no side effects among test runs. Here is an example:

 static int value1, value2;

 void setUp_MathTest ()
 {
   value1 = 2;
   value2 = 3;
 }

 ...

 CCUnitTestFixture* MathTest_newTestFixture ()
 {
   return ccunit_newTestFixture ("MathTest", setUp_MathTest, NULL);
 }

For each test implement a function which interacts with the case. Verify the expected results with assertions specified by calling CCUNIT_ASSERT on the expression you want to test:

 void testAdd ()
 {
   int result = value1 + value2;
   CCUNIT_ASSERT (result == 5);
 }

 ...

 void MathTest_newTestCase_testAdd ()
 {
   return ccunit_newTestCase ("addTest", "add test", addTest);
 }

The tests to be run can be collected into a TestSuite.

 CCUintTestSuite* MathTest_suite ()
 {
   CCUnitTestSuite* suite = ccunit_newTestSuite ("MathTest");
   CCUnitTestFixture* fixture = MathTest_newTestFixture ();
   ccunit_addTestFixture (suite, fixture);
   ccunit_addTestCase (fixture, MathTest_newTestCase_testAdd ());
   ccunit_addTestCase (fixture, MathTest_newTestCase_testDivZero ())
   return suite;
 }

Once the functions are defined you can run them. To do this, use a TestRunner.

   CCUnitTestRunner *runner = ccunit_newTestRunner (stdout);
   CCUnitTestSuite *suite = MathTest_suite ();
   runner->run (runner, suite);

A command line tool have been created for convenience. It is located in src/tools/ccunit_makeSuite.c.

See also:
CCUnitTestResult, CCUnitTestCase, CCUnitTestSuite, MakeSuite,

Function Documentation

CCUnitTestCase* ccunit_addNewTestCase ( CCUnitTestFixture fixture,
const char *  name,
const char *  desc,
void(*)()  runTest 
)

add new test case to test fixture.

Parameters:
fixture test fixture.
name test case name.
desc test case description.
runTest run test function.
Returns:
new test case

References ccunit_addTestCase(), ccunit_newTestCase(), and runTest().

Here is the call graph for this function:

void ccunit_addTestCase ( CCUnitTestFixture fixture,
CCUnitTestCase testCase 
) [inline]

add test case to test fixture.

Parameters:
fixture test fixture.
testCase test case

References ccunit_addList(), and CCUnitTestFixture::testCases.

Referenced by ccunit_addNewTestCase().

Here is the call graph for this function:

Here is the caller graph for this function:

void ccunit_deleteTestCase ( CCUnitTestCase testCase  ) 

Delete test case.

Parameters:
testCase deleting case.

Referenced by destroy().

Here is the caller graph for this function:

void ccunit_deleteTestFunc ( CCUnitTestFunc testFunc  )  [inline]

Delete test func.

Parameters:
testFunc deleting func.

Referenced by destroy().

Here is the caller graph for this function:

CCUnitTestCase * ccunit_newTestCase ( const char *  name,
const char *  desc,
void(*)()  runTest 
)

Create new test case.

Parameters:
name case name.
desc case description.
runTest run test function.
Returns:
new test case

Referenced by ccunit_addNewTestCase().

Here is the caller graph for this function:

CCUnitTestFixture* ccunit_newTestFixture ( const char *  name,
CCUnitTestFunc setUp,
CCUnitTestFunc tearDown 
)

create new test fixture.

Parameters:
name test fixture name.
setUp test fixture setUp function.
tearDown test fixture tearDown function.
Returns:
new test fixture.

References ccunit_initList(), ccunit_initTest(), ccunitTypeFixture, destroy(), CCUnitTestFixture::name, run(), safe_strdup, CCUnitTestFixture::setUp, CCUnitTestFixture::tearDown, CCUnitTestFixture::test, and CCUnitTestFixture::testCases.

Here is the call graph for this function:

CCUnitTestFunc * ccunit_newTestFunc ( const char *  name,
const char *  desc,
void(*)()  func 
) [inline]

Create new test function.

Parameters:
name func name.
desc func description.
func run test function.
Returns:
new test func.
SourceForge.jp hosts this site. Send comments to: CCUnit Developer