スキル一覧に戻る
cfircoo

pytest-best-practices

by cfircoo

10🍴 1📅 2026年1月22日
GitHubで見るManusで実行

SKILL.md


name: pytest-best-practices description: Expert guidance for writing high-quality pytest tests. Use when writing tests, setting up fixtures, parametrizing, mocking, or reviewing test code.

<essential_principles>

Test Independence

  • Each test must run in isolation - no shared state between tests
  • Use fixtures for setup/teardown, never class-level mutable state
  • Tests should pass regardless of execution order

Naming Conventions

  • Files: test_*.py or *_test.py
  • Functions: test_<description>()
  • Classes: Test<ClassName>
  • Fixtures: descriptive lowercase_with_underscores

Directory Structure

tests/
├── conftest.py          # Shared fixtures
├── unit/
│   └── test_module.py
├── integration/
│   └── test_api.py
└── fixtures/            # Test data files

Core Testing Rules

  • Use plain assert statements (pytest provides detailed failure messages)
  • One logical assertion per test when practical
  • Test edge cases: empty inputs, boundaries, invalid data, errors
  • Keep tests focused and readable

</essential_principles>

<quick_reference>

PatternUse Case
@pytest.fixtureSetup/teardown, dependency injection
@pytest.mark.parametrizeRun test with multiple inputs
@pytest.mark.skipSkip test temporarily
@pytest.mark.xfailExpected failure (known bug)
pytest.raises(Exception)Test exception raising
pytest.approx(value)Float comparison
mocker.patch()Mock dependencies
conftest.pyShare fixtures across modules

Common Commands

pytest -v                    # Verbose
pytest -x                    # Stop on first failure
pytest --lf                  # Run last failed
pytest -k "pattern"          # Match test names
pytest -m "marker"           # Run marked tests
pytest --cov=src             # Coverage report

</quick_reference>

Based on what you're doing, read the relevant reference:

TaskReference
Setting up fixtures, scopes, factoriesreferences/fixtures.md
Parametrizing tests, multiple inputsreferences/parametrization.md
Mocking, patching, faking dependenciesreferences/mocking.md
Markers, exceptions, assertions, asyncreferences/patterns.md

スコア

総合スコア

60/100

リポジトリの品質指標に基づく評価

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

+10
説明文

100文字以上の説明がある

0/10
人気

GitHub Stars 100以上

0/15
最近の活動

3ヶ月以内に更新がある

0/10
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

+5
タグ

1つ以上のタグが設定されている

0/5

レビュー

💬

レビュー機能は近日公開予定です