简体   繁体   English

如何使用 gmock 框架在 C++ 单元测试中模拟 fork 和 execlp 系统调用?

[英]How to mock fork and execlp system calls in c++ unit test using gmock framework?

I have existing C++ code, which creates a child process using fork() system call.我有现有的 C++ 代码,它使用 fork() 系统调用创建一个子进程。 And child process executes linux command using execlp() system call.子进程使用 execlp() 系统调用执行 linux 命令。 Now I want to test this code using gmock framework with 100% code coverage.现在我想使用具有 100% 代码覆盖率的 gmock 框架来测试此代码。 I googled a lot but I did not get any full proof solution.我用谷歌搜索了很多,但没有得到任何完整的证明解决方案。 Can anybody help me with this?有人可以帮我解决这个问题吗?

This is my SUT:这是我的 SUT:

int someclass :: os_fork()
{
  pid_t pid = fork();
  if (pid == -1) {
  cout<<"fork() failed with errno" <<errno <<endl;
  return false;
  }

  if (pid == 0 && (execlp("ls", "ls", nullptr) != 0))
  {
  cout<<"child process failed with errno"<<errno<<endl;
  return false;
  }
  int ppid = pid;
  return 0;
}

I want to mock fork() and execlp() system calls.我想模拟 fork() 和 execlp() 系统调用。 How could I do that?我怎么能那样做?

It's not possible to mock global functions as they are.不可能像它们一样模拟全局函数。 Instead, you can add a layer of abstraction and wrap system calls in an interface:相反,您可以添加一个抽象层并将系统调用包装在接口中:

class OsInterface {
    virtual pid_t fork() = 0;
}

class OsInterfaceImpl : public OsInterface {
    virtual pid_t fork() override 
    {
        return ::fork();
    }
}

class OsInterfaceMock : public OsInterface {
    MOCK_METHOD0(fork, pid_t());
}

This allows you to choose the real system call or mock with Dependency Injection .这允许您选择真正的系统调用或使用Dependency Injection 进行模拟。 Since you didn't provide any code, I cannot help you with that.由于您没有提供任何代码,因此我无法帮助您。

Typically you pass a pointer or a reference to the injected class to the constructor of class that should use it, but there are few other methods that could suit your project better.通常,您将一个指针或对注入类的引用传递给应该使用它的类的构造函数,但很少有其他方法可以更好地适合您的项目。

Additional advantage is that this code is more open-closed : You can easily (well, relatively easily) add for example Windows implementation without changing existing code - you'd simply provide another class inheriting from OsInterface .另一个优点是此代码更加开放封闭:您可以轻松地(好吧,相对容易地)添加例如 Windows 实现而无需更改现有代码 - 您只需提供另一个继承自OsInterface类。 You don't have to change any call to interface in your production code, just change the injected class.您不必更改生产代码中对接口的任何调用,只需更改注入的类即可。

Note that this will not run any additional processes in unit tests, but this is an advantage.请注意,这不会在单元测试中运行任何额外的进程,但这是一个优势。 You should test the code run in process separately.您应该单独测试进程中运行的代码。

It's a bit late, but you can just redefine it, and "inject" MockFunction to make some expectations.有点晚了,但你可以重新定义它,并“注入” MockFunction 以做出一些期望。 Some kind of hooking i guess.我猜是某种勾搭。

#define CALL_ORIGINAL -2    
testing::MockFunction<int()> forkHelp;

pid_t fork(void)
{
    auto mockResult = forkHelp.Call();
    if (CALL_ORIGINAL != mockResult)
    {
        return mockResult;
    }
    return syscall(SYS_fork); /* Hack for calling real syscall */
}

PS This approach has its downfall. PS 这种方法有其缺点。 At the end of the test it will say your code leaaks), so it's up to you to workaround this.在测试结束时,它会说您的代码泄漏),因此由您来解决此问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们可以在 C++ 中使用 Google Test/Gmock 模拟调用 std::thread 函数的函数吗? - Can we mock the function that calls std::thread function using Google Test/Gmock in C++? 如何在C ++中使用gtest / gmock调用另一个全局函数的地方编写针对全局函数的单元测试? - How to write unit test for global functions where it is calling another global function using gtest/gmock in C++? QTestLib单元测试框架+ Gmock(创建QTCPServer - 模拟对象) - QTestLib unit test framework + Gmock (Create QTCPServer - Mock Object) 使用GMOCK在C ++中模拟新运算符 - Mock new operator in c++ using GMOCK 将c ++单元测试/模拟框架集成到Visual Studio 2012中 - c++ unit test/mock framework integrated into visual studio 2012 用C ++ / Gock模拟中间 - Mock in the Middle with C++/Gmock 如何模拟一个方法(非虚拟)在C ++中使用GMock返回特定值? - How to Mock A Method(non-virtual) To Return Particular Value Using GMock in C++? 如何使用gmock模拟修改C++ class中私有变量的成员函数? - How to mock member functions that modify private variables in a C++ class, using gmock? 如何在运行时使用 GoogleMock 在 C++ 单元测试中模拟系统调用? - How to mock system call in C++ Unit Testing during runtime using GoogleMock? C++ gmock - 我们如何在单元测试 cpp 文件中读取/获取 cpp 文件的函数的参数值 - C++ gmock - How we can read/get the parameter value of a function of cpp file in unit test cpp file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM