使用Qtcreator 自帶的單元測試工具框架QTestlib進行測試。
一.創建一個單元測試程序
new project->other project ->Qt unit test
二.文件列表:
qtestlib/tutorial1/testqstring.cpp
qtestlib/tutorial1/tutorial1.pro
假設我們要測試QString類的行為。首先,需要一個用於包含測試函數的類。這個類必須從QObject繼承:
class TestQString: public QObject
{
Q_OBJECT
private slots:
void toUpper();
};
注意包含QTest頭文件,並且測試函數必須聲明為私有槽,這樣測試框架才可以找到並執行他們。
然后需要實現測試函數。實現看起來類似這樣:
QVERIFY()宏將計算傳入的表達式的值。如果為真,則測試函數繼續進行;否則會向測試日志中增加一條描述錯誤的信息,並且該測試函數會停止執行。
但是如果需要向測試日志中增加更多的輸出信息,你應該使用QCOMPARE()宏:
void TestQString::toUpper()
{
QString str = “Hello”;
QVERIFY(str.toUpper() == “HELLO”);
}
如果兩個字符串不相等,他們的值都會追加到測試日志中,這樣失敗的原因就一目了然了。
最后,為使我們的測試程序能夠單獨執行,需要加入下列兩行:
QTEST_MAIN(TestQString)
#include “testqstring.moc
QTEST_MAIN()宏將擴展成一個簡單的main()函數,該main()函數會執行所有的測試函數。
執行測試程序
運行生成的可執行文件,你會看到下列輸出:
********* Starttesting of TestQString *********
Config: UsingQTest library 4.5.1, Qt 4.5.1
PASS :TestQString::initTestCase()
PASS :TestQString::toUpper()
PASS :TestQString::cleanupTestCase()
Totals: 3 passed,0 failed, 0 skipped
********* Finishedtesting of TestQString *********
三.由於使用Qtestlib進行的測試無法生成代碼覆蓋率,我們需要借助linux 下的代碼覆蓋率工具gcov,lcov , genhtml。
1.gcov 與lcov 簡介
gcov是配合gcc產生覆蓋信息報告的工具;
lcov是將gcov產生的報告信息,以更直觀的方式顯示出來工具
基本的使用方法分為4個階段:
(1)、gcc編譯:產生插裝后的目標文件test、gcov結點文件 test.gcno
#gcc-fprofile-arcs -ftest-coverage-o test test.c
# ls
test test.c test.gcno
說明:參數fprofile-arcs 和ftest-coverage 告訴gcc編譯器:a.在目標文件test 插裝跟蹤代碼;
b.生成供gcov使用test.gcno [gcov node 文件]。
因此,這里的生成的目標文件比正常編譯的文件大。
(2)、運行目標文件:收集運行覆蓋信息 test.gcda
# ./test
Success -- 這里是運行結果。
# ls
test test.c test.gcno test.gcda
這里test.gcda運行結果,
(3)、gcov產生報告信息: test.c.gcov
#gcov test.c
File 'test.c'
Lines executed: 87.50% of 8
test.c: creating 'test.c.gcov'
#ls
test test.c test.c.gcov test.gcdatest.gcno
(4)、lcov:格式化test.c.gcov ,輸出到 test.info文件
#lcov -d . -t 'test' -o 'test.info' -b . -c
說明:
-d . :參數 d指路徑, "." 指當前路徑
-t "name" :指目標文件,這里 是 test
-o "filename" :輸出格式化后的信息文件名
(5)、genhtml:根據信息文件(.info)產生html 文檔,輸出到一個文件夾中
#genhtml -o result test.info
說明: -o directory :參數o (output)后面跟路徑名稱,在當前目錄下創建指定目錄,本例中是result。至此: 可以在result目錄中打開index.html 瀏覽覆蓋信息
四。Qt creator 下具體實現
1.首先確保安裝了gcov(gcc 默認一起安裝)
2.在工程文件pro中添加
QMAKE_CXXFLAGS += -g -Wall -fprofile-arcs -ftest-coverage -O0
QMAKE_LFLAGS += -g -Wall -fprofile-arcs -ftest-coverage -O0
3. 創建processCoverage.sh
#!/bin/bash
##############################################################################
# Copyright (c) 2013, Robert Wloch
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
#
# Contributors:
# Robert Wloch - initial API and implementation
##############################################################################
if [ ! $# -eq 3 ]; then
echo "usage: ${0} <gcov-files-dir> \"<file-pattern>\" <target-dir>"
exit 0
fi
lcov -d ${1} -c -o ${1}/coverage.info
lcov --list-full-path -e ${1}/coverage.info ${2} –o ${1}/coverage-stripped.info
genhtml -o ${3} ${1}/coverage-stripped.info
lcov -d ${1} –z
exit 0
4.Qtcreator左側的project->run->argument添加:
-txt > tlog && (/<path_to_script>/processCoverage.sh <path_to_gcno_files> "*/<name_of_Qt_project_to_test>/src/*" <target_path_for_testcoverage_report> && <browser_executable> <target_path_for_testcoverage_report>/index.html) || (test -f <path_to_UTM>UnitTestMonitor && UnitTestMonitor) &
注:上面<>中的內容替換為與自己工程相應的路徑。
由於mac 下無法安裝lcov ,所以只需要把lcov 工具bin目錄下的二進制文件復制到.gcda文件所在的目錄下。經測試在使用上述方法可以生成代碼覆蓋率,但在使用lcov 最后生成html 文件時出現錯誤。
具體實現可參考:
http://www.robertwloch.net/2013/06/generating-lcov-coverage-with-qtcreator/