gmock 可以用于存根 C 函数吗? [英] Can gmock be used for stubbing C functions?

查看:37
本文介绍了gmock 可以用于存根 C 函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 gmock 的新手,所以我想知道如何存根在被测函数中调用的简单 C 函数以进行单元测试.

I am new to gmock, so I want to know how can I stub simple C function called in a function under test for Unit Testing.

示例:

int func(int a)
{
  boolean find;
  // Some code
  find = func_1();
  return find;
}

我搜索了 gmock,据我所知 gmock 不提供存根简单 C 函数的功能,因此我想问一下 gmock 是否提供模拟或存根 func_1 的功能?

I have searched about gmock and in my understanding gmock does not provide functionality to stub simple C functions, therefore I want to ask does gmock provides functionality to mock or stub func_1?

如果不是,我如何在不更改源代码的情况下在我的测试代码中手动存根 func_1?我正在使用谷歌测试框架进行单元测试.

If not how can I stub func_1 manually in my test code without changing source code? I am using google test framework for unit testing.

谢谢.

推荐答案

我最近发现自己处于同样的情况.我必须为以下内容编写单元测试用 C 编写的库,而这些库又依赖于同样用 C 编写的其他库.所以我想模拟所有依赖项的函数调用使用 gmock.让我通过一个例子来解释我的方法.

I found myself in the same situation lately. I had to write unit tests for libraries written in C, which in turn had dependencies to other libraries also written in C. So I wanted to mock all function calls of dependencies using gmock. Let me explain my approach by an example.

假设要测试的代码(库A)调用另一个库中的函数,lib_x_function():

Assume the code to be tested (library A) calls a function from another library, lib_x_function():

lib_a_function()
{
   ...
   retval = lib_x_function();
   ...
}

所以,我想模拟库 X.因此我写了一个接口类和一个文件 lib_x_mock.h 中的模拟类:

So, I want to mock the library X. Therefore I write an interface class and a mock class in a file lib_x_mock.h:

class LibXInterface {
public:
   virtual ~LibXInterface() {}
   virtual int lib_x_function() = 0;
}

class LibXMock : public LibXInterface {
public:
   virtual ~LibXMock() {}
   MOCK_METHOD0(lib_x_function, int());
}

另外我创建了一个源文件(比如,lib_x_mock.cc),它定义了一个存根对于实际的 C 函数.这将调用模拟方法.注意 extern对模拟对象的引用.

Additionally I create a source file (say, lib_x_mock.cc), that defines a stub for the actual C function. This shall call the mock method. Note the extern reference to the mock object.

#include lib_x.h
#include lib_x_mock.h
extern LibXMock LibXMockObj;    /* This is just a declaration! The actual
                                   mock obj must be defined globally in your
                                   test file. */

int lib_x_function()
{
    return LibXMockObj.lib_x_function();
}

现在,在测试库 A 的测试文件中,我必须定义模拟对象全局,以便在您的测试中和从lib_x_mock.cc.这是 lib_a_tests.cc:

Now, in the test file, which tests the library A, I must define the mock object globally, so that it is both reachable within your tests and from lib_x_mock.cc. This is lib_a_tests.cc:

#include lib_x_mock.h

LibXMock LibXMockObj;  /* This is now the actual definition of the mock obj */

...
TEST_F(foo, bar)
{
   EXPECT_CALL(LibXMockObj, lib_x_function());
   ...
}

这种方法非常适合我,我有几十个测试和几个嘲笑的图书馆.但是,我有一些疑问,是否可以创建一个全局模拟对象 - 我在一个单独问题 并仍在等待答案.除此之外,我对解决方案很满意.

This approach works perfectly for me, and I have dozens of tests and several mocked libraries. However, I have a few doubts if it is ok to create a global mock object - I asked this in a separate question and still wait for answers. Besides this I'm happy with the solution.

更新:关于全局对象的问题可以通过创建对象轻松解决,例如在测试装置的构造函数中,并将指向该对象的指针存储在全局变量中.

UPDATE: The problem about the global object can be easily remedied by creating the object e.g. in the constructor of the test fixture, and just storing a pointer to that object in a global variable.

但是,请注意我刚刚发布的对这个问题的替代答案.

However, also note my alternative answer to this question, that I just posted.

这篇关于gmock 可以用于存根 C 函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆