たとえば、C++test が次のテスト ケースを生成したものとします。
void TestSuite_getData_0::test_getData_0()
{
/* Pre-condition initialization */
/* Initializing argument 0 (this) */
/* Initializing constructor argument 1 (fill) */
char _cpptest_TestObject_arg_fill = cpptestLimitsGetMaxChar();
::Data _cpptest_TestObject (_cpptest_TestObject_arg_fill);
/* Tested function call */
const char * _result = _cpptest_TestObject.getData();
/* Post-condition check */
CPPTEST_POST_CONDITION_CSTR("const char* _result", _result)
}
関数呼び出しを実行するためのオブジェクトを作成するほかに、事前条件の初期化は必要ありません。しかし、ここでは例をあげて説明するために次の変更を行うものとします。
- ::Data オブジェクトを作成するためのコンストラクターを ::Data _this('g'); に変更します。 これは内部的な文字バッファーを g で埋めます。
- 事後条件マクロを、テスト結果を制御するための検証で置き換えます。
変更されたテスト ケースは次のようになります。
void TestSuite_getData_0::test_getData_0()
{
/* Pre-condition initialization */
/* Initializing argument 0 (this) */
::Data _cpptest_TestObject('g') ;
/* Tested function call */
const char * _result = _cpptest_TestObject.getData();
/* Post-condition check */
bool res = true;
for (int i = 0; i < _cpptest_TestObject.getSize() - 1; i++) {
if (_result[i] != 'g') {
res = false;
}
}
// To see what is in the buffer:
CPPTEST_MESSAGE(_result);
// To control test case status:
CPPTEST_ASSERT_MESSAGE("Test failed : Buffer should be filled with 'g'",
res == true);
}
注意 ! CPPTEST_MESSAGE マクロは _result バッファーの内容をテスト ケース出力として送ります。CPPTEST_ASSERT_MESSAGE マクロは、テスト条件が失敗した場合にアサーションを実行します。