- API単位のテストを自動化したい
- 以下は個人的な作業メモです。
インストール
pip install tavern
試しに使ってみる
Postman Echoに対してリクエストを送ってみます。
以下のyamlをtest_postman_echo.tavern.yaml
として保存する。
test_name: Testing Postman Echo stages: - name: Get Request request: url: https://postman-echo.com/get params: foo1: "bar1" foo2: "bar2" headers: Authorization: "hoge" method: GET response: strict: # response のheadersではリクエストで明示的に設定したHeader以外があってもOKにするため - json:off status_code: 200 json: args: foo1: "bar1" foo2: "bar2" headers: authorization: "hoge" url: "https://postman-echo.com/get?foo1=bar1&foo2=bar2"
そして、テストを実行してみると、出力は以下のような形で出力される。
tavern-ci.exe ./test_postman_echo.tavern.yaml ========================================================================================================================================== test session starts =========================================================================================================================================== platform win32 -- Python 3.10.1, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 rootdir: <path/to/rootdir> plugins: tavern-1.19.0 collected 1 item test_postman_echo.tavern.yaml . [100%] ============================================================================================================================================ warnings summary ============================================================================================================================================ test_postman_echo.tavern.yaml::Testing Postman Echo <frozen importlib._bootstrap>:283: DeprecationWarning: the load_module() method is deprecated and slated for removal in Python 3.12; use exec_module() instead -- Docs: https://docs.pytest.org/en/stable/warnings.html ====================================================================================================================================== 1 passed, 1 warning in 1.17s ======================================================================================================================================
使ってみて
- エラーの表示に癖があって、ちょっと読み解くのに慣れが必要そう
- 外部のyammlファイルを読み込むとエラーになる事象の解消。
- 外部関数の呼び出しを試す。