我有两个测试类(TrialTest1
和TrialTest2
)写在两个文件(test_trial1.py
和test_trial2.py
)中大多数相同(唯一的区别是类名) :
from twisted.internet import reactor
from twisted.trial import unittest
class TrialTest1(unittest.TestCase):
def setUp(self):
print("setUp()")
def test_main(self):
print("test_main")
reactor.callLater(1, self._called_by_deffered1)
reactor.run()
def _called_by_deffered1(self):
print("_called_by_deffered1")
reactor.callLater(1, self._called_by_deffered2)
def _called_by_deffered2(self):
print("_called_by_deffered2")
reactor.stop()
def tearDown(self):
print("tearDown()")
当我完全运行每个测试时,一切都很好。但是当我启动时,我有以下输出:
setUp()
test_main
_called_by_deffered1
_called_by_deffered2
tearDown()
setUp()
test_main
tearDown()
Error
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/twisted/internet/defer.py", line 137, in maybeDeferred
result = f(*args, **kw)
File "/usr/lib/python2.7/site-packages/twisted/internet/utils.py", line 203, in runWithWarningsSuppressed
reraise(exc_info[1], exc_info[2])
File "/usr/lib/python2.7/site-packages/twisted/internet/utils.py", line 199, in runWithWarningsSuppressed
result = f(*a, **kw)
File "/home/kartoch/works/python/netkython/tests/test_twisted_trial2.py", line 13, in test_main
reactor.run()
File "/usr/lib/python2.7/site-packages/twisted/internet/base.py", line 1191, in run
self.startRunning(installSignalHandlers=installSignalHandlers)
File "/usr/lib/python2.7/site-packages/twisted/internet/base.py", line 1171, in startRunning
ReactorBase.startRunning(self)
File "/usr/lib/python2.7/site-packages/twisted/internet/base.py", line 683, in startRunning
raise error.ReactorNotRestartable()
ReactorNotRestartable
Error
DirtyReactorAggregateError: Reactor was unclean.
DelayedCalls: (set twisted.internet.base.DelayedCall.debug = True to debug)
<DelayedCall 0x8d6482c [0.98535490036s] called=0 cancelled=0 TrialTest2._called_by_deffered1()>
Process finished with exit code 0
第一次测试后反应堆似乎没有正确关闭。有谁知道问题出在哪里?似乎tearDown()
被提前调用(在第二次测试中_called_by_deffered1
之前),也许修复将使用deferTearDown
(未记录的试验单元测试方法)。
建议的解决方案之一是删除reactor.run()
和reactor.stop()
,因为反应堆不可重启,默认情况下您只有一个反应堆实例用于所有测试:
class TrialTest1(unittest.TestCase):
def setUp(self):
print("setUp()")
def test_main(self):
print("test_main")
reactor.callLater(1, self._called_by_deffered1)
def _called_by_deffered1(self):
print("_called_by_deffered1")
reactor.callLater(1, self._called_by_deffered2)
def _called_by_deffered2(self):
print("_called_by_deffered2")
def tearDown(self):
print("tearDown()")
但是当删除对这些方法的调用时,我的测试失败而没有执行_called_by_deffered
方法:
setUp()
test_main
tearDown()
Error
DirtyReactorAggregateError: Reactor was unclean.
DelayedCalls: (set twisted.internet.base.DelayedCall.debug = True to debug)
<DelayedCall 0x94967ec [0.99936413765s] called=0 cancelled=0 TrialTest1._called_by_deffered1()>
setUp()
test_main
tearDown()
Error
DirtyReactorAggregateError: Reactor was unclean.
DelayedCalls: (set twisted.internet.base.DelayedCall.debug = True to debug)
<DelayedCall 0x94968cc [0.99958896637s] called=0 cancelled=0 TrialTest2._called_by_deffered1()>
如果我只想在测试之间只使用一个reactor共享实例,那么_called_by_deffered
方法如何成为测试的一部分(即在tearDown
之前执行)?
答案 0 :(得分:4)
反应堆无法重启。您可以通过两种明显的选择来编写测试。
一个是使用全球反应堆。试用开始和停止 - 您的测试永远不必致电reactor.run
或reactor.stop
(他们永远不应该)。它可以通常的方式访问from twisted.internet import reactor
。
另一种是每次测试使用一个新的反应器实例。在twisted.test.proto_helpers
中有一些面向测试的反应器实例(这是twisted.test
的唯一部分,顺便说一下,它是一个公共的,受支持的接口)。 MemoryReactor
和StringTransport
可以帮助您测试网络互动。 twisted.internet.task.Clock
可以帮助您测试基于时间的事件。
答案 1 :(得分:4)
在Jean-Paul的帮助下this page和this question,我已经能够使用twisted.internet.task.deferLater()
来解决问题。总结我正在寻找的观点:如果一个测试方法返回一个延迟,只有当所有延迟被触发时才会调用'tearDown()'方法。
这是代码:
from twisted.trial import unittest
from twisted.internet import reactor, task
class TrialTest1(unittest.TestCase):
def setUp(self):
print("setUp()")
def test_main(self):
print("test_main()")
return task.deferLater(reactor, 1, self._called_by_deffered1)
def _called_by_deffered1(self):
print("_called_by_deffered1()")
return task.deferLater(reactor, 1, self._called_by_deffered2)
def _called_by_deffered2(self):
print("_called_by_deffered2()")
def tearDown(self):
print("tearDown()")
输出:
setUp()
test_main()
// (1s wait)
_called_by_deffered1()
// (1s wait)
_called_by_deffered2()
tearDown()