-
Notifications
You must be signed in to change notification settings - Fork 4
Tornadoのテスト方法
Yuki-Inamoto edited this page Jul 29, 2015
·
12 revisions
-
サンプルコード
-
実行メモ
tornado.testing.AsyncHTTPTestCaseを継承する。
継承したクラス内でsetup, get_app, get_http_portメソッドをoverrideする。
実装例
class TestBase(tornado.testing.AsyncHTTPTestCase):
def setUp(self):
super(TestBase, self).setUp()
def get_app(self):
settings = dict()
tornado.web.Application(handlers=url_patterns, **settings)
def get_http_port(self):
return options.port
その後同じクラス内,もしくはさらに継承したクラス内でですとコードを実装する。
テストコード例
def test_post_request_signup(self):
test_url = '/auth/signup/'
post_args = {'username': 'karateman',
'email': 'warrior@earth.co.jp',
'password': 'katyouhuugetu'
}
response = self.fetch(test_url, method='POST',
body=parse.urlencode(post_args),
follow_redirects=False)
self.assertEqual(response.code, 302)
self.assertTrue(
response.headers['Location'].startswith('/auth/login/'),
"response.headers['Location'] did not ends with /auth/login/")
POSTでpost_argsにセットされたユーザ情報をもとにユーザ登録を行う。正常ならばログイン画面にredirectされる
- TornadoでxsrfのフラグがTrue時のテスト方法
「tornado.test.web_test」 の 「XSRFTest」 をインポート
xsrfを考慮したログイン時のテスト例(一部)
post_args = {'email': 'test@test.co.jp',
'password': 'test',
'_xsrf': 'tomato'}
response = self.fetch(test_url, method='POST',
body=parse.urlencode(post_args),
follow_redirects=False,
headers=XSRFTest.cookie_headers(self, token='tomato'),
)
POSTで送るbody内の「_xsrf」とheadersの中の「token」をマッチングしている
ユーザログイン関係のテスト
様々な項目のテストコードが書かれている
mockというのを使うと良いらしいことが書かれている
-
ユニットテストで使われるもの
-
クラス間の依存関係を切り離すことができる
-
http://stackoverflow.com/questions/10853288/unit-testing-in-tornado
Unitテスト関連
- http://tornado.readthedocs.org/en/latest/testing.html
- http://www.tornadoweb.org/en/branch2.3/testing.html#tornado.testing.AsyncTestCase
その他