Qt單元測試QTestLib
[email protected]
2014年9月12日
1 概述
QT提供内部的單元測試庫,使用友善。
2 目的:使用内部功能進行單元測試。
2.1 功能測試
根據輸入,測試輸出結果。
2.2 GUI功能測試
根據UI事件輸入,并測試輸出結果。
3 原理:輸入測試資料表結果資料表,與實際值比較。
QTest提供了一系統的單元測試功能。
3.1 測試函數:testFun(),testFun_data()
對于一個測試目标函數,需要使用兩個函數進行測試,testFun(),testFun_data()。
testFun_data():資料提供類,在此函數中寫入測試資料。
testFun()是測試的實體類,讀取testFun_data()中的資料表,并逐行進行測試。如果測試結果與資料表中的結果不同,則認為測試失敗。
3.2 測試資料建構:addColumn<T>(name),QTest::newRow(name)<<input<<result
資料由QTest::addColumn<T>(name),QTest::newRow(name)<<input<<result來建構一個資料表,其中的列可以被擷取,然後将表中對應的資料按行測試,并與表中的結果列進行對比。
3.3 GUI測試:事件清單
對于GUI圖形操作的測試,則将資料設定為事件清單,供模拟測試。
3.4 通信
QTest提供一系統宏來進行資料的通信。
3.5 程式啟動入口: QTest_Main(),qExec()
QTest提供了QTest_Main()作為測試的啟動宏,建構一個main函數,也可以直接調用QTest::qExec()來啟動測試。
4 方法:使用QTestLib,将結果資料表加載後逐行與結果資料對比。
QTestLib所有相關功能都在QTest命名空間下。
1) 在PRO檔案中将testlib加入QT參數中。
2) 建立測試類:需要繼承自QObject(因為要用去信号-槽)。
3) 建立測試條目:所有的private slots下函數都将作為測試條目自動測試,并需要一個_data()函數提供資料。
4) 建立測試資料:QTest::addColumn(),QTest::newRow()。
5) 讀取測試資料:QFETCH()
6) 對比測試結果與預期值::QCOMPARE(),QVerify()等。
7) 啟動測試:QTest::qExec()或直接調用QTest_Main()宏。
8) 測試Case啟動、結束事件:initTestCase(),cleanupTestCase()。
9) 測試條目啟動、結束事件:init(),cleanup()。
10) 測試庫:直接測試庫。
11) 測試源檔案:将cpp加入Test工程,測試。
5 示例
///xx.pro
#-------------------------------------------------
#
#ProjectcreatedbyQtCreator2014-09-12T16:06:03
#
#-------------------------------------------------
QT +=coreguitestlib
greaterThan(QT_MAJOR_VERSION,4):QT+=widgets
TARGET=test
TEMPLATE=app
SOURCES+=main.cpp\
mainwindow.cpp\
qtestxxx.cpp
HEADERS +=mainwindow.h\
qtestxxx.h
FORMS +=mainwindow.ui
///xx.h
#ifndefQTESTXXX_H
#defineQTESTXXX_H
#include<QtTest/QTest>
#include<QObject>
classQTestXXX:publicQObject
{
Q_OBJECT
public:
explicitQTestXXX(QObject*parent=0);
signals:
publicslots:
privateslots:
voidtestA();
voidtestA_data();
voidtestE();
voidtestE_data();
privateslots:
virtualvoidinitTestCase();
virtualvoidcleanupTestCase();
virtualvoidinit();
virtualvoidcleanup();
};
#endif//QTESTXXX_H
///xx.cpp
#include"qtestxxx.h"
#include<QtTest/QTest>
QTestXXX::QTestXXX(QObject*parent):
QObject(parent)
{
}
voidQTestXXX::testA()
{
QFETCH(QString,input);
QFETCH(QString,rslt);
QCOMPARE(input,rslt);
}
voidQTestXXX::testA_data()
{
QTest::addColumn<QString>("input");
QTest::addColumn<QString>("rslt");
QTest::newRow("a")<<"abc"<<"abc";
QTest::newRow("b")<<"aBc"<<"abc";
QTest::newRow("c")<<"ABC"<<"abc";
}
voidQTestXXX::initTestCase(){
qDebug("initTestCase\n");
}
voidQTestXXX::cleanupTestCase(){
qDebug("cleanupTestCase\n");
}
voidQTestXXX::init(){
qDebug("init\n");
}
voidQTestXXX::cleanup(){
qDebug("cleanup\n");
}
#include<QLineEdit>
voidQTestXXX::testE()
{
QFETCH(QTestEventList,ev);
QFETCH(QString,rslt);
QLineEditlineed;
ev.simulate(&lineed);
QCOMPARE(lineed.text(),rslt);
}
voidQTestXXX::testE_data()
{
QTest::addColumn<QTestEventList>("ev");
QTest::addColumn<QString>("rslt");
QTestEventListlst1;
lst1.addKeyClick('a');
QTest::newRow("char")<<lst1<<"a";
QTestEventListlst2;
lst2.addKeyClick('b');
lst2.addMouseClick(Qt::LeftButton);
QTest::newRow("clickChar")<<lst2<<"a";
}
QTEST_MAIN(QTestXXX);
5.1 啟動多個測試類
#include"mainwindow.h"
#include<QApplication>
#include<QtTest/QtTest>
#include"maptilecodetrutilitytest.h"
#include"maptilecodetrextenseutilitytest.h"
intmain(intargc,char*argv[])
{
QApplicationa(argc,argv);
MapTileCodeTrUtilityTestut;
QTest::qExec(&ut,argc,argv);
MapTileCodeTrExtenseUtilityTestexut;
QTest::qExec(&exut,argc,argv);
return0;
}