我正在使用gtest和gmock框架进行单元测试,并且需要在类功能内部使用的外部C函数的存根/模拟中获得帮助 [英] I am doing unit testing using gtest and gmock frameworks and I need help in stubbing/mocking a external C functions used inside class functions

查看:106
本文介绍了我正在使用gtest和gmock框架进行单元测试,并且需要在类功能内部使用的外部C函数的存根/模拟中获得帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图为我的生产代码编写测试用例,但是由于使用了一些外部C库,因此覆盖率非常低,如果没有目标硬件就无法执行这些C库,所以我别无选择,只能对它们进行存根处理.现在的问题是如何对C函数存根?

So I am trying to write test cases for my production code but the coverage is drastically low due to the usage of some external C library which cannot be executed without target hardware, So I have no choice but to stub the same. Now the problem is how to stub a C function ?

我的生产代码:prod_code.cpp

My production code : prod_code.cpp

 int TargetTestClass::targetFunc()
 {
    if(externalCFunc() == True)
    {
        statement1; statement2; statement3; /// and so on
    }
 }

我的testcode.cpp通常包含这样的测试

My testcode.cpp generally contains tests like this

//Fixture for Target Test class
class TargetTestClassFixture : public testing::Test {
      TargetTestClass* targetTestClassPtr;
      void SetUp() {
         targetTestClassPtr = new TargetTestClass();
      }
      void TearDown() {
         delete targetTestClassPtr;
      }
  };



TEST_F (unitTest, test_001)
  {
       targetTestClassPtr->targetFunc(); //Need to do something here so that externalCFunc() returns true on call
  }

推荐答案

我找到了2个解决问题的方法,所以我将在此处回答相同的问题.

I found 2 solutions to my problem so I am going to answer the same here.

解决方案1:这涉及更改目标源代码.基本上,您需要编写一个包装程序,以调用如下所示的外部C函数

Solution 1 : This involved changing the target source code. Basically you need to write a wrapper that calls the external C functions like below

Class TargetTestClass{
    protected:
        int targetFunc();
        virtual int externalCFuncWrapper();  // Wrapper
};

//call the external C function from the wrapper
int TargetTestClass::externalCFunctionWrapper(){
    return(externalCFunc());
}

//Definition of targetFuc in original question

//Now write a mock class for Target Test Class as usual and mock the wrapper function to return what you want to 

class MockTargetTestClass : public TargetTestClass{
    public: MOCK_METHOD0(externalCFunctionWrapper, int());
};

//Now use the Mock class as needed
TEST_F ( TargetUnitTest, TestingExternalCFuctionCall)
{
    MockTargetTestClass mockTargetTestClassObj;
    using ::testing::Return;
    using ::testing::_;
    Expect_Call(mockTargetTestClassObj, externalCFunctionWrapper())
    .WillOnce(Return(1));

    Assert_EQ(mockTargetTestClassObj.targetFunc(), 1);
}

解决方案2:感谢@kreynolds,我研究了Fake Function Framework,并实现了以下内容:

Solution 2 : Thanks to @kreynolds, I have looked into Fake Function Framework and implemented as follows :

Class TargetTestClass{
    protected:
        int targetFunc();
        //No Code change needed in target source code
};

//In testcode.cpp 

#include <gmock-global/gmock-global.h>

MOCK_GLOBAL_FUNC0(externalCFunc, int());

TEST( Unittest, test002){
    using ::testing::Return;
    using ::testing::_;
    EXPECT_GLOBAL_CALL(externalCFunc, externalCFunc()).WillOnce(Return(1));

    TargetTestClass targetFunc; //This does not contain any wrapper
    EXPECT_EQ(targetTestClassObj.targetFunc(), 1);
}

我正在使用第二种解决方案,因为这不需要更改我的源代码并且易于使用.

I am using the second solution as this does not require any change in my source code and easier to use.

再次感谢大家给我的时间.

Once again thank you everyone for giving your time.

这篇关于我正在使用gtest和gmock框架进行单元测试,并且需要在类功能内部使用的外部C函数的存根/模拟中获得帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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