question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

How to handle modal dialog in pytest-qt without mocking the dialog

See original GitHub issue

I am using pytest-qt to automate the testing of a PyQt GUI. The dialogs need to be handled as a part of the testing(dialogs should not be mocked).

For example, file dialog that comes after a button-click has to be handled. There are 2 problems

  1. After the button click command, the program control goes to the event handler and not to the next line where I can try to send mouseclick/keystrokes to the dialog.

  2. Since the QDialog is not added to the main widget, it is not being listed among the children of the main widget. So how to get the reference of the QDialog?

I tried multi-threading but that didnā€™t work, later I found that QObjects are not thread-safe.

def test_filedialog(qtbot, window):
    qtbot.mouseClick(window.browseButton, QtCore.Qt.LeftButton, delay=1)    
    print("After mouse click")    
    #This is where I need to get the reference of QDialog and handle it

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
nicoddemuscommented, Mar 29, 2019

Thanks! I would love a PR updating the docs with an example like this one.

Please close the issue if you donā€™t have further questions. šŸ‘

1reaction
nicoddemuscommented, Mar 29, 2019

Is there any way to find the reference of a dialog when it is not added to any parent control? Else would you suggest to add the dialog as a child to any other control?

On way that Iā€™ve accomplished this is to keep the reference to the dialog somewhere, for testing only. Then you can trigger your event in the next iteration of the event loop by passing 0 timeout to the timer:


class MyWindow(QWidget):


    def on_browse_button(self):
        self._test_dialog = QFileDialog(...)
        try:
            # do something with dialog
        finally:
            self._test_dialog = None
    

def test_filedialog(qtbot, window):
    def handle_dialog():
        while window._test_dialog is None:
            qApp.processEvents()
        # handle dialog now
    QTimer.singleShot(0, handle_dialog)
    qtbot.mouseClick(window.browseButton, QtCore.Qt.LeftButton, delay=1) 

I think this small hack is a small enough price for the reliability it brings to the table.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to handle modal dialog in pytest-qt without mocking the ...
It can be done using QTimer . def test_filedialog(qtbot, window): def handle_dialog(): # get a reference to the dialog and handle it hereĀ ......
Read more >
A note about Modal Dialogs ā€” pytest-qt documentation
Simple DialogsĀ¶. For QMessageBox.question one approach is to mock the function using the monkeypatch fixture: def test_Qt(qtbot, monkeypatch): simpleĀ ...
Read more >
Testing A Qfiledialog While Using Pyside - ADocLib
How to handle modal dialog in pytestqt without mocking the dialog. ... In it we'll cover the basics of using PyQt in conjunction...
Read more >
pytest-qt - PyPI
If not, pytest-qt will try to import and use the Qt APIs, in this order: PySide6. PySide2. PyQt6. PyQt5. To force aĀ ...
Read more >
QGIS 3 Plugin Tutorial - Geocoding with Nominatim Part 4 ...
Read this tutorial to see it's really not that painful. ... parts of the Qt/QGIS API which are harder to test, such as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found