スキル一覧に戻る
eyadsibai

pytest

by eyadsibai

0🍴 0📅 2026年1月15日
GitHubで見るManusで実行

SKILL.md


name: Pytest description: This skill should be used when the user asks about "pytest", "writing tests", "test fixtures", "parametrize", "mocking", "test coverage", "conftest" version: 1.0.0

Pytest Testing

Guidance for writing effective tests with pytest.

Core Concepts

Fixtures

Reusable test setup/teardown. Use yield for cleanup.

Scopes:

ScopeRunsUse Case
functionEach testDefault, isolated
classOnce per classShared state in class
moduleOnce per fileExpensive setup
sessionOnce totalDB connections, servers

Key concept: conftest.py makes fixtures available to all tests in directory.


Parametrize

Test multiple inputs without repeating code.

Key concept: Generates separate test for each parameter set - failures are isolated.

Use pytest.param() for:

  • Custom test IDs
  • Expected failures (marks=pytest.mark.xfail)
  • Conditional skips

Markers

Tag tests for filtering and special behavior.

Built-inPurpose
@pytest.mark.skipAlways skip
@pytest.mark.skipif(condition)Conditional skip
@pytest.mark.xfailExpected failure
@pytest.mark.asyncioAsync test (with pytest-asyncio)

Custom markers: Define in pyproject.toml, run with pytest -m "marker"


Mocking

ToolUse Case
Mock()Create fake object
patch()Replace import
MagicMock()Mock with magic methods
AsyncMock()Mock async functions

Key concept: Patch where the thing is used, not where it's defined.


Test Organization

tests/
├── conftest.py          # Shared fixtures
├── unit/                # Fast, isolated
├── integration/         # Multiple components
└── e2e/                 # Full system

Coverage

Key thresholds:

  • 80%+ is good coverage
  • 100% is often overkill
  • Branch coverage catches conditionals

Config in pyproject.toml: Set source, branch=true, fail_under


Common Commands

CommandPurpose
pytest -vVerbose output
pytest -xStop on first failure
pytest -k "pattern"Filter by name
pytest -m "marker"Filter by marker
pytest -sShow print output
pytest -n autoParallel (pytest-xdist)
pytest --cov=srcWith coverage

Best Practices

PracticeWhy
One assertion per testClear failures
Descriptive test namesSelf-documenting
Arrange-Act-AssertConsistent structure
Test behavior, not implementationRefactor-proof
Use factories for test dataMaintainable
Keep tests fastRun often

Resources

スコア

総合スコア

50/100

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

SKILL.md

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

+20
LICENSE

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

0/10
説明文

100文字以上の説明がある

0/10
人気

GitHub Stars 100以上

0/15
最近の活動

3ヶ月以内に更新がある

0/10
フォーク

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

0/5
Issue管理

オープンIssueが50未満

+5
言語

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

+5
タグ

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

0/5

レビュー

💬

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