简体   繁体   中英

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?

There's a good tutorial about using Python's unit testing framework with QTest 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.).

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!"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM