QtTest Test Multiple Classes
Solved
General and Desktop
-
IS it possible to have two test classes in one project?
I tryed to add QTEST_MAIN in the two cpp files of the test classes but it does not work.
Do i have to make one sub project per class i want to test?
Whats the normal approach with QtTest?
-
Hi,
I am not sure I exactly get your point but I think what you are after is described in this article.
Hope it helps
-
I followed all the steps in the article but when try to tun my tests i get this:
QFATAL : TestRoom::room() QWidget: Must construct a QApplication before a QWidget FAIL! : TestRoom::room() Received a fatal error. Loc: [Unknown file(0)] Totals: 1 passed, 1 failed, 0 skipped, 0 blacklisted, 0ms ********* Finished testing of TestRoom ********* 19:37:29: The program has unexpectedly finished.
The class i want to test is derrived from QWidget. What can I do to exceute the tests?
Here my code:
QT += widgets QT += testlib QT += gui CONFIG += c++17 console TEMPLATE = app include (../hunt_the_wumpus-src.pri) APP_PATH = ../src INCLUDEPATH += $$APP_PATH DEPENDPATH += $$APP_PATH HEADERS += \ testroom.h \ testsuite.h SOURCES += \ main.cpp \ testroom.cpp \ testsuite.cpp
#ifndef TESTSUITE_H #define TESTSUITE_H #include <QObject> #include <vector> class TestSuite: public QObject { public: TestSuite(); static std::vector<QObject*> & suite(); }; #endif // TESTSUITE_H
#include "testsuite.h" TestSuite::TestSuite() { suite().push_back(this); } std::vector<QObject*> & TestSuite::suite() { static std::vector<QObject*> objects; return objects; }
#ifndef TESTROOM_H #define TESTROOM_H #include "testsuite.h" #include <QtTest/QtTest> class TestRoom : public TestSuite { Q_OBJECT public: using TestSuite::TestSuite; private slots: void room_data(); void room(); void setAndHasPit_data(); void setAndHasPit(); void setAndHasBat_data(); void setAndHasBat(); void setAndHasPlayer_data(); void setAndHasPlayer(); void setAndPlayerWasHere_data(); void setAndPlayerWasHere(); }; #endif // TESTROOM_H
#include "testroom.h" #include "room.h" /// all the tests static TestRoom TEST_ROOM;
#include "testsuite.h" #include <QtTest/QtTest> int main(int argc, char *argv[]) { // setup lambda int status = 0; auto runTest = [&status, argc, argv](QObject* obj) { status |= QTest::qExec(obj, argc, argv); }; // run suite auto &suite = TestSuite::suite(); for (auto it = suite.begin(); it != suite.end(); ++it) { runTest(*it); } }
-
ok this could be solved easy with adding
QApplication
tomain
:int main(int argc, char *argv[]) { QApplication app(argc, argv); // setup lambda int status = 0; auto runTest = [&status, &argc, argv](QObject* obj) { status |= QTest::qExec(obj, argc, argv); }; // run suite auto &suite = TestSuite::suite(); for (auto it = suite.begin(); it != suite.end(); ++it) { runTest(*it); } }
Only sad part i can't see the tests under Tools->Tests anymore. So only
Application Output
with this method I guess. -