Gtest场景案例之private成员函数如何单元测试独望枫

1.首先构建测试套件,定义一个继承自testing::Test的类

classmodelTest:publictesting::Test{

protected:

};

2.同样需要复写SetUp/TearDown两个函数

voidSetUp()override{

}

voidTearDown()override{

3.定义想要测试单元测试需要用到的数据,并在SetUp进行初始化,在TearDown中进行清理或析构。

pm=newmodel;

if(pm){

deletepm;

pm=NULL;

model*pm;

4.通过TEST_F宏定义想要测试的类的private函数的测试用例

//Testsfactorialofnegativenumbers.

TEST_F(modelTest,Negative){

//Thistestisnamed"Negative",andbelongstothe"FactorialTest"

//testcase.

EXPECT_EQ(1,pm->Factorial(-5));

EXPECT_EQ(1,pm->Factorial(-1));

EXPECT_GT(pm->Factorial(-10),0);

//Testsfactorialof0.

TEST_F(modelTest,Zero){

EXPECT_EQ(1,pm->Factorial(0));

//Testsfactorialofpositivenumbers.

TEST_F(modelTest,Positive){

EXPECT_EQ(1,pm->Factorial(1));

EXPECT_EQ(2,pm->Factorial(2));

EXPECT_EQ(6,pm->Factorial(3));

EXPECT_EQ(40320,pm->Factorial(8));

此时运行的话,系统会报错:

classmodel

{

private:

device*_pdevice;

network*_pnetwork;

public:

model();

~model();

voidprintmodelinfo();

voidprintmodelverion();

voidprintmodeldeviceinfo();

voidprintmodeldeviceversion();

voidprintmodeldeviceserial();

voidprintallinfo();

boolshowImg();

boolshowData();

boolIsPrime(intn);

intFactorial(intn);

#ifdefUT

#defineprotectedpublic

#defineprivatepublic

#endif//UT

简单、直接、粗暴,有效。

但是这个几行代码虽然简单,但如果有很多个类并且没有一个都被包含了的头文件的话,加起来还是有工作量的。那么可以使用一个python脚本来执行这个处理,如果使用到了独立脚本进行处理这个事情,则可以有另一个粗暴的方法:

修改为

//private:

单测运行后再次运行脚本将''//private:''转为''private:''

这样后台将在编译前会进行private的public处理,编译完成后恢复privated的访问权限

附编译前private处理脚本及编译后private还原处理的脚本【请到文末的github连接下载俩脚本】。

但是这种方式,如果在前期没考虑到,后期修改成本较高;而第一第二种方案,虽然没第三种优雅,但其成本较低而且不用修改到源工程的功能代码。可以根据实际情况,按需使用。

THE END
1.gtestNOTE: If you want to build an executable against gtest, but not declare it to be a test on its own (e.g., when you intend the executable to be run by rostest), use catkin_add_executable_with_gtest(). NOTE:Using Ubuntu 11.10 you will get a "undefined reference to `pthread_getspe...http://wiki.ros.org/gtest
2.macportspackagesgtest安装包下载开源镜像站macports-packages-gtest安装包是阿里云官方提供的开源镜像免费下载服务,每天下载量过亿,阿里巴巴开源镜像站为包含macports-packages-gtest安装包的几百个操作系统镜像和依赖包镜像进行免费CDN加速,更新频率高、稳定安全。https://mirrors.aliyun.com/macports/packages/gtest/
3.C++单元测试框架(gtest).PDFC++单元测试框架(gtest).PDF,原创:/coderzh/ 整理: 玩转 Google 开源 C++单元测试框架 Google Test 系列(gtest)之一 - 初识 gtest 玩转 Google 开源 C++单元测试框架 Google Test 系列(gtest)之一 - 初识 gtest 玩玩转转GGooooggllee开开源源 CC++++单单元元测测试试框https://max.book118.com/html/2018/0124/150350830.shtm
1.googletest(gtest)/googlemock(gmock)GTest Runneris a Qt5 based automated test-runner and Graphical User Interface with powerful features for Windows and Linux platforms. GoogleTest UIis a test runner that runs your test binary, allows you to track its progress via a progress bar, and displays a list of test failures. Clicking...https://github.com/google/googletest
2.gtest单元测试论算法工程师如何维护自己的代码这样会造成算法开发人员耗费大量的时间和精力。而gtest就是这样的一个工具,它是Google的一个开源框架,主要功能是用于编写单元测试,从而检查自己的程序是否符合预期行为。这当然是QA(测试工程师)最常用的工具,但是作为一名算法工程师也需要清楚自己的算法是否符合设计需求以及规范。https://blog.csdn.net/lovely_yoshino/article/details/124324445
3.gtest框架版本幸福的地图的技术博客单元测试不必从头开始,目前已经有很多优秀的测试框架可供选择。本文主要介绍google的gtest框架,其github地址:https://github.com/google/googletest gtest安装 从github的release中下载发布版本,目前最新版本为v1.10.0。以在windows下安装为例,Linux下类似。 https://blog.51cto.com/u_14112/12682391
4.GTest问题itest.nz腾讯云开发者社区添加以下代码,注意==不要包含gtest/gtest.h头文件==,若多包含可能会出现struct std::__cxx11::basic_stringbuf<_CharT, _Traits, _Alloc>::__xfer_bufptrs' redeclared with different access错误 代码语言:javascript 复制 #defineprivatepublic#defineprotectedpublic#undefprivate#undefprotected ...https://cloud.tencent.com/developer/article/2157972
5.FindGTest—CMake3.31.0DocumentationThe Google Testgtestlibrary, if found; adds Thread::Thread automatically GTest::gtest_main The Google Testgtest_mainlibrary, if found Added in version 3.23. GTest::gmock The Google Mockgmocklibrary, if found; adds Thread::Thread automatically ...https://cmake.org/cmake/help/latest/module/FindGTest.html
6.GTest的安装与使用【注】在GTest的安装与使用(https://www.cnblogs.com/helloworldcode/p/9606838.html) 的基础上进行了小部分修改补充 下载gtest,release-1.8.0 git clone https://github.com/google/googletest gtest编译 cd googletest 生成Makefile文件(先安装cmake,ubuntu系统:apt install cmake, windows系统:下载cmake-3.18....https://www.jianshu.com/p/96158afbb91d
7.unittestingI'm currently trying to implement the most basic test, to confirm I've compiled/installed gtest right and it's not working. The only source file (testgtest.cpp) is taken almost exactly fromthisprevious answer: #include <iostream> #include "gtest/gtest.h" ...https://stackoverflow.com/questions/8507723/how-to-start-working-with-gtest-and-cmake
8.ubuntu下gtest的调试技巧与常见问题解决云计算设置断点:在需要调试的地方使用断点,可以通过在代码中插入GTEST_BREAK()或者GTEST_BREAK_ON_FAILURE()来设置断点。 使用GDB调试:在运行测试时加上--gtest_break_on_failure参数,可以让GTest在断言失败时调用GDB。 打印调试信息:使用ADD_FAILURE()或ADD_FAILURE_AT()函数在测试中打印调试信息。 https://www.jindouyun.cn/document/cloud/details/341390
9.单元测试框架gtest使用研究gtest是一款由google开发并开源的C++单元测试框架,对C++的各种单元测试场景提供了完备的支持,并且可以在多种平台下运行,本文主要介绍gtest框架中测试部分的内容(mock部分将在下一篇中介绍)。gtest在Github地址为:GitHub - google/googletest: Googletest - Google Testing and Mocking Framework ...https://wsa.jianshu.io/p/8e858e64d359
10.C++使用gtest框架编写单元测试的教程详解C语言gtest 是 Google 开发的一个用于 C++ 的测试框架,广泛应用于编写和运行单元测试,并且支持任何类型的测试,而不仅仅是单元测试。 注意: 本教程使用 cmake 启动并运行 GoogleTest:需提前安装 CMake。 术语:测试(Test)、测试用例(Test Case)和测试套件(Test Suite)。 使用cmake 启动并运行 gtest 1. 设置项目 CMak...https://www.jb51.net/program/325466el1.htm
11.用於HAL測試的參數化GTestAndroidOpenSourceProjectHAL 介面可能會有多種實作方式。如要測試 HAL 實作的每個例項,標準做法是編寫值參數化 GTest。 基本測試設定 GTest 必須繼承基礎類別testing::TestWithParam,其中參數是每個例項的名稱。在SetUp方法中,您可以根據執行個體名稱例項化服務,如以下程式碼片段所示。 https://source.android.google.cn/docs/core/tests/vts/gtest?hl=zh-tw
12.googletestLinux环境安装使用1、准备测试框架gtest1.gtest源码下载:https://github.com/google/googletest2.使用CMake编译出静态库 libgtest.a 步骤: 1...mingw32-make 执行makefile文件编译gtest源码,生成libgtest.a 在新建build目录下,cmd进入命令行终端,执行mingw32-cmake。 2、搭建gtest工程 配置库路径 ...https://www.pianshen.com/article/1893984308/
13.电子邮件中国地质大学邮箱客户端设置指南:本指南以邮件账号cugtest@cug.edu.cn为例。 3、使用Pop协议与使用IMAP协议设置客户端有何区别? 简单地说,使用Pop协议的情况下,客户端只能将"收件箱"里的邮件收取到本地,并且在客户端上对本地邮件的操作均不会影响服务器上的邮件; ...https://wlzx.cug.edu.cn/xxhen/old/fw/dzyj.htm