尝试在龙卷风中编写针对Web身份验证的测试。但是收到错误:
C:\python3\lib\site-packages\tornado\testing.py:402: in fetch
return self.wait()
C:\python3\lib\site-packages\tornado\testing.py:323: in wait
self.io_loop.start()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <tornado.platform.select.SelectIOLoop object at 0x00B73B50>
def start(self):
if self._running:
raise RuntimeError("IOLoop is already running")
E RuntimeError:IOLoop已在运行
不知道该怎么办。需要帮助。代码如下:
import pytest
import tornado
from tornado.testing import AsyncTestCase
from tornado.testing import AsyncHTTPTestCase
from tornado.httpclient import AsyncHTTPClient
from tornado.httpserver import HTTPServer
from tests.commons.testUtils import TestUtils
from tornado.web import Application, RequestHandler
import urllib.parse
from handlers.authentication.restAuthHandlers import RESTAuthHandler
import app
class TestRESTAuthHandler(AsyncHTTPTestCase):
def get_app(self):
return app
@tornado.testing.gen_test
def test_http_fetch_login(self):
data = urllib.parse.urlencode(dict(username='user', password='123456'))
response = self.fetch("http://localhost:8888/web/auth/login", method="POST", body=data)
self.assertIn('http test', response.body)
AsyncHTTPTestCase支持两种模式:使用self.stop
和的传统/旧版模式self.wait
,以及使用的较新模式@gen_test
。为一种模式设计的功能在另一种模式下不起作用;self.fetch
是为前一种模式设计的。
您可以通过两种方式编写此测试。首先,使用self.fetch,就像您编写的一样,但是@gen_test
删除装饰器。其次,这是带有的版本@gen_test
:
@tornado.testing.gen_test
def test_http_fetch_login(self):
data = urllib.parse.urlencode(dict(username='user', password='123456'))
response = yield self.http_client.fetch("http://localhost:8888/web/auth/login", method="POST", body=data)
self.assertIn('http test', response.body)
区别在于使用yield self.http_client.fetch
而不是self.fetch
。该@gen_test
版本通常更“现代”,可让您以编写应用程序的方式编写测试,但是它有一个很大的缺点:您可以调用self.fetch('/')
,它会自动填充为测试启动的服务器的主机和端口。 ,但self.http_client.fetch
您必须构造完整的url。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句