CCUnit  2.1
A C Unit Testing Library
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Data Structures | Typedefs | Functions
Writing Test Case
Collaboration diagram for Writing Test Case:

Data Structures

struct  CCUnitTestCase
 Wraps a test case with setUp and tearDown function. More...
struct  CCUnitTestFunc
 A single test function object. More...

Typedefs

typedef struct CCUnitTestCase CCUnitTestCase
 Wraps a test case with setUp and tearDown function.
typedef struct CCUnitTestFunc CCUnitTestFunc
 A single test function object.

Functions

CCUnitTestCaseccunit_newTestCase (const char *name)
 create new test case.
void ccunit_deleteTestCase (CCUnitTestCase *testCase)
 Destructs test case.
void ccunit_addTestFunc (CCUnitTestCase *testCase, CCUnitTestFunc *f)
 add test function to test case.
CCUnitTestFuncccunit_addNewTestFunc (CCUnitTestCase *testCase, const char *name, const char *desc, void(*runTest)())
 add new test func to test case.
CCUnitTestFuncccunit_newTestFunc (const char *name, const char *desc, void(*runTest)())
 Create new test function.
void ccunit_deleteTestFunc (CCUnitTestFunc *f)
 Delete test function.

Detailed Description


Typedef Documentation

Wraps a test case with setUp and tearDown function.

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;
}
...
CCUnitTestCase* MathTest_newTestCase ()
{
return ccunit_newTestCase ("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 ("testAdd", "add test", testAdd);
}

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

CCUintTestSuite* MathTest_suite ()
{
CCUnitTestSuite* suite = ccunit_newTestSuite ("MathTest");
CCUnitTestCase* tcase = MathTest_newTestCase ();
ccunit_addTestCase (suite, tcase);
ccunit_addTestCase (tcase, MathTest_newTestCase_testAdd ());
ccunit_addTestCase (tcase, MathTest_newTestCase_testDivZero ())
return suite;
}

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

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,

A single test function 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);
}
...
int main ()
{
CCUnitTestCase* c = ccunit_newTestCase ("math test");
CCUnitTestFunc* f = ccunit_newTestFunc ("testAdd", "add test", testAdd);
return 0;
}
See also:
CCUnitTestCase, CCUnitTestSuite, MakeSuite

Function Documentation

CCUnitTestFunc* ccunit_addNewTestFunc ( CCUnitTestCase testCase,
const char *  name,
const char *  desc,
void(*)()  runTest 
)

add new test func to test case.

Parameters:
testCasetest case.
nametest case name.
desctest case description.
runTestrun test function.
Returns:
new test func

References ccunit_addTestFunc(), ccunit_newTestFunc(), and runTest().

Referenced by ccunit_newTestSuiteFromDfn().

Here is the call graph for this function:

Here is the caller graph for this function:

void ccunit_addTestFunc ( CCUnitTestCase testCase,
CCUnitTestFunc f 
)

add test function to test case.

Parameters:
testCasetest case.
ftest function.

References ccunit_addList(), ccunit_deleteTestFunc(), CCUnitTestFunc::name, CCUnitTestCase::setUp, CCUnitTestCase::setUpBeforeClass, CCUnitTestCase::tearDown, CCUnitTestCase::tearDownAfterClass, and CCUnitTestCase::testFuncs.

Referenced by ccunit_addNewTestFunc().

Here is the call graph for this function:

Here is the caller graph for this function:

void ccunit_deleteTestCase ( CCUnitTestCase testCase)
inline

Destructs test case.

Parameters:
testCasedeleting case.

References ccunit_deleteTest(), and CCUnitTestCase::test.

Here is the call graph for this function:

void ccunit_deleteTestFunc ( CCUnitTestFunc f)
inline

Delete test function.

Parameters:
fdeleting function.

References CCUnitTestFunc::desc, CCUnitTestFunc::name, and safe_free.

Referenced by ccunit_addTestFunc(), and destroy().

Here is the caller graph for this function:

CCUnitTestCase* ccunit_newTestCase ( const char *  name)

create new test case.

Parameters:
nametest case name.
Returns:
new test case.

References ccunit_initList(), ccunit_initTest(), ccunitTypeTestCase, destroy(), CCUnitTestCase::name, run(), safe_strdup, CCUnitTestCase::test, and CCUnitTestCase::testFuncs.

Referenced by ccunit_newTestSuiteFromDfn().

Here is the call graph for this function:

Here is the caller graph for this function:

CCUnitTestFunc * ccunit_newTestFunc ( const char *  name,
const char *  desc,
void(*)()  runTest 
)

Create new test function.

Parameters:
namefunction name.
descfunction description.
runTestrun test function.
Returns:
new test function object.

References CCUnitTestFunc::desc, CCUnitTestFunc::name, CCUnitTestFunc::runTest, runTest(), and safe_strdup.

Referenced by ccunit_addNewTestFunc().

Here is the call graph for this function:

Here is the caller graph for this function:

SourceForge.jp hosts this site. Send comments to: CCUnit Developer