手動の機能テストを作成する

FastAPI入門

Matt Eckerle

Software and Data Engineering Leader

機能テストとは?

システムテスト

  • 焦点: 個別のシステム操作
  • 目的: システム機能の検証
  • 範囲: エンドポイント
  • 環境: アプリ実行中の Python 環境
def test_read():
    response = client.get("/items/1")
    assert response.status_code == 200

機能テスト

  • 焦点: 統合されたシステム
  • 目的: システム全体の検証
  • 範囲: アプリケーション
  • 環境: アプリ実行中の Python 環境
def test_delete_then_read():
    response = client.delete("/items/1")
    assert response.status_code == 200
    response = client.get("/items/1")
    assert response.status_code == 404
FastAPI入門

テストのワークフロー

成功・失敗のテストワークフロー例を示す図

FastAPI入門

機能テストのワークフロー例

成功するワークフロー

  • 作成してから読み取り
  • 作成してから更新
  • 作成してから削除
  • ...

失敗するワークフロー

  • 作成せずに読み取り
  • 削除後に更新
  • 作成せずに削除
  • ...
FastAPI入門

機能テストスクリプト

  • テストフレームワーク外 ― 「手動テスト」
  • requests を使用
import requests
ENDPOINT = "http://localhost:8000/items"
# Create item "rock"
r = requests.post(ENDPOINT, json={"name": "rock"})
assert r.status_code == 200
# Get item rock
r = requests.get(ENDPOINT, json={"name": "rock"})
assert r.status_code == 200
  • 既知のアプリ状態に基づいてワークフローを構築
FastAPI入門

練習しましょう!

FastAPI入門

Preparing Video For Download...