Skip to content

Instantly share code, notes, and snippets.

@dogrunjp
Last active January 9, 2017 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dogrunjp/debb1aa8f92c7fc60ba0b48ba938c186 to your computer and use it in GitHub Desktop.
Save dogrunjp/debb1aa8f92c7fc60ba0b48ba938c186 to your computer and use it in GitHub Desktop.
Pythonのbottleで書いているAPIをテストするために、webTestを使ったテストツールを試してみました。unittestの基本的な構成のみですが、自分用メモ&公開しておきます。
import unittest
import app
import os
from webtest import TestApp
os.environ['WEBTEST_TARGET_URL'] = 'http://localhost:8080'
app = TestApp(app)
class ApiTest(unittest.TestCase):
def test_api_root(self):
res = app.get('/sra')
self.assertEqual(res.status, '200 OK')
self.assertEqual(res.content_type, 'application/json')
if __name__ == '__main__':
unittest.main()

PythonのbottleアプリでwebTestを使ったAPIのテストを行ってみる

## アプリケーションとテストの諸条件

  • 最小構成に近いテストスクリプトです。
  • アプリケーションはapp.pyで保存しています。
  • 同じディレクトリにテストアプリケーションのモジュールを置いて実行しています。
  • 実運用の環境ではmod_wsgiを利用しますが、テストはPycharmのプロジェクト内で実行しています。
  • WSGIProxy2が無いと怒られたので、virtualenv環境にpip install WSGIProxy2 しました。

少し戸惑ったのは、テストするアプリケーションを指定する方法ですが、

import apiモジュール名

で TestApp()の引数にモジュールを渡せば良いようです。

参考

ほぼ参考先のままです、、、

PythonのWebアプリをUnitTestするコードを書く

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment