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. Is there a canonical way to set up QApplication and Google Test together?
Forum Updated to NodeBB v4.3 + New Features

Is there a canonical way to set up QApplication and Google Test together?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 5 Posters 3.8k Views 2 Watching
  • 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.
  • shaveraS Offline
    shaveraS Offline
    shavera
    wrote on last edited by
    #1

    When testing anything using QWidget stuff, one obviously needs to set up a QApplication. However, it's not clear how to actually run the tests within this QApplication.
    Specifically, it seems to me like I would want QApplication::exec to start the RUN_ALL_TESTS() macro, and then for the result of that macro to go into a QApplication::exit(return) statement. Naively, I tried to connect QApplication::applicationStateChanged to a function that would call RUN_ALL_TESTS and exit, but that signal doesn't appear to be sent when QApplication::exec is called.
    Seemingly, with the increased compatibility with Google test framework (Qt Creator support, autotest, eg), there should be some kind of canonical way to spin up both QApplication and execute the tests.

    My existing, kind of working setup is one that creates a QApplication in a test fixture that needs it, spins it up, and then exits. But we know that QApplication should be a singleton, and I would guess that calling exec/exit multiple times on such an object would be problematic. It turns out that this setup does crash in a seemingly non-deterministic way, but I haven't investigated too deeply since I suspect it's already broken by the above.

    1 Reply Last reply
    0
    • shaveraS Offline
      shaveraS Offline
      shavera
      wrote on last edited by
      #2

      After many iterations, I've finally found what I think works well here:

      int main(int argc, char *argv[])
      {
          QCoreApplication app{argc, argv};
      
          QTimer::singleShot(0, [&]()
          {
              ::testing::InitGoogleTest(&argc, argv);
              auto testResult = RUN_ALL_TESTS();
              app.exit(testResult);
          });
      
          return app.exec();
      }
      
      R 1 Reply Last reply
      5
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        If you have a recent Qt Creator, there's a template project for that.

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

        D 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          If you have a recent Qt Creator, there's a template project for that.

          D Offline
          D Offline
          Dariusz
          wrote on last edited by Dariusz
          #4

          @SGaist said in Is there a canonical way to set up QApplication and Google Test together?:

          Hi,

          If you have a recent Qt Creator, there's a template project for that.

          Seems like after 4 years its not the case...>
          35fe92a3-f260-435e-951e-e791b76b9362-image.png
          84b832e1-49d3-4a2f-be6c-0a3b626652c8-image.png

          No information how QApplication starts/etc.

          1 Reply Last reply
          0
          • shaveraS shavera

            After many iterations, I've finally found what I think works well here:

            int main(int argc, char *argv[])
            {
                QCoreApplication app{argc, argv};
            
                QTimer::singleShot(0, [&]()
                {
                    ::testing::InitGoogleTest(&argc, argv);
                    auto testResult = RUN_ALL_TESTS();
                    app.exit(testResult);
                });
            
                return app.exec();
            }
            
            R Offline
            R Offline
            RainyTown
            wrote on last edited by
            #5

            @shavera said in Is there a canonical way to set up QApplication and Google Test together?:

            After many iterations, I've finally found what I think works well here:

            int main(int argc, char *argv[])
            {
                QCoreApplication app{argc, argv};
            
                QTimer::singleShot(0, [&]()
                {
                    ::testing::InitGoogleTest(&argc, argv);
                    auto testResult = RUN_ALL_TESTS();
                    app.exit(testResult);
                });
            
                return app.exec();
            }
            

            it works for me

            1 Reply Last reply
            0

            • Login

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