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?
我的程序是用PyQt编写的。我应该选择PyUnit,还是Qt的内置QTest?
更多回答
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).
这里有一个很好的教程,介绍如何在QTest中使用Python的单元测试框架(旧的链接不再起作用。在WayBackMachine中,页面显示在此处)。
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.).
这不是二选一的问题。相反,它是关于一起使用它们。QTest的目的只是模拟击键、鼠标点击和鼠标移动。Python的单元测试框架负责其余的工作(设置、拆卸、启动测试、收集结果等)。
As another option there is also pytest-qt
if you prefer working with pytest
:
作为另一种选择,如果您喜欢使用pytest,也可以使用pytest-qt:
https://pytest-qt.readthedocs.io/en/latest/intro.html
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:
它允许您测试pyqt和pyside应用程序,并允许模拟用户交互。以下是其文档中的一个小示例:
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!"
There is perfect tutorial where they use pytest-qt:
有一个完美的教程,其中他们使用了pytest-qt:
https://www.youtube.com/watch?v=WjctCBjHvmA&ab_channel=AdamBemski
https://github.com/adambemski/blog/tree/master/006_pytest_qt_GUI_testing
Https://www.youtube.com/watch?v=WjctCBjHvmA&ab_channel=AdamBemski https://github.com/adambemski/blog/tree/master/006_pytest_qt_GUI_testing
It is really simple, there is a code from the tutorial:
这真的很简单,教程中有一段代码:
import pytest
from PyQt5 import QtCore
import example_app
@pytest.fixture
def app(qtbot):
test_hello_app = example_app.MyApp()
qtbot.addWidget(test_hello_app)
return test_hello_app
def test_label(app):
assert app.text_label.text() == "Hello World!"
def test_label_after_click(app, qtbot):
qtbot.mouseClick(app.button, QtCore.Qt.LeftButton)
assert app.text_label.text() == "Changed!"
更多回答
Link does not appear to work. Maybe this is the same?
链接似乎不起作用。也许这是一样的?
@djvg Both links no longer work. Is there another link?
@djvg两个链接都不再工作。还有别的联系吗
@a-hendry: Sorry, I wouldn't know, mine was already a guess.
@a-Hendry:对不起,我不知道,我的只是个猜测。
The content of original the link can be found here thanks to the Wayback Machine.
原始链接的内容可以在这里找到,这要归功于Wayback Machine。
我是一名优秀的程序员,十分优秀!