Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在pyside单元测试中启动事件循环?_Python_Qt_Pyside - Fatal编程技术网

Python 如何在pyside单元测试中启动事件循环?

Python 如何在pyside单元测试中启动事件循环?,python,qt,pyside,Python,Qt,Pyside,我试图对涉及信号和qtimer的类进行单元测试。(见附件)。我很难理解将app.exec_389;()调用放在何处,而不冻结所有内容并使用发出的信号执行测试。就像在我引用的问题中一样,我认为这里的问题是我需要一个事件循环,但是加入setUpClass肯定不起作用。这是我为重现我的问题而编写的最小的代码片段,实际上,这是一个更大的代码库 testSignals.py: import time from PySide import QtCore class TestSignals(QtCore.Q

我试图对涉及信号和qtimer的类进行单元测试。(见附件)。我很难理解将app.exec_389;()调用放在何处,而不冻结所有内容并使用发出的信号执行测试。就像在我引用的问题中一样,我认为这里的问题是我需要一个事件循环,但是加入setUpClass肯定不起作用。这是我为重现我的问题而编写的最小的代码片段,实际上,这是一个更大的代码库

testSignals.py:

import time
from PySide import QtCore

class TestSignals(QtCore.QObject):

    def __init__(self):
        self.timer = QtCore.QTimer()
        self.myTime = ''
        self.timer.timeout.connect(self.updateTime)
        self.timer.start(100)

    def getTime(self):
        return self.myTime

    def updateTime(self):
        self.myTime = time.strftime('%H:%M:%S')

    def stop(self):
        self.timer.stop()
unitTesting.py:

import unittest
from testSignals import TestSignals 
from PySide import QtGui, QtCore
from testSignals import TestSignals
import sys

class test(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        app = QtGui.QApplication(sys.argv)
        assert id(app) == id(QtGui.qApp) and id(app) == id(QtCore.QCoreApplication.instance())
        cls.test = TestSignals()
        # app.exec_()   # uncommenting this only causes the code to freeze

    def testStarted(self):
        mytime = self.test.getTime()
        self.assertNotEqual(mytime,'','Time is still empty')

if __name__ == '__main__':
    unittest.main()
结果是唯一的测试失败,因为“mytime”仍然为空。为了清楚起见,该课程确实有效:

class testConnection():
    def receiveMe(self):
        print(self.test.getTime())
        # print(self.test.getData())

    # signal is emmited when data is changed
    def __init__(self):
        self.test = TestSignals()
        self.test.timer.timeout.connect(self.receiveMe)
        # self.test.start()

app = QtCore.QCoreApplication([])
test = testConnection()
timer = QtCore.QTimer()
timer.singleShot(4000,app.exit)
app.exec_()
产生:

>>>$ python3 testConection.py 
14:50:21
14:50:22
14:50:23
14:50:24

计时器的用途是什么?请提供一个实际的示例,并解释您真正想要测试的内容。一个类将从服务读取数据。这些数据将在GUI中使用,以便打印或放入表格中。每n秒读取一次数据(n为1/10、1/100或1/1000)。我不想检查数据是否更改,我想要一个链接到函数的信号,以便在读取数据后更新更改。我能做到,如果我能让它工作的话。我真的很想对此进行单元测试,因为代码将来可能会增长。我想测试的是信号是否发出。这似乎微不足道。但是要使用信号,我需要一个事件循环,这是我不知道如何在单元测试中实现的。举个例子就足够了。现在考虑一下,我甚至不需要示例中的计时器。我需要一个信号,一个插槽,我想要的是测试它。在单线程应用程序中,信号不需要事件循环。在这种情况下,它们的工作方式非常类似于回调(即同步调用连接的插槽)。你看过吗?这里有一些你可能会觉得有用的示例代码。是的,我已经读过了,但是在这个示例中没有信号或事件循环。我发现了一些在QT中测试信号和线程的例子,但是用C++代码。测试的主要功能已修改,但不完全了解如何在pyside中实现这一点。