c++ - 检查 C 函数是否在 C++ CppUTest 中被调用

标签 c++ c unit-testing cpputest

我有一个嵌入式 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() 函数。

这解释了类似的问题:https://github.com/cpputest/cpputest/issues/1054

关于c++ - 检查 C 函数是否在 C++ CppUTest 中被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52723810/

相关文章:

c++ - Python 的无返回功能模仿 C++

c++ - "separating module interface/implementation unit in a different source file"和使用 "private module fragment"之间的权衡是什么

c++ - 使用 C++ Builder 5 实现 Word 自动化

c++ - 用direct2d创建位图图集, "current bitmap"指的是什么?

c - 如何实现向 C 添加非 C 功能的库?

.net - 如何在 WPF 应用程序 (Windows XP) 中全局设置区域性?

python - 尝试导入测试床时出错

c - 为什么程序应该进入 if 语句却没有进入

c# - 单元测试依赖于 UserManager 和 RoleManager

Python单元测试: Reporting Exception as Failure