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);
相關文章
相關標籤/搜索