How to handle modal dialog in pytest-qt without mocking the dialog
See original GitHub issueI 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
-
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.
-
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:
- Created 4 years ago
- Reactions:1
- Comments:8 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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. š
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:I think this small hack is a small enough price for the reliability it brings to the table.