Receiving error whe run test RuntimeError: IOLoop is already running. What to do?
Receiving error whe run test RuntimeError: IOLoop is already running. What to do?
尝试为tornado 中的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 is already running
不知道该怎么办。需要help.here是代码:
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
的 traditional/legacy 模式,以及使用 @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
版本主要是 "modern" 并且允许您像编写应用程序一样编写测试,但它有一个很大的缺点:您可以调用 self.fetch('/')
并且它会自动填充为测试启动的服务器的主机和端口,但在 self.http_client.fetch
中,您必须构建完整的 url。
尝试为tornado 中的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 is already running
不知道该怎么办。需要help.here是代码:
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
的 traditional/legacy 模式,以及使用 @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
版本主要是 "modern" 并且允许您像编写应用程序一样编写测试,但它有一个很大的缺点:您可以调用 self.fetch('/')
并且它会自动填充为测试启动的服务器的主机和端口,但在 self.http_client.fetch
中,您必须构建完整的 url。