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

来源:互联网 发布:红叶知弦小说 编辑:程序博客网 时间:2024/06/06 09:44

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.

阅读全文
0 0