在单元测试CI中模拟PyQt5 QMessageBox小部件中的按钮单击

伺服器

如果我们运行下面的最小示例,则无需长篇大论:

$ python3
Python 3.7.6 (default, Jan 30 2020, 09:44:41) 
[GCC 9.2.1 20190827 (Red Hat 9.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import unittest import sys
>>> from PyQt5.QtWidgets import QMessageBox, QApplication
>>> import unittest
>>>
>>> class Fortest():
...     def messagebox(self):
...         app = QApplication(sys.argv)
...         msg = QMessageBox()
...         msg.setIcon(QMessageBox.Warning)
...         msg.setText("message text")
...         msg.setStandardButtons(QMessageBox.Close)
...         msg.buttonClicked.connect(msg.close)
...         msg.exec()
... 
>>> class Test(unittest.TestCase):
...     def testMessagebox(self):
...         a=Fortest()
...         a.messagebox()
... 
>>> unittest(Test().testMessagebox())

我们停留在要求单击“关闭”按钮的小部件上。这与持续集成单元测试不兼容...

如何模拟测试代码(类Test)中关闭按钮的单击,而无需更改要测试的代码(类Fortest)?

永乐

逻辑:

  • 获取QMessageBox,在此您可以使用QApplication::activeWindow()

  • 使用button()QMessageBox方法获取QPushButton

  • 使用QTest子模块的mouseClick()方法单击。

但是上述操作必须在显示QMessageBox之后的瞬间完成,并且为此必须延迟(在这种情况下,您可以使用threading.Timer())。

import sys

import unittest
import threading

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMessageBox, QApplication
from PyQt5.QtTest import QTest


class Fortest:
    def messagebox(self):
        app = QApplication(sys.argv)
        msg = QMessageBox()
        msg.setIcon(QMessageBox.Warning)
        msg.setText("message text")
        msg.setStandardButtons(QMessageBox.Close)
        msg.buttonClicked.connect(msg.close)
        msg.exec_()


class Test(unittest.TestCase):
    def testMessagebox(self):
        a = Fortest()
        threading.Timer(1, self.execute_click).start()
        a.messagebox()

    def execute_click(self):
        w = QApplication.activeWindow()
        if isinstance(w, QMessageBox):
            close_button = w.button(QMessageBox.Close)
            QTest.mouseClick(close_button, Qt.LeftButton)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何单击小部件中的按钮来关闭覆盖图?

来自分类Dev

处理主屏幕小部件中的按钮单击

来自分类Dev

小部件崩溃并且无法通过单击小部件中的按钮来启动新活动

来自分类Dev

如何从定制的QMessageBox中捕获按钮单击?

来自分类Dev

如何通过单击按钮突出显示(选择)“文本”小部件中的文本?

来自分类Dev

GTK + 2 C-输入按下或单击按钮以从条目小部件中获取文本

来自分类Dev

GTK + 2 C-输入按下或单击按钮以从条目小部件中获取文本

来自分类Dev

如何在小部件中的按钮单击上更新我的TextView?

来自分类Dev

如何在单击小部件中的按钮时显示烤面包并打开蓝牙?

来自分类Dev

在matplotlib小部件中单击按钮时提取/打印滑块值(Python 2.7)

来自分类Dev

在单元测试中模拟/存根RuntimeException

来自分类Dev

单元测试中的部分模拟/错误

来自分类Dev

在单元测试中模拟Spark RDD

来自分类常见问题

在AngularJS单元测试中模拟$ modal

来自分类Dev

在单元测试中模拟$ mdSideNav

来自分类Dev

在单元测试中模拟HTTP请求

来自分类Dev

TyphoonPatcher,用于单元测试中的模拟

来自分类Dev

在单元测试中模拟FTP

来自分类Dev

在单元测试中模拟ngModel

来自分类Dev

在单元测试中模拟服务

来自分类Dev

在单元测试中模拟IMemoryCache

来自分类Dev

模拟python单元测试中的问题

来自分类Dev

在单元测试中模拟依赖项

来自分类Dev

Angular:在单元测试中模拟HttpClient

来自分类Dev

在grails单元测试中模拟表单

来自分类Dev

TyphoonPatcher,用于单元测试中的模拟

来自分类Dev

在单元测试中模拟$ mdSideNav

来自分类Dev

在Tkinter中更改按钮小部件的大小

来自分类Dev

删除 kivy 小部件中的按钮

Related 相关文章

热门标签

归档