tdd-pytest について
tdd-pytestは、Pythonプロジェクト向けのテストスキルです。Red-Green-Refactorに沿ったpytestテストの作成と監査に加え、テスト実行、カバレッジ確認、設定、レポート生成を支援します。
主な機能
- 01Red-Green-Refactorでテストを作成
- 02pytestテストの品質を監査
- 03テスト実行とカバレッジ確認
- 04pyproject.tomlでpytestを設定
- 05TESTING_REPORT.local.mdを生成
活用シーン
- 01新機能の実装前に失敗するテストを作成
- 02既存のpytestファイルを監査
- 03テストを実行してカバレッジを確認
- 04テスト結果と監査提案をまとめる
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ 89jobrien/ steve --skill tdd-pytestインストール後、Claude Code で/tdd-pytestを入力して読み込まれたか確認します。
詳細
TDD-Pytest スキル
次のような支援が必要な場合に、このスキルを有効にします。
- TDD の手法(Red-Green-Refactor)を使ったテストの作成
- 既存の pytest テストファイルの品質監査
- カバレッジを測定しながらのテスト実行
TESTING_REPORT.local.mdへのテストレポート生成pyproject.tomlでの pytest 設定
TDD ワークフロー
Red-Green-Refactor サイクル
-
RED - まず失敗するテストを書く
- 適切な理由で失敗すること(インポートエラーではないこと)
- テストは最小限で、焦点を絞ること
- 失敗したテストの出力を表示する
-
GREEN - テストに合格する最小限のコードを書く
- テストに合格するために必要なものだけを実装する
- 時期尚早な最適化はしない
- 合格したテストの出力を表示する
-
REFACTOR - テストを合格状態に保ちながらコードを改善する
- 重複を整理する
- 命名を改善する
- 必要に応じて関数やクラスを抽出する
- 変更のたびにテストを実行する
テストの構成
ファイル構成
project/
src/
module.py
tests/
conftest.py # Shared fixtures
test_module.py # Tests for module.py
pyproject.toml # Pytest configuration
命名規則
- テストファイル:
test_*.pyまたは*_test.py - テスト関数:
test_* - テストクラス:
Test* - フィクスチャ:説明的な名前(
mock_database、sample_user)
Pytest のベストプラクティス
フィクスチャ
import pytest
@pytest.fixture
def sample_config():
return {"key": "value"}
@pytest.fixture
def mock_client(mocker):
return mocker.MagicMock()
パラメーター化
@pytest.mark.parametrize("input,expected", [
("hello", "HELLO"),
("world", "WORLD"),
("", ""),
])
def test_uppercase(input, expected):
assert input.upper() == expected
非同期テスト
import pytest
@pytest.mark.asyncio
async def test_async_function():
result = await async_operation()
assert result == expected
例外のテスト
def test_raises_value_error():
with pytest.raises(ValueError, match="invalid input"):
process_input(None)
テストの実行
uv を使用する場合
uv run pytest # Run all tests
uv run pytest tests/test_module.py # Run specific file
uv run pytest -k "test_name" # Run by name pattern
uv run pytest -v --tb=short # Verbose with short traceback
uv run pytest --cov=src --cov-report=term # With coverage
よく使うフラグ
-v/--verbose- 詳細な出力-x/--exitfirst- 最初の失敗で停止--tb=short- トレースバックを短く表示--tb=no- トレースバックを表示しない-k EXPR- 式に一致するテストを実行-m MARKER- マーカーが付いたテストを実行--cov=PATH- 指定したパスのカバレッジを測定--cov-report=term-missing- 未カバーの行を表示
pyproject.toml の設定
最小限の設定
[tool.pytest.ini_options]
asyncio_mode = "auto"
testpaths = ["tests"]
完全な設定
[tool.pytest.ini_options]
asyncio_mode = "auto"
asyncio_default_fixture_loop_scope = "function"
testpaths = ["tests"]
python_files = ["test_*.py", "*_test.py"]
python_functions = ["test_*"]
python_classes = ["Test*"]
addopts = "-v --tb=short"
markers = [
"slow: marks tests as slow",
"integration: marks integration tests",
]
filterwarnings = [
"ignore::DeprecationWarning",
]
[tool.coverage.run]
source = ["src"]
branch = true
omit = ["tests/*", "*/__init__.py"]
[tool.coverage.report]
exclude_lines = [
"pragma: no cover",
"if TYPE_CHECKING:",
"raise NotImplementedError",
]
fail_under = 80
show_missing = true
レポートの生成
TESTING_REPORT.local.md ファイルには、次の内容を含めます。
- テスト実行の概要(成功/失敗/スキップ)
- モジュールごとのカバレッジ指標
- 重大度別の監査結果
- ファイル名と行番号を参照した推奨事項
- エビデンス(コマンドの出力)
会話との連携
ユーザーがテストの作成を依頼した場合:
- 何をテストするかに関する会話履歴を確認する
- 話題になっているコードや機能を特定する
- 不明確な場合は、確認の質問をする:
- 「具体的にどのような動作をテストすればよいですか?」
- 「X のエッジケースも含めますか?」
- 「ユニットテスト、統合テスト、または両方が必要ですか?」
- TDD に従い、まず失敗するテストを書いてから実装する
利用可能なコマンド
/tdd-pytest:init- pytest の設定を初期化する/tdd-pytest:test [path]- TDD を使ってテストを書く(コンテキストを考慮)/tdd-pytest:test-all- すべてのテストを実行する/tdd-pytest:report- TESTING_REPORT.local.md を生成/更新する
コメント
Markdown 対応