例如,假设 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 _cpptest_TestObject('g');。这将用 g 填充内部 char 缓冲区。
- 将后置条件宏替换为具有用于控制测试结果的验证的部分。
修改后的测试用例如下所示:
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 将断言。