我刚刚完成了一个软件,现在想借助python的TestCase
对其进行测试。方法test_show_video
应该启动一个QTimer
。它连接到test_run_iteration
,后者每0,1秒将一个索引相加。在调用assert
之前,该方法有3秒钟的时间来汇总索引,因此它应该大于零。但事实并非如此。有人有线索吗?实际上,到计时器的超时连接似乎是错误的。在
try:
app = QtGui.QApplication(sys.argv)
except RuntimeError:
app = QtCore.QCoreApplication.instance()
class TestProgressPresenter(TestCase):
def test_show_video(self):
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.test_run_iteration)
self.timer.start(100)
self.index = 0
millis = start_time = int(round(time.time() * 1000))
while start_time + 3000 > millis:
millis = int(round(time.time() * 1000))
assert self.index > 0
def test_run_iteration(self):
self.index += 1
在这里,您将在
while
循环中阻塞Qt事件循环3秒钟。实际上,计时器的timeout
信号在控件返回到事件循环之前是不会被调用的,这是while完成和test_show_video
完成的时候。在如果希望触发计时器,则应确保事件在while循环检查期间得到处理。为此,你可以有一些东西,比如:
使用本地事件循环可以更轻松地等待几秒钟:
^{pr2}$相关问题 更多 >
编程相关推荐