2016-11-22 9 views
0

竜巻でWeb認証のテストを書くようにしてください。 しかし、受信エラー:テスト中にエラーが発生する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コードである必要があります。self.stopself.waitを使用して伝統的な/レガシーモード、および@gen_testを使用して、新しいモード:

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は、2つのモードをサポートしています。あるモード用に設計された機能は、他のモードでは機能しません。 self.fetchは以前のモード用に設計されています。

このテストは2通りの方法で作成できます。まず、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を構築する必要があります。

+0

私はE Con​​nectionRefusedErrorを受け取りました:[Errno 10061]不明なエラー - 応答の行にあります。何をすべきか? – Serhiy

+0

localhost:8888でリッスンしていますか? –

+0

解決済み!助けてくれてありがとう!!! 1 – Serhiy

関連する問題