Question

This is the first time I'm making a test and i decided to try it in Qt since i'm on a Qt project now. What i tried to make is basically a "hello world" of a unit test so i can get familiar with Qt tests. The problem however is that compiling the test fails with:

$ make
g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++-64 -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I/usr/include/qt4/QtTest -I. -I. -o test_simple.o test_simple.cpp
/usr/bin/moc-qt4 -DQT_WEBKIT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++-64 -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I/usr/include/qt4/QtTest -I. -I. test_simple.h -o moc_test_simple.cpp
g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++-64 -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I/usr/include/qt4/QtTest -I. -I. -o moc_test_simple.o moc_test_simple.cpp
g++ -m64 -Wl,-O1 -o qtests test_simple.o moc_test_simple.o    -L/usr/lib/x86_64-linux-gnu -lQtTest -lQtGui -lQtCore -lpthread 
moc_test_simple.o: In function `main':
moc_test_simple.cpp:(.text.startup+0x0): multiple definition of `main'
test_simple.o:test_simple.cpp:(.text.startup+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [qtests] Error 1

The code consists of the class Simple and a testing class test_Simple.

simple.h:

#ifndef SIMPLE_H
#define SIMPLE_H

#include <QObject>

class Simple : public QObject
{
    int _a,_b;

public:
    void setA(int a) { _a = a; }
    void setB(int b) { _b = b; }
    int a() { return _a; }
    int b() { return _b; }
};

#endif //SIMPLE_H

test_simple.h:

#ifndef TEST_SIMPLE_H
#define TEST_SIMPLE_H

#include <QTest>
#include "simple.h"

class test_Simple : public QObject
{
    Q_OBJECT

private slots:
    void testNothing();
};

QTEST_MAIN( test_Simple )

#endif //TEST_SIMPLE_H

test_simple.cpp:

void test_Simple::testNothing()
{

    Simple s;
    s.setA(3);

    QCOMPARE( s.a(), 3 );
    QCOMPARE( s.a(), 2 );
}

EDIT: in addition to this question i want to confirm that you cannot test a class that does not inherit from QObject correct?

Was it helpful?

Solution

QTEST_MAIN( test_Simple )

This defines a main function for you -- so just put it in your testcase "main" .cpp, not in a header!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top