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();
}

運行結果如下圖:

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM