我们的Python代码库使用nose来运行所有单元测试,我尝试使用nose.twistedtools来运行所有测试,但是他们都在关机时挂起。任何有助于理顺这一点的帮助都会很棒。这是一个简单的测试:
from nose.twistedtools import deferred
from twisted.internet.defer import Deferred
@deferred(timeout=1)
def test_rudimentary():
def done(_ignored):
print "DONE"
defer = Deferred()
defer.addCallback(done)
return defer
调用和输出如下所示:
$ nosetests test_simple.py
F
======================================================================
FAIL: test_simple.test_rudimentary
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/<ENV>/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/home/<ENV>/lib/python2.7/site-packages/nose/twistedtools.py", line 165, in wrapper
% timeout)
TimeExpired: timeout expired before end of test (1.000000 s.)
----------------------------------------------------------------------
Ran 1 test in 1.001s
FAILED (failures=1)
如果我没有设置超时,测试会永远挂起。
答案 0 :(得分:2)
您的代码负责调用defer.callback / errback(最有可能是间接的)。试试:
reactor.callLater(0.1, defer.callback)
return defer