Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Qt Unit tests

Looking at Qt's site, and at another Stackoverflow answer because I don't want to create a separate project for each class I want to test, I've come up with the following code:

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;
}

However, I receive the following linker errors:

...
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 ...

What am I doing wrong here?

like image 279
wrongusername Avatar asked Jan 10 '11 02:01

wrongusername


People also ask

How long should unit tests run?

Still, it seems as though a 10 second short-term attention span is more or less hard-wired into the human brain. Thus, a unit test suite used for TDD should run in less than 10 seconds. If it's slower, you'll be less productive because you'll constantly lose focus.

Do unit tests run in production?

Yes of course! Unit tests run on all build configurations. Unit tests are always intact but this does not mean that shipped assemblies are dependent on anything relating to the tests. Tests are always written in a parallel assembly (in the same build environment) that then tests the production assembly.


1 Answers

Add:

CONFIG += qtestlib 

to the .pro file to get qmake to link in the qtest library.

like image 194
Michael Burr Avatar answered Sep 28 '22 19:09

Michael Burr