Qt 单元测试

来源:互联网 发布:soap php 编辑:程序博客网 时间:2024/06/03 12:38

使用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/
0 0