The first example comes from Qt Creator Help Document: Qt 5.11 > Qt Test

创建简单的Unit test。

新建unit test工程

private Q_SLOTS中写入自己的测试函数:
cpp文件

#include <QString>
#include <QtTest>

class String : public QObject
{
Q_OBJECT

public:
String();

private Q_SLOTS:
void toUpper();
};

String::String()
{
}

void String::toUpper()
{
QVERIFY2(true, "Failure");
}

QTEST_APPLESS_MAIN(String)
#include "tst_string.moc"            // compiled from tst_string.cpp by Meta-Object Compiler

pro配置:

QT += testlib
TARGET = tst_string
CONFIG += console
CONFIG -= app_bundle

SOURCES += \
tst_string.cpp

DEFINES += SRCDIR=\\\"$$PWD/\\\"

其中 QTEST_APPLESS_MAIN(String) 会创建main函数来运行我们的测试函数。之后直接

qmake
make
./tst_string

即可。

********* Start testing of String *********
Config: Using QtTest library 5.9.2, Qt 5.9.2 (x86_64-little_endian-lp64 shared (dynamic) release build; by Clang 7.0.2 (clang-700.1.81) (Apple))
PASS : String::initTestCase()
PASS : String::toUpper()
PASS : String::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped, 0 blacklisted, 1ms
********* Finished testing of String *********

数据驱动测试

增添private slot void toUpper_data()

void TestQString::toUpper_data()
{
QTest::addColumn<QString>("string");
QTest::addColumn<QString>("result");

QTest::newRow("all lower") << "hello" << "HELLO";
QTest::newRow("mixed") << "Hello" << "HELLO";
QTest::newRow("all upper") << "HELLO" << "HELLO";
}

修改测试函数:

void TestQString::toUpper()
{
QFETCH(QString, string);    // take data from table
QFETCH(QString, result);

QCOMPARE(string.toUpper(), result);
}

运行结果:

********* Start testing of String *********
Config: Using QtTest library 5.11.2, Qt 5.11.2 (x86_64-little_endian-lp64 shared (dynamic) release build; by Clang 8.1.0 (clang-802.0.42) (Apple))
PASS : String::initTestCase()
PASS : String::toUpper(all lower)
PASS : String::toUpper(mixed)
PASS : String::toUpper(all upper)
PASS : String::cleanupTestCase()
Totals: 5 passed, 0 failed, 0 skipped, 0 blacklisted, 2ms
********* Finished testing of String *********

code link:
https://github.com/theArcticOcean/qtLib/tree/master/StringTest

需要注意的是,数据准备的函数,命名一定要正确。(以_data结尾)

另一个例子

同样,我们可以测试四则运算 – 加法。

#include <QtTest>
#include <QDebug>

// add necessary includes here

class PlusTest : public QObject
{
    Q_OBJECT

public:
    PlusTest();
    ~PlusTest();

private slots:
    void initTestCase();
    void cleanupTestCase();
    void TestCases_data();
    void TestCases();
};

PlusTest::PlusTest()
{

}

PlusTest::~PlusTest()
{

}

void PlusTest::initTestCase()
{

}

void PlusTest::cleanupTestCase()
{

}

void PlusTest::TestCases_data()
{
    QTest::addColumn<int>("number1");
    QTest::addColumn<int>("number2");
    QTest::addColumn<int>("result");

    QTest::newRow("test1") << 1 << 1 << 2;
    QTest::newRow("test2") << 2 << 2 << 4;
    QTest::newRow("test3") << 1 << 2 << 3;
}

void PlusTest::TestCases()
{
    QFETCH(int, number1);    // take data from table
    QFETCH(int, number2);
    QFETCH(int, result);

    QCOMPARE(number1+number2, result);
}

QTEST_APPLESS_MAIN(PlusTest)

#include "tst_plustest.moc"

code link:
https://github.com/theArcticOcean/qtLib/tree/master/PlusTest

 

分类: C plus plus

发表评论

电子邮件地址不会被公开。 必填项已用*标注