Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QtTest Test Multiple Classes

QtTest Test Multiple Classes

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 4.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sandro4912
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • S Offline
        S Offline
        sandro4912
        wrote on last edited by
        #3

        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);
            }
        }
        
        1 Reply Last reply
        0
        • S Offline
          S Offline
          sandro4912
          wrote on last edited by
          #4

          ok this could be solved easy with adding QApplication to main:

          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.

          1 Reply Last reply
          2
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            This also breaks test output to file (xml/xunit/csv). It's just cleaner and better to create a separate project for each test with an individual main if you want to use the Qt Test framework

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            2
            • SGaistS SGaist referenced this topic on

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved