C++单元测试(Unit Test)中Catch和Gmock的结合使用

本文介绍如何在Visual Studio 2013中将C++单元测试框架Catch与Google的Mock框架(GMock)集成使用。通过编写自定义的main函数,并在测试代码中引入GMock,演示了如何为一个简单的消息传递类创建Mock对象并进行测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Tutorial of Catch

Chinese version:
http://blog.guorongfei.com/2016/08/22/cpp-unit-test-catch/
Official(recommand, very clear and useful):
https://github.com/philsquared/Catch/blob/master/docs/Readme.md

2. Integrate Catch with Gmock in VS2013

In Short: Write custom main function and involve Gmock code in testcase of Catch in test code.

1. Structure

Visual Studio2013


2. Code

1. hello_world.h
#pragma once
#include "messager.h"

class HelloWorld
{
public:
    HelloWorld();
    virtual ~HelloWorld();
    string getMessage(Messager *messager) const;
};
2. messager.h

Class to be mocked.

#pragma once
#include <string>
using namespace std;

class Messager
{
public:
    virtual ~Messager() {}
    virtual string getMessage() = 0;
};
3. mock_messager.h

Mock Messager class.

#pragma once
#include "messager.h"

class MockMessager :public Messager
{
public:
    MOCK_METHOD0(getMessage, string());
};
4. hello_world.cpp

It invokes getMessage() in Messager.

#include "hello_world.h"
#include "messager.h"

HelloWorld::HelloWorld(){}

HelloWorld::~HelloWorld(){}

string HelloWorld::getMessage(Messager *messager) const
{
    return messager->getMessage();
}
5. Test.cpp

For using Gmock, we need to write our own main function to involve
::testing::GTEST_FLAG(throw_on_failure) = true;
::testing::InitGoogleMock(&argc, argv);

Reference:
1. Gmock: Using Google Mock with Any Testing Framework
2. Catch: Supplying your own main()

//Test.cpp
// write your own main function
#define CATCH_CONFIG_RUNNER
#include "catch.hpp"
#include "gmock/gmock.h"
#include "mock_messager.h"
#include "hello_world.h"
#include <String>
using namespace testing;

TEST_CASE("Test Gmock", "[MockMessager]") {
    MockMessager messager;
    std::string msg = "Hello World";
    EXPECT_CALL(messager, getMessage()).WillRepeatedly(Return(ByRef(msg)));
    HelloWorld helloWorld;

    SECTION("HandleGetMessage") {
        //CAPTURE(helloWorld.getMessage(&messager));
        REQUIRE("Hello Worl" == helloWorld.getMessage(&messager));
    }
}

int main(int argc, char* argv[])
{
    // The following line causes Google Mock to throw an exception on failure,
    // which will be interpreted by your testing framework as a test failure.
    ::testing::GTEST_FLAG(throw_on_failure) = true;
    ::testing::InitGoogleMock(&argc, argv);
    // global setup...
    int result = Catch::Session().run(argc, argv);
    // global clean-up...
    system("pause");
    return (result < 0xff ? result : 0xff);
}
6. Result

I intentionally misspell the required word “hello_worl” for showing the fail report.
这里写图片描述


3. Problem

Integrate Catch and Gmock will cause the conflicts of two macros FAIL and SUCCEED. Thank god, these two macros are not necessary, just don't use them :) and can be realized using other macros.
1. FAIL
A macro for log information in Catch. FAIL(message expression), test will fail, if the expression is true.
2. SUCCEED
Not in the tutorial. It’s also a macro for log information.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值