本文介绍了PyQt &unittest - 测试信号和插槽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 pyqt 应用程序,我正在为其编写单元测试,它严重依赖信号和插槽.为了正确测试它,我必须检查是否发送了正确的信号.

I have a pyqt application that I'm writing unit tests for, and it relies heavily on signals and slots. To properly test it, I have to check that the correct signals are sent.

这样做的最佳方法是什么?我看到 Qt 库有一个 QSignalSpy,但我在 PyQt 中找不到任何对此的引用.我能想到的唯一选择是模拟发射,例如

What is the best way to do this? I see that the Qt library has a QSignalSpy, but I can't find any reference to this in PyQt. The only option I can think of is to mock emit, e.g.

import testedmodule

def myemit(signal):
    ....

testedmodule.QObject.emit = myemit

但我希望有更好的方法.

but I'm hoping there is a better way.


我的模块作为线程运行,在这种情况下,在启动线程后重写实例的发出不再起作用,因此我更新了上面的代码以反映这一点.


My module is run as a thread, in that case overriding emit of an instance no longer worked after starting the thread so I updated the code above to reflect this.

推荐答案

注意 QSignalSpy 是 可用作为 PyQt5 中的 QtTest.QSignalSpy.

Note QSignalSpy is available as QtTest.QSignalSpy in PyQt5.

这篇关于PyQt &unittest - 测试信号和插槽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 01:18