我有一个嵌入式 C/C++ 项目,我想使用 CppUTest 为其编写单元测试。我想做的一个简单测试是确保在测试期间调用特定的 C 函数。
假设我在 function.h
中定义了两个 C 函数:
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
所以我的问题很简单:如何确保在 big_process
期间调用 success
?
最佳答案
您正确设置了模拟期望,但没有将模拟连接到 success() 函数。
关于c++ - 检查 C 函数是否在 C++ CppUTest 中被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52723810/