2011-01-10 21 views
6

Guardando Qt's site, ea another Stackoverflow answer perché non voglio creare un progetto separato per ogni classe voglio provare, mi è venuta in mente il seguente codice:Esecuzione Qt unit test

testqstring.h

#ifndef TESTQSTRING_H 
#define TESTQSTRING_H 

#include <QtTest/QTest> 

class TestQString : public QObject 
{ 
    Q_OBJECT 

private slots: 
    void toUpper(); 
}; 

#endif // TESTQSTRING_H 

testqstring.cpp

#include "testqstring.h" 
#include <QString> 

void TestQString::toUpper() 
{ 
    QString str = "Hello"; 
    QCOMPARE(str.toUpper(), QString("HELLO")); 
} 

main.cpp

#include "testqstring.h" 

int main(int argc, char *argv[]) 
{ 
    TestQString testqstring; 
    QTest::qExec(&testqstring, argc, argv); 
    return 0; 
} 

Tuttavia, ricevo i seguenti errori del linker:

... 
g++ -headerpad_max_install_names -arch i386 -o tester main.o testqstring.o moc_testqstring.o -F/Library/Frameworks -L/Library/Frameworks -framework QtCore 
Undefined symbols: 
"QTest::qExec(QObject*, int, char**)", referenced from: 
_main in main.o 
"QTest::compare_helper(bool, char const*, char*, char*, char const*, char const*, char const*, int)", referenced from: 
bool QTest::qCompare<QString>(QString const&, QString const&, char const*, char const*, char const*, int)in testqstring.o 
... and more like that ... 

che cosa sto facendo male qui?

+1

Non è necessario collegarsi con QTest? –

+0

@Noah cosa intendi con questo? – wrongusername

risposta

10

Add:

CONFIG += qtestlib 

al file .pro per arrivare qmake a linkare la libreria Qtest.

+5

O al giorno d'oggi: "QT + = testlib" – StellarVortex