pyqt5学习笔记2: UI测试
1.QTest介绍
PyQt程序可以使用QTest进行自动测试。
QTest要配合unittest使用。
QTest可以对窗体中的各种控件进行模拟操作
类型 | 函数 | 说明 |
---|---|---|
键盘操作 | KeyClick() | 键盘按一个键 |
KeyClicks() | 键盘按多个键 | |
KeyEvent() | 键盘事件 | |
KeyPress() | 键盘按下 | |
KeyRelease() | 键盘释放 | |
鼠标操作 | mouseClick() | 鼠标单击 |
mouseDClick() | 鼠标双击 | |
mouseMove() | 鼠标移动 | |
mousePress() | 鼠标按下 | |
mouseRelease() | 鼠标释放 |
2. 实例
UI效果图:
UI代码(保存为文件main_window.py):
import sys
from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5 import QtGui
class CMainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.o_btn_pre = self._gen_btn_pre()
self.o_btn_next = self._gen_btn_next()
self.o_btn_clear = self._gen_btn_clear()
self.o_label = self._gen_label()
self.o_central_widget = self._gen_central_widget()
self.setCentralWidget(self.o_central_widget)
def _gen_btn_pre(self):
_o_btn = QtWidgets.QPushButton('减一')
_o_btn.setFont(QtGui.QFont('Roman times', 15))
_o_btn.clicked.connect(self._on_btn_pre_clicked)
return _o_btn
def _on_btn_pre_clicked(self):
i = self._get_label_value()
self.o_label.setText(f'{i-1}')
def _gen_btn_next(self):
_o_btn = QtWidgets.QPushButton('加一')
_o_btn.setFont(QtGui.QFont('Roman times', 15))
_o_btn.clicked.connect(self._on_btn_next_clicked)
return _o_btn
def _on_btn_next_clicked(self):
_i = self._get_label_value()
self.o_label.setText(f'{_i+1}')
def _gen_btn_clear(self):
_o_btn = QtWidgets.QPushButton('回零')
_o_btn.setFont(QtGui.QFont('Roman times', 15))
_o_btn.clicked.connect(self._on_btn_clear_clicked)
return _o_btn
def _on_btn_clear_clicked(self):
self.o_label.setText(f'0')
def _gen_label(self):
_o_label = QtWidgets.QLabel('0')
_o_label.setFont(QtGui.QFont('Roman times', 40))
return _o_label
def _get_label_value(self):
_i = int(self.o_label.text())
return _i
def _gen_central_widget(self):
_o_widget = QtWidgets.QWidget()
_o_widget.setLayout(self._gen_layout())
return _o_widget
def _gen_layout(self):
_o_layout_label = QtWidgets.QHBoxLayout()
_o_layout_label.addStretch(1)
_o_layout_label.addWidget(self.o_label)
_o_layout_label.addStretch(1)
_o_layout_btn = QtWidgets.QHBoxLayout()
_o_layout_btn.addWidget(self.o_btn_pre)
_o_layout_btn.addWidget(self.o_btn_next)
_o_layout_btn.addWidget(self.o_btn_clear)
_o_layout_main = QtWidgets.QVBoxLayout()
_o_layout_main.addLayout(_o_layout_label)
_o_layout_main.addLayout(_o_layout_btn)
return _o_layout_main
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
win = CMainWindow()
win.show()
sys.exit(app.exec_())
UI测试效果图:
测试代码:保存为文件test_ui.py
import sys
import unittest # 导入单元测试模块
from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5 import QtTest # 导入PyQt测试模块
from main_window import CMainWindow # 导入自定义窗口
class TestUi(unittest.TestCase): # 创建TestCase
def setUp(self):
# 在setUp中对窗口进行实例化(需要先例化app, 不需要窗口.show()和app.exec())
self.o_app = QtWidgets.QApplication(sys.argv)
self.o_main_window = CMainWindow()
#self.o_main_window.show()
#sys.exit(self.o_app.exec_())
def test_btn_clear(self):
# 测试回零按钮
# 先点几下减一按钮, 再点击回零按钮, 判断值是否为0
QtTest.QTest.mouseClick(self.o_main_window.o_btn_pre, QtCore.Qt.LeftButton)
QtTest.QTest.mouseClick(self.o_main_window.o_btn_pre, QtCore.Qt.LeftButton)
QtTest.QTest.mouseClick(self.o_main_window.o_btn_pre, QtCore.Qt.LeftButton)
QtTest.QTest.mouseClick(self.o_main_window.o_btn_clear, QtCore.Qt.LeftButton)
self.assertEqual('0', self.o_main_window.o_label.text())
# 先点几下加一按钮, 再点击回零按钮, 判断值是否为0
QtTest.QTest.mouseClick(self.o_main_window.o_btn_next, QtCore.Qt.LeftButton)
QtTest.QTest.mouseClick(self.o_main_window.o_btn_next, QtCore.Qt.LeftButton)
QtTest.QTest.mouseClick(self.o_main_window.o_btn_next, QtCore.Qt.LeftButton)
QtTest.QTest.mouseClick(self.o_main_window.o_btn_clear, QtCore.Qt.LeftButton)
self.assertEqual('0', self.o_main_window.o_label.text())
#print('btn_clear done')
def test_btn_pre(self):
# 测试减一按钮
_i = self.o_main_window._get_label_value()
# 点击减一, 看数值是否减一
QtTest.QTest.mouseClick(self.o_main_window.o_btn_pre, QtCore.Qt.LeftButton)
self.assertEqual(f'{_i-1}', self.o_main_window.o_label.text())
# 再点击减一, 看数值是否又减一
QtTest.QTest.mouseClick(self.o_main_window.o_btn_pre, QtCore.Qt.LeftButton)
self.assertEqual(f'{_i-2}', self.o_main_window.o_label.text())
#print('btn_pre done')
def test_btn_next(self):
# 测试加一按钮
_i = self.o_main_window._get_label_value()
# 点击加一, 看数值是否加一
QtTest.QTest.mouseClick(self.o_main_window.o_btn_next, QtCore.Qt.LeftButton)
self.assertEqual(f'{_i+1}', self.o_main_window.o_label.text())
# 再点击加一, 看数值是否又加一
QtTest.QTest.mouseClick(self.o_main_window.o_btn_next, QtCore.Qt.LeftButton)
self.assertEqual(f'{_i+2}', self.o_main_window.o_label.text())
#print('btn_next done')
# 再点击加一, 但比较值不是_i+3,会报告Fail。
QtTest.QTest.mouseClick(self.o_main_window.o_btn_next, QtCore.Qt.LeftButton)
self.assertEqual(f'{_i+2}', self.o_main_window.o_label.text())
#print('btn_next done')
if __name__ == '__main__':
unittest.main()