当使用Tornado AsyncHTTPTestCase运行测试时,我得到一个与测试无关的堆栈跟踪。测试正在通过,所以这可能是在测试清理时发生的?
我使用的是Python 2.7.2,Tornado 2.2。
测试代码为:
class AllServersHandlerTest(AsyncHTTPTestCase):
def get_app(self):
return Application([('/rest/test/', AllServersHandler)])
def test_server_status_with_advertiser(self):
on_new_host(None, '127.0.0.1')
response = self.fetch('/rest/test/', method='GET')
result = json.loads(response.body, 'utf8').get('data')
self.assertEquals(['127.0.0.1'], result)测试通过了,但是我从Tornado服务器得到了下面的堆栈跟踪。
OSError: [Errno 9] Bad file descriptor
INFO:root:200 POST /rest/serverStatuses (127.0.0.1) 0.00ms
DEBUG:root:error closing fd 688
Traceback (most recent call last):
File "C:\Python27\Lib\site-packages\tornado-2.2-py2.7.egg\tornado\ioloop.py", line 173, in close
os.close(fd)
OSError: [Errno 9] Bad file descriptor有什么办法干净利落地关闭测试用例吗?
发布于 2012-04-02 01:44:18
我深入研究了tornado代码,发现此代码将all_fds设置为True,这会导致io_loop.close()中的堆栈跟踪:
def tearDown(self):
if (not IOLoop.initialized() or
self.io_loop is not IOLoop.instance()):
# Try to clean up any file descriptors left open in the ioloop.
# This avoids leaks, especially when tests are run repeatedly
# in the same process with autoreload (because curl does not
# set FD_CLOEXEC on its file descriptors)
self.io_loop.close(all_fds=True)
super(AsyncTestCase, self).tearDown()因此,覆盖测试类中的tearDown()将停止堆栈跟踪。
class AllServersHandlerTest(AsyncHTTPTestCase):
def tearDown(self):
pass
def get_app(self):
return Application([('/rest/test/', AllServersHandler)])
def test_server_status_with_advertiser(self):
on_new_host(None, '127.0.0.1')
response = self.fetch('/rest/test/', method='GET')
result = json.loads(response.body, 'utf8').get('data')
self.assertEquals(['127.0.0.1'], result)我不确定这种方法会带来什么危害,所以如果其他人有更好的建议,请让我知道!
https://stackoverflow.com/questions/9966530
复制相似问题