QtTest console application without x server

2k views Asked by At

I have some unit tests for my project that I did with the QtTest framework. It is a console application, and actually I don't use anything from Qt apart from QtTest.

On my computer it works fine, however I am setting up continuous integration on Travis and I get :

lib_testapp: cannot connect to X server 

I tried running my app with -platform offscreen as I saw on another thread but it doesn't seems to change anything.

There are these lines in the .pro :

TEMPLATE = app
CONFIG  += console c++11
QT      += testlib

My main.cpp looks like this :

class WatermarkingTests: public QObject
{
    Q_OBJECT
private slots:
void initTestCase() { }
void test1() { someTest(); }
void cleanupTestCase() { }
};

QTEST_MAIN(WatermarkingTests)
#include "main.moc"

someTest() is a purely mathematical function which makes some calls to QVERIFY. Thanks!

1

There are 1 answers

0
László Papp On BEST ANSWER

Your problem is the following line:

QTEST_MAIN(WatermarkingTests)

From the documentation:

QTEST_MAIN ( TestClass )

Implements a main() function that instantiates an application object and the TestClass, and executes all tests in the order they were defined. Use this macro to build stand-alone executables.

If QT_GUI_LIB is defined, the application object will be a QApplication, otherwise it will be a QCoreApplication. If qmake is used and the configuration includes QT += gui, then QT_GUI_LIB will be defined automatically.

This means, you will get a QApplication rather than QCoreApplication since the QT variable contains core and gui by default. You would need to use the following statement to get around it:

QT = core testlib

Note that with Qt 5, you can just the following macro instead:

QTEST_GUILESS_MAIN( TestClass)

Implements a main() function that instantiates a QCoreApplication object and the TestClass, and executes all tests in the order they were defined. Use this macro to build stand-alone executables.

Behaves like QTEST_MAIN(), but instantiates a QCoreApplication instead of the QApplication object. Use this macro if your test case doesn't need functionality offered by QApplication, but the event loop is still necessary.

This function was introduced in Qt 5.0.