Qt单元测试


单元测试之作用
要完成测试用例,保证设计上的耦合依赖
通过测试用例,保证覆盖率,提高程序质量

QTest一些有用的静态函数
QTest::qExec
QTest::qSleep
QTest::qWait
 
例子
.pro项目文件中加入
QT += testlib

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    SelfUnitTestClass tc;
    MyUnitTest mt;
    QTest::qExec(&mt);
    return QTest::qExec(&tc, argc, argv);
}

class MyUnitTest : public QObject
{
    Q_OBJECT       
private Q_SLOTS:
    void toUpper_data();
    void toUpper();
};

void MyUnitTest::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 MyUnitTest::toUpper()
{
//    QString str = "Hello";
//    QVERIFY(str.toUpper() == "HELLO");
     //数据驱动
    QFETCH(QString, string);
    QFETCH(QString, result);
    QCOMPARE(string.toUpper(), result);
}
 
#define private public/protect

//all private method is under private slots
Foo a;
QMetaObject::invokeMethod(&a, "Test", Qt::DirectConnection);
原文地址:https://www.cnblogs.com/danju/p/3691489.html