Qt使用gtest进行C++单元测试-01
环境: win7/win10+qt5.8.0(MinGW),
1.gtest获取: 从:https://www.bogotobogo.com/cplusplus/google_unit_test_gtest.php
获取gtest-1.7.0-rc1.zip,下载链接,下载打包的源码
或在git仓库下载: git clone https://github.com/google/googletest.git
2.解压gtest-1.7.0;在Qt创建一个静态C++库项目,将生成的Qt project文件拷贝到gtest-1.7.0目录下,命名为:gtest.pro;
3.编辑gtest.pro:主要添加源文件(SOURCES)信息和包含信息如下:
SOURCES += src/gtest_main.cc\
src/gtest.cc\
......
(所有src目录下所有源文件)
INCLUDEPATH += ./include
TEMPLATE +=lib
代码如下:
#-------------------------------------------------
#
# Project created by QtCreator --24T17::
#
#------------------------------------------------- QT -= gui TARGET = gtest
TEMPLATE = lib
CONFIG += staticlib
INCLUDEPATH +=./include # The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0. SOURCES += src/gtest.cc\
src/gtest_main.cc\
src/gtest-all.cc\
src/gtest-death-test.cc\
src/gtest-filepath.cc\
src/gtest-port.cc\
src/gtest-printers.cc\
src/gtest-test-part.cc\
src/gtest-typed-test.cc HEADERS += qtlib.h
unix {
target.path = /usr/lib
INSTALLS += target
}
4.使用Qt打开gtest.pro工程, 进行构建, Qt会在.pro的上一级目录下生成对应的编译目录和输出目录, 如下图:

在输出目录下, 可以看到MinGW编译出的gtest库文件libgtest.a和main.o,如下图:

5.编译得到想要的gtest库后, 开始使用在Qt环境下使用:
使用Qt新建一个console的验证工程gtestforqt;
将gtest-1.7.0\include路径下的文件夹gtest拷贝到验证工程的gtestforqt文件夹下,同时将MinGW编译出的gtest库文件libgtest.a也拷贝到gtestforqt文件夹;
编辑gtestforqt.pro文件, 使其可以连接到我们编译的gtest库文件, 代码如下:
QT += core
QT -= gui CONFIG += c++ INCLUDEPATH += ..\gtestforqt\include //增加对gtest头文件的链接路径
LIBS += ..\gtestforqt\libgtest.a //是增加对gtest库文件的链接路径 TARGET = gtestforqt
CONFIG += console
CONFIG -= app_bundle TEMPLATE = app SOURCES += main.cpp # The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.
编辑源文件(main.cpp),include gtest的文件,并初始化gtest, 代码如下:
#include <QCoreApplication>
#include "gtest\gtest.h" int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
testing::InitGoogleTest(&argc,argv);
return RUN_ALL_TESTS();
/*在编译的时候会链接src/gtest_main.cc 文件,这个文件包含了
main()函数,在main()函数里调用RUN_ALL_TESTS(), 而此函数会调用我们所定义
的所有TEST()函数,并打印运行结果,返回值为0表示成功,为1表示失败。*/
构建运行后, gtest成功执行;
6.简单的单元测试举例如下:
#include <QCoreApplication>
#include "gtest\gtest.h"
using namespace std; int Factorial(int n) {
int result = ;
for (int i = ; i <= n; i++) {
result *= i;
} return result;
}
bool IsPrime(int n)
{
if (n <= ) return false;
if (n % == ) return n == ;
for (int i = ; ; i += )
{
if (i > n/i) break;
if (n % i == ) return false;
}
return true;
} TEST(zhengshu_Test,zhengshu)
{
EXPECT_EQ(, Factorial()); //EXPECT_EQ(expected,actual)与EXPECT_TRUE((expected) == (actual))等同,
EXPECT_EQ(,Factorial());
EXPECT_EQ(, Factorial()); //当EXPECT_EQ(expected,actual)失败时会打印出期望的值与实际的值
EXPECT_EQ(, Factorial()); //但EXPECT_TRUE可以接受任何类型的布尔表达式
EXPECT_EQ(, Factorial()); //eg:falied case
}
TEST(IsPrimeTest, Trivial) {
EXPECT_FALSE(IsPrime()); //eg:falied case
EXPECT_FALSE(IsPrime());
EXPECT_TRUE(IsPrime());
EXPECT_TRUE(IsPrime());
} int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
testing::InitGoogleTest(&argc,argv);
return RUN_ALL_TESTS();
}
运行结果如下图:

Qt使用gtest进行C++单元测试-01的更多相关文章
- Qt Creator 源码学习笔记01,初识QTC
阅读本文大概需要 4 分钟 Qt Creator 是一款开源的轻量级 IDE,整个架构代码全部使用 C++/Qt 开发而成,非常适合用来学习C++和Qt 知识,这也是我们更加深入学习Qt最好的方式,学 ...
- Junit单元测试--01
如何编写单元测试 燕双龙 一 单元测试简介 单元测试是代码正确性验证的最重要的工具,也是系统测试当中最重要的环节.也是唯一需要编写代码才能进行测试的一种测试方法.在标准的开发过程中,单元测试的代码与实 ...
- C++单元测试 之 gtest -- 组合数计算.
本文将介绍如何使用gtest进行单元测试. gtest是google单元测试框架.使用非常方便. 首先,下载gtest (有些google项目包含gtest,如 protobuf),复制目录即可使用. ...
- Qt高级——QTestLib单元测试框架
一.QTestLib简介 1.QTestLib简介 QTestLib是Qt提供的一种针对基于Qt编写的程序或库的单元测试框架.QTestLib提供了单元测试框架的基本功能,并提供了针对GUI测试的扩展 ...
- 白盒测试之gtest第一个demo
认识gtest工具后,关于它的使用,下面将用一个demo程序演示一下gtest的用法以及成果展示. 一.需要测试的C++代码: #include "myfunction.h" // ...
- Qt 5.7 > Qt Applications
本文翻译自Qt官方文档: http://doc.qt.io/qt-5/qmlapplications.html QML 应用 QML是声明式语言,它使得用户界面以及交互行为可以被"描述&qu ...
- 使用 ViS2005 进行单元测试
1. 新建一个空白解决方案,命名为"单元测试- 01"吧. 2.在该解决方案下创建一个类库,作为此次单元测试的测试对象:我们就创建一个数学类(用于实现运算的简单类).命名为&quo ...
- Qt Creator 源码学习笔记04,多插件实现原理分析
阅读本文大概需要 8 分钟 插件听上去很高大上,实际上就是一个个动态库,动态库在不同平台下后缀名不一样,比如在 Windows下以.dll结尾,Linux 下以.so结尾 开发插件其实就是开发一个动态 ...
- TFS(Taobao File System)安装方法
文章目录: 一.TFS(Taobao File System)安装方法 二.TFS(Taobao File System)配置dataServer.分区.挂载数据盘 三.TFS(Taobao File ...
随机推荐
- program by the way......
ostrich birds fruit apple constructor height weight method overload override base sub inherit extend ...
- linux下安装php php-fpm(转载)
centos安装php php-fpm 1.下载php源码包http://www.php.net/downloads.php2 .安装phptar -xvf php-5.5.13.tar.bz2cd ...
- 201621123008 《Java程序设计》第一周学习总结
1. 本章学习总结 对于我们学计算机的学生而言,要想提高编程能力,只有多练习,把我们所学到的东西运用到实践中去,整天抱着书本冥思苦想而不动手到具体的环境中去试验是很难有所提升的.大一学C语言的时候平时 ...
- 用 npm 安装删除模块
npm安装模块 [npm install xxx]利用 npm 安装xxx模块到当前命令行所在目录: [npm install -g xxx]利用npm安装全局模块xxx: 本地安装时将模块写入pac ...
- 4. Configure maven in Spring Tool Suite
First of all, you need to copy the folder named like: Choose Window->Preferences->Maven->Us ...
- distinct group by 去重查询
select * from dc_restaurants; 31 select DISTINCT (restaurant_name),id from dc_restaurants ; 31 (会按照 ...
- java bulid path 和 WEB-INF/lib 下jar 包区别
用Java Build Path导入包和把包复制到lib下是有区别的,它俩其实不会冲突,也没有什么关系的, Java Build Path是我们编译需要的包, 导入到lib下是程序运行时需要的包 , ...
- 学习bn算法
好处: 1.归一化后有什么好处呢?原因在于神经网络学习过程本质就是为了学习数据分布,一旦训练数据与测试数据的分布不同,那么网络的泛化能力也大大降低: 2.另外一方面,一旦每批训练数据的分布各不相同(b ...
- 2018.07.22 洛谷P4316 绿豆蛙的归宿(概率dp)
传送门 简单的递推. 由于是DAG" role="presentation" style="position: relative;">DAGDA ...
- LA 3942 && UVa 1401 Remember the Word (Trie + DP)
题意:给你一个由s个不同单词组成的字典和一个长字符串L,让你把这个长字符串分解成若干个单词连接(单词是可以重复使用的),求有多少种.(算法入门训练指南-P209) 析:我个去,一看这不是一个DP吗?刚 ...