Przykladykdeqt

Tworzymy programik o treści:

#include <QtTest/QtTest>

class TestQString: public QObject
{
    Q_OBJECT

private slots:
    void toUpper_data();
    void toUpper();
};

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);
    QFETCH(QString, result);

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

QTEST_MAIN(TestQString)
#include "testqstring.moc"

oraz plik testqstring.pro o treści:

SOURCES += testqstring.cpp

CONFIG  += qtestlib

Kompilacja w konsoli:

kris@sphinx:~/workspace/testqstring2> qmake -project "CONFIG += qtestlib"

kris@sphinx:~/workspace/testqstring2> qmake

kris@sphinx:~/workspace/testqstring2> make

Notatka:
Jeśli program pisaliśmy w Qt Creator to naciskamy jedynie Uruchom.

Pliki po kompilacji:

kris@sphinx:~/workspace/testqstring2> ll

razem 608
-rw-r--r-- 1 kris users   7039 04-07 14:50 Makefile
-rwxr-xr-x 1 kris users 264539 04-07 14:50 testqstring2
-rw-r--r-- 1 kris users     47 04-07 14:50 testqstring2.pro
-rw-r--r-- 1 kris users    607 04-07 14:50 testqstring.cpp
-rw-r--r-- 1 kris users   2366 04-07 14:50 testqstring.moc
-rw-r--r-- 1 kris users 332680 04-07 14:50 testqstring.o

Uruchomienie:

kris@sphinx:~/workspace/testqstring2> ./testqstring2
********* Start testing of TestQString *********
Config: Using QTest library 4.6.3, Qt 4.6.3
PASS   : TestQString::initTestCase()
PASS   : TestQString::toUpper()
PASS   : TestQString::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped
********* Finished testing of TestQString *********