Python メモ – REST APIのテスト
PythonでREST APIをテストするツールを探していたら、Tavern
なるものを見つけたのでメモ。
REST API
まず、テスト対象として、ユーザーを追加とユーザー取得のAPIを用意。
# app.py
from flask import Flask, request, jsonify
import json
from dataclasses import dataclass
app = Flask(__name__)
users = []
@app.route('/user/<int:id>', methods=['GET'])
def index(id):
return jsonify(users[id-1])
@app.route('/user', methods=['POST'])
def create():
users.append(User(request.json['name'], request.json['age']))
return jsonify({'id': len(users)})
@dataclass
class User:
name: str
age: str
APIサーバーを起動しておく。
$ export FLASK_APP=app.py
$ export FLASK_ENV=development
$ flask run
Tavernインストール
Tavern
とテスフレームワークのPytest
をインストール
$ pip install pytest
$ pip install tavern
テストケース作成
テストケースをyamlで記述する。ここではテストケースで共通で使う変数をcommon.yaml
に、テストケースをtest_user.tavern.yaml
に書いた。
common.yaml
このファイルにAPIの接続先を設定した。最初は、name
とdescription
を書いていなかったが、これらは必須であったためエラーになった。忘れずに書きましょう。
# common.yaml
name: Common test information
description: information for test server
variables:
protocol: http
host: localhost
port: 5000
test_user.tavern.yaml
このテストケースでは、最初にPOSTでユーザーを作成して、次に作成したユーザーをGETできるか検証している。
# tests/integration/test_user.tavern.yaml
test_name: add/get User
strict:
- body
includes:
- !include common.yaml
stages:
- name: add user
request:
url: "{protocol:s}://{host:s}:{port:d}/user"
method: POST
json:
name: test taro
age: 7
response:
status_code: 200
headers:
content-type: application/json
verify_response_with:
function: tavern.testutils.helpers:validate_pykwalify
extra_kwargs:
schema:
type: map
mapping:
id:
type: int
required: True
save:
body:
userid: id
- name: get user
request:
url: "{protocol:s}://{host:s}:{port:d}/user/{userid}"
method: GET
response:
status_code: 200
headers:
content-type: application/json
body:
name: test taro
age: 7
POST /user
- POSTでユーザー作成
- レスポンスの検証
- ステータスコードが
200
であること - HTTPヘッダーの
Content-Type
がapplication/json
であること - 応答メッセージの
id
がint
であること
- ステータスコードが
- 応答メッセージの
id
の値を変数userid
に格納
テスト実行
$ pytest
=================================== test session starts ===================================
platform darwin -- Python 3.8.0, pytest-4.5.0, py-1.8.0, pluggy-0.13.1
rootdir: /Users/nikutama/study/python-tavern
plugins: tavern-0.33.0
collected 1 item
tests/integration/test_user.tavern.yaml . [100%]
================================ 1 passed in 0.93 seconds =================================
今まではPostmanで手動で確認していたが、これでAPIの検証も自動化でき楽になる。