エンドポイントをテスト

Claude Code によるソフトウェア開発

Dani Zysman

AI Curriculum Manager @ DataCamp

総仕上げ

recraft: half: モニターで合格を示す緑色のテスト結果を見て喜ぶ開発者。チェックマークとデータベースのアイコンが添えられている

  • 第2章: 個々の関数の単体テスト
  • 第3章: MCP + Pydantic でエンドポイントを構築
  • 今回: 全体をエンドツーエンドでテスト

同じワークフロー:Claude にテストを書いてもらいましょう!

Claude Code によるソフトウェア開発

単体テスト vs 統合テスト

単体テストはモックを使って1つの関数を分離し、統合テストはリクエストからレスポンスまでの全体の流れを検証します

Claude Code によるソフトウェア開発

これまでのプロジェクト

music-analytics-api/
  app.py
  routes.py
  models.py
  db_routes.py         # Database-backed Blueprint
  chinook.db
  test_routes.py       # Unit tests (Ch.2)
  test_db_routes.py    # <- Integration tests
  CLAUDE.md
Claude Code によるソフトウェア開発

テストを書いて実行する

> Generate pytest integration tests for @db_routes.py and run them
Test Coverage

TestGetArtistAlbums: Core functionality
- Retrieval, sorting, 404s, invalid IDs

TestDatabaseConnection: Connection handling
- Path, closure on success and error

TestErrorHandling: 500 on database errors
TestResponseFormat: JSON format and field validation

Uses temporary SQLite database, independent of chinook.db

Ran all 23 tests: Passed!
Claude Code によるソフトウェア開発

練習しましょう!

Claude Code によるソフトウェア開発

Preparing Video For Download...