Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I unit testing my GUI program with Python and PyQt?

I heard Unit Testing is a great method to keep code working correctly.

The unit testing usually puts a simple input to a function, and check its simple output. But how do I test a UI?

My program is written in PyQt. Should I choose PyUnit, or Qt's built-in QTest?

like image 307
比尔盖子 Avatar asked Feb 23 '13 19:02

比尔盖子


People also ask

Is PyQt good for GUI?

PyQt is a Python binding for Qt, which is a set of C++ libraries and development tools that include platform-independent abstractions for Graphical User Interfaces (GUI), as well as networking, threads, regular expressions, SQL databases, SVG, OpenGL, XML, and many other powerful features.

Which is better PyQt or KIVY?

In this article, we conclude that there is no much difference in Kivy or PyQt when speaking about working on GUI in Python, but they both are frameworks that work on different applications such Kivy is much better for mobile apps and other multi-touch apps than desktop apps, whereas PyQt is much better for desktop apps ...

Is PyQt5 better than TkInter?

thenPyQt comes with many powerful and advanced widgets. TkInter does not come with advanced widgets. 5. PyQt have a Qt Designer tool which we can use to build GUIs than get python code of that GUI using Qt Designer.


2 Answers

There's a good tutorial about using Python's unit testing framework with QTest here (old link that does not work anymore. From the WayBackMachine, the page is displayed here).

It isn't about choosing one or the other. Instead, it's about using them together. The purpose of QTest is only to simulate keystrokes, mouse clicks, and mouse movement. Python's unit testing framework handles the rest (setup, teardown, launching tests, gathering results, etc.).

like image 143
WLin Avatar answered Oct 11 '22 09:10

WLin


As another option there is also pytest-qt if you prefer working with pytest:

https://pytest-qt.readthedocs.io/en/latest/intro.html

It lets you test pyqt and pyside applications and allows the simulation of user interaction. Here is a small example from its documentation:

def test_hello(qtbot):     widget = HelloWidget()     qtbot.addWidget(widget)      # click in the Greet button and make sure it updates the appropriate label     qtbot.mouseClick(widget.button_greet, QtCore.Qt.LeftButton)      assert widget.greet_label.text() == "Hello!"  
like image 36
rayon Avatar answered Oct 11 '22 09:10

rayon