2015-10-26 98 views
1

嘗試在Python 3.4,Tornado版本4.1上使用AsyncHTTPTestCase測試POST請求。AsyncHTTPTestCase發送請求傳遞數據

from tornado.testing import AsyncHTTPTestCase 
from myapp import myclass 

class TestFooHandler(AsyncHTTPTestCase): 

    def get_app(self): 
     return myclass.application 

    def test_post_handler(self): 
     import urllib.parse 
     post_body = urllib.parse.urlencode({"key":"val"}) 
     response = self.fetch("/foo", method="POST", data=post_body) 
     self.assertEqual(response.code, 200) 

的代碼失敗消息: unexpected keyword argument 'data'

下面是完整的跟蹤:

Traceback (most recent call last): 
    File "/usr/local/lib/python3.4/dist-packages/tornado/testing.py", line 120, in __call__ 
    result = self.orig_method(*args, **kwargs) 
    File "/home/gub/App/unit_tests/test_cors.py", line 22, in test_post_handler 
    response = self.fetch("/foo", method="POST", data=post_body) 
    File "/usr/local/lib/python3.4/dist-packages/tornado/testing.py", line 380, in fetch 
    self.http_client.fetch(self.get_url(path), self.stop, **kwargs) 
    File "/usr/local/lib/python3.4/dist-packages/tornado/httpclient.py", line 227, in fetch 
    request = HTTPRequest(url=request, **kwargs) 
TypeError: __init__() got an unexpected keyword argument 'data' 

什麼是不正確的與上面的代碼?

回答

2

參數名稱是body,而不是dataself.fetch("/foo", method="POST", body=post_body)