我有一个嵌入式 C/C++ 项目,我想用 CppUTest 为它编写单元测试。我想做的一个简单测试是确保在测试期间调用特定的 C 函数。
假设我在中定义了两个 C 函数function.h
:
void success(void)
{
// ... Do Something on success
}
void bid_process(void)
{
bool happy = false;
// ... processing modifiying 'happy' and setting it to 'true'
if (happy)
success(); // Call to success
}
我想测试这个功能big_process
,如果没有被调用,我希望我的测试失败success
。
为此,我在单独的测试文件 test.cpp 中编写了一些CppUTests:
#include <CppUTest/CommandLineTestRunner.h>
#include "CppUTest/TestHarness.h"
#include "CppUTestExt/MockSupport.h"
#ifdef __cplusplus
extern "C"
{
#include "function.h"
}
#endif
TEST_GROUP(TestGroup)
{
void teardown()
{
mock().clear();
}
};
TEST(TestGroup, Test_big_process)
{
mock().expectOneCall("success"); // success should be called by the call to big process
big_process();
mock().checkExpectations();
}
我手动检查了big_process
它工作正常并且正在调用success
,但现在我希望我的测试能够做到这一点。但测试失败并告诉我:
Mock Failure: Expected call did not happen.
EXPECTED calls that did NOT happen:
success -> no parameters
所以我的问题很简单:如何确保success
在 期间被调用big_process
?