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 ...
随机推荐
- Spring框架整合Struts2框架的传统方法
1. 导入CRM项目的UI页面,找到添加客户的页面,修改form表单,访问Action * 将menu.jsp中133行的新增客户的跳转地址改为:href="${pageContext.re ...
- .NET 发送邮件
//邮件配置 public static string mail_smtp = System.Configuration.ConfigurationManager.AppSettings[" ...
- Linux tcpdump命令
一.简介 用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具. tcpdump可以将网络中传送的数据 ...
- LINUX 笔记5
配置环境变量:(只是在当前命令行窗口中有用) 单独查看PATH环境变量,可用: [root@localhost u-boot-sh4]#echo $PATH 添加PATH环境变量,可用: [root@ ...
- Storm 系列(三)Storm 集群部署和配置
Storm 系列(二)Storm 集群部署和配置 本章中主要介绍了 Storm 的部署过程以及相关的配置信息.通过本章内容,帮助读者从零开始搭建一个 Storm 集群. 一.Storm 的依赖组件 1 ...
- UVa 12034 Race (递推+组合数学)
题意:A,B两个人比赛,名次有三种情况(并列第一,AB,BA).输入n,求n个人比赛时最后名次的可能数. 析:本来以为是数学题,排列组合,后来怎么想也不对.原来这是一个递推... 设n个人时答案为f( ...
- Android的方法数超过65535问题
Under the Hood: Dalvik patch for Facebook for Android 先来看一段中文内容 Hack Dalvik VM解决Android 2.3 DEX/Line ...
- java中的static(包括类前面修饰的static、方法前面修饰的static、成员变量前面修饰的static)
static是静态修饰符: 什么叫静态修饰符呢?大家都知道,在程序中任何变量或者代码都是在编译时由系统自动分配内存来存储的,而所谓静态就是指在编译后所分配的内存会一直存在,直到程序退出内存才会释放这个 ...
- wc.java
GitHub代码链接 1.项目相关要求 •基本功能列表: -c 统计文件中字符的个数 -w 统计文件中的词数 -l 统计文件中的行数 •拓展功能: -a 统计文件中代码行数.注释行数.空行 2 ...
- [FMX]将 Android 程序切换到后台及从后台切换到前台实现
有时候,我们需要将自己的Android程序切换到后台运行,在必要时,将其切换到前台运行.下面提供了一种实现方式,首先需要引用三个单元: 1 uses Androidapi.JNI.App,Andr ...