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
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.
- C++单元测试(Unit Test)中Catch和Gmock的结合使用
- 在Android中同时使用unit test和instrument test作为单元测试
- 在Android中同时使用unit test和instrument test作为单元测试
- 解决“使用google test做单元测试(unit test)中访问类的私有成员”的几种方法
- 单元测试-unit-test的必备的认识
- Spring Boot的单元测试(Unit Test)
- 单元测试技术(Unit Test)
- Golang Unit Test单元测试
- 单元测试 Unit Test Generator
- c语言实现一个单元测试框架(Unit Test Framework)
- c语言实现一个单元测试框架(Unit Test Framework)代码
- 使用 Visual Studio 2005 Team System 进行单元测试并生成用于 Unit Test Framework 的源代码
- 使用 Visual Studio 2005 Team System 进行单元测试并生成用于 Unit Test Framework 的源代码
- 使用 Visual Studio 2005 Team System 进行单元测试并生成用于 Unit Test Framework 的源代码
- 使用 Visual Studio 2005 Team System 进行单元测试并生成用于 Unit Test Framework 的源代码
- 使用 Visual Studio 2005 Team System 进行单元测试并生成用于 Unit Test Framework 的源代码
- 使用 Visual Studio 2005 Team System 进行单元测试并生成用于 Unit Test Framework 的源代码
- 游戏开发中更好的使用Unit Test
- HDU 6201 树形DP
- JFinal上传文件时用getFile()方法报错
- Nginx配置详解
- XML的使用
- 谁知道vimrc中的这条横线是vimrc中的哪一条?看着好别扭
- C++单元测试(Unit Test)中Catch和Gmock的结合使用
- HDU 6194 后缀数组+lcp+rmq
- Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类
- 用python简单实现mysql数据同步到ElasticSearch
- Web单选下拉列表与多选下拉列表的清除
- 如何快速转载CSDN中的博客
- Android 中判断为空的简单语句
- kd树识别压缩有的mnist数据集
- k近邻算法(knn) 学习