スキル一覧に戻る
yonatangross

pytest-advanced

by yonatangross

The Complete AI Development Toolkit for Claude Code — 159 skills, 34 agents, 20 commands, 144 hooks. Production-ready patterns for FastAPI, React 19, LangGraph, security, and testing.

29🍴 4📅 2026年1月23日
GitHubで見るManusで実行

SKILL.md


name: pytest-advanced description: Advanced pytest patterns including custom markers, plugins, hooks, parallel execution, and pytest-xdist. Use when implementing custom test infrastructure, optimizing test execution, or building reusable test utilities. context: fork agent: test-generator version: 1.0.0 tags: [pytest, testing, python, markers, plugins, xdist, 2026] author: OrchestKit user-invocable: false

Advanced Pytest Patterns

Master pytest's advanced features for scalable, maintainable test suites.

Overview

  • Building custom test markers for categorization
  • Writing pytest plugins and hooks
  • Configuring parallel test execution with pytest-xdist
  • Creating reusable fixture patterns
  • Optimizing test collection and execution

Quick Reference

Custom Markers

# pyproject.toml
[tool.pytest.ini_options]
markers = [
    "slow: marks tests as slow (deselect with '-m \"not slow\"')",
    "integration: marks tests requiring external services",
    "smoke: critical path tests for CI/CD",
]
import pytest

@pytest.mark.slow
def test_complex_analysis():
    result = perform_complex_analysis(large_dataset)
    assert result.is_valid

# Run: pytest -m "not slow"  # Skip slow tests
# Run: pytest -m smoke       # Only smoke tests

See custom-plugins.md for plugin development.

Parallel Execution (pytest-xdist)

[tool.pytest.ini_options]
addopts = ["-n", "auto", "--dist", "loadscope"]
@pytest.fixture(scope="session")
def db_engine(worker_id):
    """Isolate database per worker."""
    db_name = "test_db" if worker_id == "master" else f"test_db_{worker_id}"
    engine = create_engine(f"postgresql://localhost/{db_name}")
    yield engine

See xdist-parallel.md for distribution modes.

Factory Fixtures

@pytest.fixture
def user_factory(db_session) -> Callable[..., User]:
    """Factory fixture for creating users."""
    created = []

    def _create(**kwargs) -> User:
        user = User(**{"email": f"u{len(created)}@test.com", **kwargs})
        db_session.add(user)
        created.append(user)
        return user

    yield _create
    for u in created:
        db_session.delete(u)

Key Decisions

DecisionRecommendation
Parallel executionpytest-xdist with --dist loadscope
Marker strategyCategory (smoke, integration) + Resource (db, llm)
Fixture scopeFunction default, session for expensive setup
Plugin locationconftest.py for project, package for reuse
Async testingpytest-asyncio with auto mode

Anti-Patterns (FORBIDDEN)

# NEVER use expensive fixtures without session scope
@pytest.fixture  # WRONG - loads every test
def model():
    return load_ml_model()  # 5s each time!

# NEVER mutate global state
@pytest.fixture
def counter():
    global _counter
    _counter += 1  # WRONG - leaks between tests

# NEVER skip cleanup
@pytest.fixture
def temp_db():
    db = create_db()
    yield db
    # WRONG - missing db.drop()!

# NEVER use time.sleep (use mocking)
def test_timeout():
    time.sleep(5)  # WRONG - slows tests
  • unit-testing - Basic pytest patterns and AAA structure
  • integration-testing - Database and API testing patterns
  • property-based-testing - Hypothesis integration with pytest

References

Capability Details

custom-markers

Keywords: pytest markers, test categorization, smoke tests, slow tests Solves: Categorize tests, run subsets in CI, skip expensive tests

pytest-xdist

Keywords: parallel, xdist, distributed, workers, loadscope Solves: Run tests in parallel, worker isolation, optimize distribution

pytest-hooks

Keywords: hook, plugin, conftest, pytest_configure, collection Solves: Customize pytest behavior, add timing reports, reorder tests

fixture-patterns

Keywords: fixture, factory, async fixture, cleanup, scope Solves: Factory fixtures, async fixtures, ensure cleanup runs

parametrize-advanced

Keywords: parametrize, indirect, cartesian, pytest.param, xfail Solves: Test multiple scenarios, fixtures with params, expected failures

スコア

総合スコア

75/100

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

SKILL.md

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

+20
LICENSE

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

+10
説明文

100文字以上の説明がある

+10
人気

GitHub Stars 100以上

0/15
最近の活動

1ヶ月以内に更新

+10
フォーク

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

0/5
Issue管理

オープンIssueが50未満

+5
言語

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

+5
タグ

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

+5

レビュー

💬

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