例如,假设 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');。这样,内部 char 缓冲区将使用 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 将在测试条件失败时触发断言。