jmcgeheeiv / pyqttestexample Goto Github PK
View Code? Open in Web Editor NEWHow to unit test GUIs created with PyQt
How to unit test GUIs created with PyQt
Commenter Rob raised this issue in the comments of deprecated blog article https://johnnado.com/pyqt-qtest-example/.
In their response, Tord gives a solution. This needs to be added to the example code.
October 22, 2015 @ 3:10 am
Nice article, John, and I have this working for my own app. Thanks! But I’m a bit disappointed because I have events connected to the widgets, but the widgets don’t signal when their values change. In retrospect, this isn’t a surprise, because app.exec_() isn’t called. I thought about putting app.exec_() on a thread, but apparently that’s a big no-no. The best I can do is after I change a value with QTest, I can all the event handler manually. But whether the correct event handler is called should be part of what I’m testing! IMHO, it seems like all we can do is test whether PyQt can change its widget values, not whether our GUIs actually respond to widget changes in the correct manner. Any ideas?
September 23, 2017 @ 11:28 pm
Hi, i have managed to get signalling working for my application, i think in general that signalling should work but i think you have to have everything connected to the same “root object”, i do like this:
When starting the application normally (not in test mode):
my_qapplication = QtWidgets.QApplication(sys.argv)
my_main = mc.matc_main.MyMainClass(my_qapplication)
my_main.main_window_qmainwindow.show()
sys.exit(my_qapplication.exec_())
For the tests:
test_app = QtWidgets.QApplication(sys.argv) # – just before the test class
my_main = mc.matc_main.MyMainClass(test_app) # -at the start of the test function where i want to use it
Also i found out that there’s something called “signal spy" which i haven’t checked out yet but which may be interesting as well.
Commenter Charlotte Godley raised this issue in the comments of deprecated blog article https://johnnado.com/pyqt-qtest-example/. In their response, Tord added links to the pytest plugin that would be used for this.
This needs to be implemented:
README.md
October 16, 2016 @ 4:30 pm
Hey,
Re other people’s comments about unittest and pyqt, I found it much easier to fix these issues using pytest because you can scope your fixtures. Means you have to move to a whole different way of working though (injecting fixtures using parameters rather than class values).
Other thing I’m trying to work on right now is testing actions and menus, which are different because you apparently can’t use mouseClick for actions. You wouldn’t happen to know anything about that would you?
September 26, 2017 @ 11:47 am
Just for reference for those finding this article (thank you John for writing it), the plugin for pytest used for running tests for PyQt is called pytest-qt:
Kind Regards, Tord
Dan raised this issue in the comments of deprecated blog article https://johnnado.com/pyqt-qtest-example/.
This may be a duplicate of #4.
December 13, 2020 @ 11:50 am
Helpful tutorial thanks! Does anybody know a good way to test the getSaveFileName()
dialogs? For example, you click File -> Save As – and then a modal dialog appears asking for the location and name of the file you want to save. When completed we should be able to assert a new file has been created somewhere (or at least the dialog generated a path). Is there anyway to complete this using unittest and QTest? Thanks!
PyQt has moved on. Update the example to the latest version.
http://johnnado.com/pyqt-qtest-example returns "403 Forbidden: You don't have permission to access /index.php on this server."
Currently blog article https://johnnado.com/pyqt-qtest-example/ is separate from the example code here on GitHub.
README.md
Tord raised this issue in the comments of deprecated blog article https://johnnado.com/pyqt-qtest-example/.
This needs to be demonstrated in the example code and documented in README.md
.
January 24, 2018 @ 5:01 pm
Hi and thank you for a great article! I wanted to ask: How can we test launching and accepting results from modal dialogs? In our project when we try to do this the execution freezed at “exec_” for the modal dialog. Can signals be used somehow or is there another approach? Kind Regards, Tord
January 24, 2018 @ 6:23 pm
I’ve found out that there are two ways to create modal dialogs, you don’t have to use .exec_()
, instead you can use .setModal(True)
and .show()
. It does require a slightly different approach but it solves the problem described above
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.