2016-11-22 33 views
0

嘗試編寫龍捲風網絡認證測試。 但收到錯誤:運行測試時收到錯誤RuntimeError:IOLoop已在運行。該怎麼辦?

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) 
+0

刪除@ tornado.testing.gen_test它工作。發現信息在這裏:https://github.com/tornadoweb/tornado/issues/1154 – Serhiy

回答

2

AsyncHTTPTestCase支持兩種模式:使用self.stopself.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你必須構造完整的網址。

+0

我收到E Con​​nectionRefusedError:[Errno 10061]未知的錯誤 - 在響應線。該怎麼辦? – Serhiy

+0

本地主機上正在監聽:8888? –

+0

解決了!謝謝你的幫助!!! 1 – Serhiy

相關問題