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

代码如下:

 1 #-------------------------------------------------
 2 #
 3 # Project created by QtCreator 2019-04-24T17:05:02
 4 #
 5 #-------------------------------------------------
 6 
 7 QT       -= gui
 8 
 9 TARGET = gtest
10 TEMPLATE = lib
11 CONFIG += staticlib
12 INCLUDEPATH +=./include
13 
14 # The following define makes your compiler emit warnings if you use
15 # any feature of Qt which as been marked as deprecated (the exact warnings
16 # depend on your compiler). Please consult the documentation of the
17 # deprecated API in order to know how to port your code away from it.
18 DEFINES += QT_DEPRECATED_WARNINGS
19 
20 # You can also make your code fail to compile if you use deprecated APIs.
21 # In order to do so, uncomment the following line.
22 # You can also select to disable deprecated APIs only up to a certain version of Qt.
23 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
24 
25 SOURCES += src/gtest.cc\
26                 src/gtest_main.cc\
27                 src/gtest-all.cc\
28                 src/gtest-death-test.cc\
29                 src/gtest-filepath.cc\
30                 src/gtest-port.cc\
31                 src/gtest-printers.cc\
32                 src/gtest-test-part.cc\
33                 src/gtest-typed-test.cc
34 
35 HEADERS += qtlib.h
36 unix {
37     target.path = /usr/lib
38     INSTALLS += target
39 }

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++11

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.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 = 1;
    for (int i = 1; i <= n; i++) {
        result *= i;
    }

    return result;
}
bool IsPrime(int n)
{
  if (n <= 1) return false;
  if (n % 2 == 0) return n == 2;
  for (int i = 3; ; i += 2)
  {
    if (i > n/i) break;
       if (n % i == 0) return false;
  }
    return true;
}


TEST(zhengshu_Test,zhengshu)
{
    EXPECT_EQ(2, Factorial(2));             //EXPECT_EQ(expected,actual)与EXPECT_TRUE((expected) == (actual))等同,
    EXPECT_EQ(1,Factorial(0));
    EXPECT_EQ(1, Factorial(1));             //当EXPECT_EQ(expected,actual)失败时会打印出期望的值与实际的值
    EXPECT_EQ(6, Factorial(3));             //但EXPECT_TRUE可以接受任何类型的布尔表达式
    EXPECT_EQ(2, Factorial(8));             //eg:falied case
}
TEST(IsPrimeTest, Trivial) {
    EXPECT_FALSE(IsPrime(5));                //eg:falied case
    EXPECT_FALSE(IsPrime(1));
    EXPECT_TRUE(IsPrime(2));
    EXPECT_TRUE(IsPrime(3));
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    testing::InitGoogleTest(&argc,argv);
    return RUN_ALL_TESTS();
}

运行结果如下图:



猜你喜欢

转载自www.cnblogs.com/lw-five/p/10770636.html