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. Help with automated testing of a QThread that pops a modal dialog
Forum Updated to NodeBB v4.3 + New Features

Help with automated testing of a QThread that pops a modal dialog

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 526 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.
  • C Offline
    C Offline
    Chris Hennes
    wrote on 23 Nov 2022, 00:14 last edited by
    #1

    Writing some automated tests, and I've got one that's stumping me.

    I have a function that can find a specific modal dialog, and it's run via a QTimer: it normally looks like this

    QtCore.QTimer.singleShot(90,dialog_watcher.run)
    self.installer_gui._report_dependency_failure("Unit test","Fake error message")
    self.assertTrue(dialog_watcher.dialog_found, "Failed to find the expected dialog box")
    

    Works great because _report_dependency_failure has a QMessageBox modal that blocks, but the timer continues to work because it's got an event loop.

    Now I have to test a function that doesn't block: it spins up a QThread and returns, and later (when it completes) it calls the function that opens the modal dialog. So my test needs to block itself until the dialog pops up, gets found, and gets closed. I tried things like:

    QtCore.QTimer.singleShot(1000,dialog_watcher.run)
    self.installer_gui.install() # This does not block
    while not dialog_watcher.has_run: # has_run gets set at the end of a call to run()
        QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents,100)
    print("Finished") # Should happen after one second, right?
    

    That timer runs on the GUI thread (the calling thread), right? What's wrong with my faked event loop there, that the timer doesn't ever seem to fire?

    Chris Hennes, Pioneer Library System

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 24 Nov 2022, 20:06 last edited by
      #4

      Then why not use only mocking and replace the slot so the dialog does not even need to be shown ?

      Note that you don't need to switch over your tests. pytest works as well with Python's unittest module.

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

      C 1 Reply Last reply 26 Nov 2022, 03:47
      1
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 23 Nov 2022, 20:31 last edited by
        #2

        Hi,

        What testing framework are you using ?

        If not already, you should consider pytest-qt. It simplifies writing test of Qt applications.

        You can then mock the slot called in order to gather information for your test.

        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
        0
        • C Offline
          C Offline
          Chris Hennes
          wrote on 24 Nov 2022, 06:05 last edited by
          #3

          We use the Python unittest package -- unfortunately I can't migrate away from that (it's a large application, with many existing tests). In this case I don't care about the dialog itself, I have a separate unit test for that. I just need to get it closed.

          Chris Hennes, Pioneer Library System

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 24 Nov 2022, 20:06 last edited by
            #4

            Then why not use only mocking and replace the slot so the dialog does not even need to be shown ?

            Note that you don't need to switch over your tests. pytest works as well with Python's unittest module.

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

            C 1 Reply Last reply 26 Nov 2022, 03:47
            1
            • S SGaist
              24 Nov 2022, 20:06

              Then why not use only mocking and replace the slot so the dialog does not even need to be shown ?

              Note that you don't need to switch over your tests. pytest works as well with Python's unittest module.

              C Offline
              C Offline
              Chris Hennes
              wrote on 26 Nov 2022, 03:47 last edited by
              #5

              @SGaist said in Help with automated testing of a QThread that pops a modal dialog:

              Then why not use only mocking and replace the slot so the dialog does not even need to be shown ?

              Hah! Because I’m blind and was looking for the most complicated possible solution? Disconnect the signal. Duh. Thanks for the kick šŸ˜Ž.

              Chris Hennes, Pioneer Library System

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 26 Nov 2022, 20:29 last edited by
                #6

                I would rather mock the slot than disconnect the signal. By doing so you change the way your application works. The mocking allows you to ensure that the slot still gets called as expected but in a controlled fashion. You can also use that technique for example to simulate when a user would need to input something.

                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
                0

                1/6

                23 Nov 2022, 00:14

                • Login

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