• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

QT项目搭建完整的单元测试流程

武飞扬头像
码农飞飞
帮助1

在介绍QT的单元测试框架之前,先说一下单元测试。单元测试最重要的就是要将应用拆分成一个个独立的可测试的函数模块。只有将应用拆分成一个个函数模块之后,应用才是可测的。所以开发领域衍生出来了一个概念,Test-driven development(TDD)测试驱动的开发。将应用拆分成一个个独立的可测试的模块之后,我们就可以针对函数模块进行测试编码了。

针对函数模块的各种可能的调用场景编写测试用例,这样每次我们的代码修改的时候,我们都可以通过测试来验证我们的修改是否会对模块的功能产生影响。这样就相当于给我们的软件添加了一层防护网。编写测试用例很早之前都得是靠自己手写的,这样效率很低,后来出现了一些测试框架能帮助开发者自动化完成这一工作,比较出名的测试框架有Google Test和我们今天介绍的Qt Test。

使用Qt的测试框架,首先新建一个测试工程

学新通

新建完测试工程之后,我们在工程中添加所有单元测试用例用到的基类,基类中包含了所有测试用例用到的通用方法和属性。

  1.  
    //test-suite.h
  2.  
    #ifndef TESTSUITE_H
  3.  
    #define TESTSUITE_H
  4.  
     
  5.  
    #include <QObject>
  6.  
    #include <QString>
  7.  
    #include <QtTest/QtTest>
  8.  
    #include <vector>
  9.  
     
  10.  
    namespace test {
  11.  
     
  12.  
    class TestSuite : public QObject
  13.  
    {
  14.  
    Q_OBJECT
  15.  
    public:
  16.  
    explicit TestSuite(const QString& _testName = "");
  17.  
    virtual ~TestSuite();
  18.  
     
  19.  
    //测试用例的名称
  20.  
    QString testName;
  21.  
    //获取所有实例化的测试用例
  22.  
    static std::vector<TestSuite*>& testList();
  23.  
    };
  24.  
     
  25.  
    }
  26.  
     
  27.  
    #endif
学新通
  1.  
    //test-suite.cpp
  2.  
    #include "test-suite.h"
  3.  
    #include <QDebug>
  4.  
     
  5.  
    namespace test {
  6.  
    TestSuite::TestSuite(const QString& _testName)
  7.  
    : QObject()
  8.  
    , testName(_testName)
  9.  
    {
  10.  
    //只要测试用例创建就会被添加到静态容器中
  11.  
    qDebug() << "create test:" << testName;
  12.  
    testList().push_back(this);
  13.  
    qDebug() << testList().size() << "test added";
  14.  
    }
  15.  
     
  16.  
    TestSuite::~TestSuite()
  17.  
    {
  18.  
    qDebug() << "destory test:" << testName;
  19.  
    }
  20.  
     
  21.  
    std::vector<TestSuite*>& TestSuite::testList()
  22.  
    {
  23.  
    static std::vector<TestSuite*> instance = std::vector<TestSuite*>();
  24.  
    return instance;
  25.  
    }
  26.  
    }
学新通

通过基类的方法和成员变量,我们就可以获取每个测试用例的名称,以及所有实例化的测试用例的列表了。默认的测试工程是没有添加程序的入口函数的,需要手动添加,程序的入口函数如下,在程序入口中我们执行所有的测试用例,并将测试结果输出到xml文件中,为后面的分析和使用做准备。

  1.  
    //main.cpp
  2.  
    #include <QtTest/QtTest>
  3.  
    #include <QDebug>
  4.  
    #include "test-suite.h"
  5.  
    #include "demotest.h"
  6.  
     
  7.  
    using namespace test;
  8.  
    int main(int argc, char *argv[])
  9.  
    {
  10.  
    Q_UNUSED(argc);
  11.  
    Q_UNUSED(argv);
  12.  
     
  13.  
    qDebug() << "found:" << TestSuite::testList().size() << "test suite";
  14.  
     
  15.  
    //记录测试失败的数量
  16.  
    int failedTestsCount = 0;
  17.  
    for(TestSuite* i : TestSuite::testList())
  18.  
    {
  19.  
    qDebug() << "Executing test " << i->testName;
  20.  
     
  21.  
    //执行每个测试
  22.  
    QString filename(i->testName ".xml");
  23.  
     
  24.  
    //执行每个测试并将测试结果输出到xml文件中
  25.  
    int result = QTest::qExec(i, QStringList() << " " << "-o" << filename << "-xunitxml");
  26.  
    qDebug() << "Test result " << result;
  27.  
    if(result != 0)
  28.  
    {
  29.  
    failedTestsCount ;
  30.  
    }
  31.  
    }
  32.  
     
  33.  
    qDebug() << "Test suite complete:" << failedTestsCount << "failures detected.";
  34.  
     
  35.  
    return failedTestsCount;
  36.  
    }
学新通

QTest::qExec支持很多配置参数,这里选几个常用的介绍一下,有需要更加详细的可以去查询官方文档。

  1.  
    -o filename format 以指定的格式将测试结果输出到某个文件
  2.  
    -o filename 将测试结果输出到某个文件
  3.  
    -txt 以纯文本的形式输出测试信息
  4.  
    -xml 以XML的形式输出测试信息
  5.  
    -lightxml 以轻量级xml的形式输出测试信息
  6.  
    -xunitxml 将测试结果输出成xml文档
  7.  
    -csv 以CSV的格式输出测试信息
  8.  
    -teamcity 以TeamCity格式输出结果

搭建完成测试用例执行的框架之后,我们就可以为每一个单元模块添加测试了,这里以一个简单的模块为例介绍一下测试用例的用法。

这个模块主要有两个方面的功能

1.求两个整数的和

2.求时间的各种表示格式

  1.  
    //demo.h
  2.  
    #ifndef DEMO_H
  3.  
    #define DEMO_H
  4.  
    #include <QObject>
  5.  
    #include <QDateTime>
  6.  
     
  7.  
    class Demo : public QObject
  8.  
    {
  9.  
    Q_OBJECT
  10.  
    public:
  11.  
    Demo(const QDateTime& time);
  12.  
    Demo();
  13.  
     
  14.  
    public:
  15.  
    int addTwoNumber(int number1,int number2);
  16.  
    QString toIso8601String() const;
  17.  
    QString toPrettyDateString() const;
  18.  
    QString toPrettyTimeString() const;
  19.  
    QString toPrettyString() const;
  20.  
    private:
  21.  
    QDateTime datetime;
  22.  
    signals:
  23.  
    void inputtwozero();
  24.  
    };
  25.  
     
  26.  
    #endif // DEMO_H
学新通
  1.  
    //demo.cpp
  2.  
    #include "demo.h"
  3.  
    #include <QLocale>
  4.  
     
  5.  
    Demo::Demo(const QDateTime &time):
  6.  
    QObject(),
  7.  
    datetime(time)
  8.  
    {}
  9.  
     
  10.  
    Demo::Demo()
  11.  
    {
  12.  
    }
  13.  
    int Demo::addTwoNumber(int number1, int number2)
  14.  
    {
  15.  
    if((number1 == 0) && (number2 == 0))
  16.  
    {
  17.  
    emit inputtwozero();
  18.  
    }
  19.  
    return number1 number2;
  20.  
    }
  21.  
     
  22.  
    QString Demo::toIso8601String() const
  23.  
    {
  24.  
    if (datetime.isNull()) {
  25.  
    return "";
  26.  
    } else {
  27.  
    return datetime.toString(Qt::ISODate);
  28.  
    }
  29.  
    }
  30.  
     
  31.  
    QString Demo::toPrettyString() const
  32.  
    {
  33.  
    if (datetime.isNull()) {
  34.  
    return "Not set";
  35.  
    } else {
  36.  
    QLocale locale = QLocale::English;
  37.  
    return locale.toString(datetime,"ddd d MMM yyyy @ HH:mm:ss");
  38.  
    }
  39.  
    }
  40.  
     
  41.  
    QString Demo::toPrettyDateString() const
  42.  
    {
  43.  
    if (datetime.isNull()) {
  44.  
    return "Not set";
  45.  
    } else {
  46.  
    QLocale locale = QLocale::English;
  47.  
    return locale.toString(datetime,"d MMM yyyy");
  48.  
    }
  49.  
    }
  50.  
     
  51.  
    QString Demo::toPrettyTimeString() const
  52.  
    {
  53.  
    if (datetime.isNull()) {
  54.  
    return "Not set";
  55.  
    } else {
  56.  
    QLocale locale = QLocale::English;
  57.  
    return locale.toString(datetime,"hh:mm ap");
  58.  
    }
  59.  
    }
学新通

在测试工程中为单元模块添加测试用例,测试用例添加有几点需要了解。

1.测试用例的初始化函数和清理函数

测试用例中有几个被预留的函数,负责初始化测试用例以及测试用例执行完之后的清理操作。他们分别是:

  1.  
    /// 在第一个测试函数执行之前被调用的初始化函数
  2.  
    void initTestCase();
  3.  
    /// 在最后一个测试函数执行完毕之后调用的清理操作
  4.  
    void cleanupTestCase();
  5.  
     
  6.  
    /// 每次执行测试函数之前调用的初始化函数
  7.  
    void init();
  8.  
    /// 每次执行完测试函数之后调用的清理操作
  9.  
    void cleanup();

2.记录某个信号触发的次数

在测试工程中,有时候我们需要验证某个信号是否触发,触发了多少次,这时候我们需要使用QSignalSpy类来记录信号触发的次数。

3.在测试用例的实现文件中添加静态的全局变量,这样在构建工程的时候测试用例就会自动添加到测试用例列表。

测试用例的实现如下:

  1.  
    //demotest.h
  2.  
    #ifndef DEMOTEST_H
  3.  
    #define DEMOTEST_H
  4.  
    #include <QtTest/QtTest>
  5.  
    #include "test-suite.h"
  6.  
     
  7.  
    namespace test {
  8.  
    class DemoTest : public TestSuite
  9.  
    {
  10.  
    Q_OBJECT
  11.  
    public:
  12.  
    DemoTest();
  13.  
     
  14.  
    private slots:
  15.  
    /// @brief 第一个测试函数执行之前被调用
  16.  
    void initTestCase();
  17.  
    /// @brief 最后一个测试函数执行之后被调用
  18.  
    void cleanupTestCase();
  19.  
    /// @brief 每个测试函数执行之前被调用
  20.  
    void init();
  21.  
    /// @brief 每个测试函数执行之后被调用
  22.  
    void cleanup();
  23.  
     
  24.  
    //测试加法运算
  25.  
    void addTwoNumber_twoPositiveNum_returnInt();
  26.  
    void addTwoNumber_twoNegative_returnInt();
  27.  
    void addTwoNumber_nagativeandpostive_returnInt();
  28.  
    void addTwoNumber_twozero_returnInt();
  29.  
     
  30.  
    //toIso8601String 接口测试(缺省值和外部设置值)
  31.  
    void toIso8601String_whenDefaultValue_returnsString();
  32.  
    void toIso8601String_whenValueSet_returnsString();
  33.  
     
  34.  
    //toPrettyDateString 接口测试(缺省值和外部设置值)
  35.  
    void toPrettyDateString_whenDefaultValue_returnsString();
  36.  
    void toPrettyDateString_whenValueSet_returnsString();
  37.  
     
  38.  
    //toPrettyTimeString 接口测试(缺省值和外部设置值)
  39.  
    void toPrettyTimeString_whenDefaultValue_returnsString();
  40.  
    void toPrettyTimeString_whenValueSet_returnsString();
  41.  
     
  42.  
    //toPrettyTimeString 接口测试(缺省值和外部设置值)
  43.  
    void toPrettyString_whenDefaultValue_returnsString();
  44.  
    void toPrettyString_whenValueSet_returnsString();
  45.  
     
  46.  
    private:
  47.  
    //测试数据
  48.  
    QDateTime testDate{QDate(2022, 6, 4), QTime(16, 40, 32)};
  49.  
    };
  50.  
     
  51.  
    }
  52.  
    #endif // DEMOTEST_H
学新通
  1.  
    //demotest.cpp
  2.  
    #include "demotest.h"
  3.  
    #include <QDebug>
  4.  
    #include "demo.h"
  5.  
     
  6.  
    namespace test {
  7.  
     
  8.  
    //实例化静态变量自动添加到列表中
  9.  
    static DemoTest instance;
  10.  
     
  11.  
    DemoTest::DemoTest():TestSuite("demoTest")
  12.  
    {
  13.  
    }
  14.  
     
  15.  
    void DemoTest::initTestCase()
  16.  
    {
  17.  
    }
  18.  
     
  19.  
    void DemoTest::cleanupTestCase()
  20.  
    {
  21.  
    }
  22.  
     
  23.  
    void DemoTest::init()
  24.  
    {
  25.  
    }
  26.  
     
  27.  
    void DemoTest::cleanup()
  28.  
    {
  29.  
    }
  30.  
     
  31.  
    void DemoTest::addTwoNumber_twoPositiveNum_returnInt()
  32.  
    {
  33.  
    Demo demo;
  34.  
    QCOMPARE(demo.addTwoNumber(1955,1932), 3887);
  35.  
    }
  36.  
     
  37.  
    void DemoTest::addTwoNumber_twoNegative_returnInt()
  38.  
    {
  39.  
    Demo demo;
  40.  
    QCOMPARE(demo.addTwoNumber(-1955,-1932), -3887);
  41.  
    }
  42.  
     
  43.  
    void DemoTest::addTwoNumber_nagativeandpostive_returnInt()
  44.  
    {
  45.  
    Demo demo;
  46.  
    QCOMPARE(demo.addTwoNumber(-1955,1932), -23);
  47.  
    }
  48.  
     
  49.  
    void DemoTest::addTwoNumber_twozero_returnInt()
  50.  
    {
  51.  
    Demo demo;
  52.  
    QSignalSpy valueChangedSpy(&demo, &Demo::inputtwozero);
  53.  
    QCOMPARE(demo.addTwoNumber(0,0), 0);
  54.  
    QCOMPARE(valueChangedSpy.count(), 1);
  55.  
    }
  56.  
     
  57.  
    void DemoTest::toIso8601String_whenDefaultValue_returnsString()
  58.  
    {
  59.  
    Demo demo;
  60.  
    QCOMPARE(demo.toIso8601String(), QString(""));
  61.  
    }
  62.  
     
  63.  
    void DemoTest::toIso8601String_whenValueSet_returnsString()
  64.  
    {
  65.  
    Demo demo(testDate);
  66.  
    QString string_value = demo.toIso8601String();
  67.  
    QCOMPARE(demo.toIso8601String(), QString("2022-06-04T16:40:32"));
  68.  
    }
  69.  
     
  70.  
    void DemoTest::toPrettyDateString_whenDefaultValue_returnsString()
  71.  
    {
  72.  
    Demo demo;
  73.  
    QCOMPARE(demo.toPrettyDateString(), QString("Not set"));
  74.  
    }
  75.  
     
  76.  
    void DemoTest::toPrettyDateString_whenValueSet_returnsString()
  77.  
    {
  78.  
    Demo demo(testDate);
  79.  
    QCOMPARE(demo.toPrettyDateString(), QString("4 Jun 2022"));
  80.  
    }
  81.  
     
  82.  
    void DemoTest::toPrettyTimeString_whenDefaultValue_returnsString()
  83.  
    {
  84.  
    Demo demo;
  85.  
    QCOMPARE(demo.toPrettyTimeString(), QString("Not set"));
  86.  
    }
  87.  
     
  88.  
    void DemoTest::toPrettyTimeString_whenValueSet_returnsString()
  89.  
    {
  90.  
    Demo demo(testDate);
  91.  
    QString pm = demo.toPrettyTimeString();
  92.  
    QCOMPARE(demo.toPrettyTimeString(), QString("04:40 pm"));
  93.  
    }
  94.  
     
  95.  
    void DemoTest::toPrettyString_whenDefaultValue_returnsString()
  96.  
    {
  97.  
    Demo demo;
  98.  
    QCOMPARE(demo.toPrettyString(), QString("Not set"));
  99.  
    }
  100.  
     
  101.  
    void DemoTest::toPrettyString_whenValueSet_returnsString()
  102.  
    {
  103.  
    Demo demo(testDate);
  104.  
    QCOMPARE(demo.toPrettyString(), QString("周六 4 6月 2022 @ 16:40:32"));
  105.  
    }
  106.  
    }
学新通

添加完测试用例之后,执行测试工程,我们就可以在输出目录里面查看测试结果了,通过测试结果,我们就可以知道哪个测试成功,哪个测试失败了。测试文件数据格式如下:

  1.  
    <?xml version="1.0" encoding="UTF-8" ?>
  2.  
    <testsuite errors="0" failures="1" tests="14" name="test::DemoTest">
  3.  
    <properties>
  4.  
    <property value="5.9.0" name="QTestVersion"/>
  5.  
    <property value="5.9.0" name="QtVersion"/>
  6.  
    <property value="Qt 5.9.0 (i386&#x002D;little_endian&#x002D;ilp32 shared (dynamic) debug build; by MSVC 2015)" name="QtBuild"/>
  7.  
    </properties>
  8.  
    <testcase result="pass" name="initTestCase"/>
  9.  
    <testcase result="pass" name="addTwoNumber_twoPositiveNum_returnInt"/>
  10.  
    <testcase result="pass" name="addTwoNumber_twoNegative_returnInt"/>
  11.  
    <testcase result="pass" name="addTwoNumber_nagativeandpostive_returnInt"/>
  12.  
    <testcase result="pass" name="addTwoNumber_twozero_returnInt"/>
  13.  
    <testcase result="pass" name="toIso8601String_whenDefaultValue_returnsString"/>
  14.  
    <testcase result="pass" name="toIso8601String_whenValueSet_returnsString"/>
  15.  
    <testcase result="pass" name="toPrettyDateString_whenDefaultValue_returnsString"/>
  16.  
    <testcase result="pass" name="toPrettyDateString_whenValueSet_returnsString"/>
  17.  
    <testcase result="pass" name="toPrettyTimeString_whenDefaultValue_returnsString"/>
  18.  
    <testcase result="pass" name="toPrettyTimeString_whenValueSet_returnsString"/>
  19.  
    <testcase result="pass" name="toPrettyString_whenDefaultValue_returnsString"/>
  20.  
    <testcase result="fail" name="toPrettyString_whenValueSet_returnsString">
  21.  
    <failure message="Compared values are not the same
  22.  
    Actual (demo.toPrettyString()) : &quot;Sat 4 Jun 2022 @ 16:40:32&quot;
  23.  
    Expected (QString(&quot;周六 4 6月 2022 @ 16:40:32&quot;)): &quot;\uFFFD\uFFFD\uFFFD\uFFFD 4 6\uFFFD\uFFFD 2022 @ 16:40:32&quot;" result="fail"/>
  24.  
    </testcase>
  25.  
    <testcase result="pass" name="cleanupTestCase"/>
  26.  
    <system-err/>
  27.  
    </testsuite>
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfhbeeb
系列文章
更多 icon
同类精品
更多 icon
继续加载