c++ - 为 gmock 自动生成模拟类

标签 c++ unit-testing visual-studio-2008 googlemock

我正在使用 gmock 进行 单元测试 C++ 代码。我没有使用 gtest 框架。我正在使用 Visual Studio 2008 的内置测试框架。

现在我的问题是我必须手动为真实类编写模拟类来进行单元测试。例如,如果我有一个具有 5 个函数的类,那么我必须编写具有 5 个函数的 MockAClass。有什么办法可以自动生成这些类。

class AClass
{
public:
    virtual int AFunction()
    {
        return 5;
    }
    virtual int AFunctionWithArguments(int x)
    {
        return x;
    }



class MockAClass : public AClass
{
public:
    MOCK_METHOD0(AFucntion, int());
    MOCK_METHOD1(AFunctionWithArgument, int(int x));
};

最佳答案

有一个与 google mock 项目捆绑在一起的工具可以帮助您做到这一点。但是我认为该工具需要安装 python,我不知道它在 windows 环境中的效果如何。我还发现生成的文件有时需要稍作调整才能完美运行。

这是来自 docs 的信息:

Tip: If even this is too much work for you, you'll find the gmock_gen.py tool in Google Mock's scripts/generator/ directory (courtesy of the cppclean project) useful. This command-line tool requires that you have Python 2.4 installed. You give it a C++ file and the name of an abstract class defined in it, and it will print the definition of the mock class for you. Due to the complexity of the C++ language, this script may not always work, but it can be quite handy when it does. For more details, read the user documentation.

这里是 new localization of this script .

关于c++ - 为 gmock 自动生成模拟类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9256664/

相关文章:

c++ - 错误 LNK2019 : unresolved external symbol _Direct3DCreate9Ex@8 referenced in function "protected: XYX()"

C++ 查找列中的最大和最小数

c# - 在 TDD 重构后编写更多单元测试

java - 如何在maven中测试之前取消设置环境变量

perl - 与字符串进行比较时,Test::More is_deeply 不会漂亮地打印数组/哈希引用

.net - 多个项目需要使用1个SNK文件

asp.net - VS2008 中的许多 ascx-to-one ascx.cs 错误

c++ - 使用 STL 编写内存泄漏安全代码应该避免什么?

c++ - 二维复值 vector 初始化

c++ - 使用 Qt Creator 和 Cocoa 进行编程(从当前应用程序复制所选文本)