CCUnit: テストケースを書く

CCUnit 2.1 A C Unit Testing Library

- メインページ
- 関連ページ
- モジュール
- データ構造
- ファイル

全て データ構造 ファイル 関数 変数 型定義 列挙型 列挙型の値 マクロ定義 グループ ページ
データ構造 | 型定義 | 関数
テストケースを書く

テスト関数の集まりのテストケースを書きます。 [詳細]

テストケースを書くのコラボレーション図

## データ構造
struct | CCUnitTestCase
| Wraps a test case with setUp and tearDown function. [詳細]
struct | CCUnitTestFunc
| A single test function object. [詳細]

## 型定義
typedef struct CCUnitTestCase | CCUnitTestCase
| Wraps a test case with setUp and tearDown function.
typedef struct CCUnitTestFunc | CCUnitTestFunc
| A single test function object.

## 関数
CCUnitTestCase * | ccunit_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.
CCUnitTestFunc * | ccunit_addNewTestFunc (CCUnitTestCase *testCase, const char *name, const char *desc, void(*runTest)())
| add new test func to test case.
CCUnitTestFunc * | ccunit_newTestFunc (const char *name, const char *desc, void(*runTest)())
| Create new test function.
void | ccunit_deleteTestFunc (CCUnitTestFunc *f)
| Delete test function.

## 説明

テスト関数の集まりのテストケースを書きます。

## 型定義

typedef struct CCUnitTestCase CCUnitTestCase

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.

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.

参照:
  CCUnitTestResult, CCUnitTestCase, CCUnitTestSuite, MakeSuite,

typedef struct CCUnitTestFunc CCUnitTestFunc

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 ()
{
CCUnitTestResult* r;
CCUnitTestCase* c = ccunit_newTestCase ("math test");
CCUnitTestFunc* f = ccunit_newTestFunc ("testAdd", "add test", testAdd);
ccunit_addTestFunc (c, f);
r = ccunit_runTestCase (c);
return 0;
}

参照:
  CCUnitTestCase, CCUnitTestSuite, MakeSuite

## 関数

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

add new test func to test case.

引数:

  testCase | test case.
  name | test case name.
  desc | test case description.
  runTest | run test function.

戻り値:
  new test func

参照先 ccunit_addTestFunc(), ccunit_newTestFunc(), と runTest().

参照元 ccunit_newTestSuiteFromDfn().

関数の呼び出しグラフ:

呼出しグラフ:

void ccunit_addTestFunc | ( | CCUnitTestCase * | testCase,
|  | CCUnitTestFunc * | f
| ) |  |

add test function to test case.

引数:

  testCase | test case.
  f | test function.

参照先 ccunit_addList(), ccunit_deleteTestFunc(), CCUnitTestFunc::name, CCUnitTestCase::setUp, CCUnitTestCase::setUpBeforeClass, CCUnitTestCase::tearDown, CCUnitTestCase::tearDownAfterClass, と CCUnitTestCase::testFuncs.

参照元 ccunit_addNewTestFunc().

関数の呼び出しグラフ:

呼出しグラフ:

void ccunit_deleteTestCase | ( | CCUnitTestCase * | testCase | ) |
| inline

Destructs test case.

引数:

  testCase | deleting case.

参照先 ccunit_deleteTest(), と CCUnitTestCase::test.

関数の呼び出しグラフ:

void ccunit_deleteTestFunc | ( | CCUnitTestFunc * | f | ) |
| inline

Delete test function.

引数:

  f | deleting function.

参照先 CCUnitTestFunc::desc, CCUnitTestFunc::name, と safe_free.

参照元 ccunit_addTestFunc(), と destroy().

呼出しグラフ:

CCUnitTestCase* ccunit_newTestCase | ( | const char * | name | ) |

create new test case.

引数:

  name | test case name.

戻り値:
  new test case.

参照先 ccunit_initList(), ccunit_initTest(), ccunitTypeTestCase, destroy(), CCUnitTestCase::name, run(), safe_strdup, CCUnitTestCase::test, と CCUnitTestCase::testFuncs.

参照元 ccunit_newTestSuiteFromDfn().

関数の呼び出しグラフ:

呼出しグラフ:

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

Create new test function.

引数:

  name | function name.
  desc | function description.
  runTest | run test function.

戻り値:
  new test function object.

参照先 CCUnitTestFunc::desc, CCUnitTestFunc::name, CCUnitTestFunc::runTest, runTest(), と safe_strdup.

参照元 ccunit_addNewTestFunc().

関数の呼び出しグラフ:

呼出しグラフ:

| hosts this site. |  | Send comments to: CCUnit Developer |