如何使用 Python 和 PyQt 对我的 GUI 程序进行单元测试?

如何使用 Python 和 PyQt 对我的 GUI 程序进行单元测试?

问题描述:

我听说单元测试是保持代码正常工作的好方法.

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

单元测试通常将一个简单的输入放入一个函数,并检查它的简单输出.但我如何测试 UI?

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

我的程序是用 PyQt 编写的.我应该选择 PyUnit,还是 Qt 内置的 QTest?

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

有一个关于使用 Python 的单元测试框架和 QTest 的很好的教程 这里.

There's a good tutorial about using Python's unit testing framework with QTest here.

这不是关于选择一个或另一个.相反,它是关于将它们一起使用. QTest 的目的只是模拟击键、鼠标点击和鼠标移动.Python 的单元测试框架会处理其余的工作(设置、拆卸、启动测试、收集结果等).

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.).