python-pytest-patterns

Guides authoring and reviewing idiomatic pytest tests with fixtures, parametrization, and async support.

Updated May 7, 2026
One-click install
npx skills add https://github.com/PremModhaOfficial/NFR-pipeline --skill python-pytest-patterns-premmodhaofficial
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: python-pytest-patterns
Source: https://github.com/PremModhaOfficial/NFR-pipeline/tree/main/skills/python-pytest-patterns
Command: npx skills add https://github.com/PremModhaOfficial/NFR-pipeline --skill python-pytest-patterns-premmodhaofficial

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Python test suites often accumulate unittest.TestCase classes, for-loops inside tests, mis-scoped fixtures that leak state, and unregistered markers that pollute CI logs. This Skill codifies idiomatic pytest patterns so tests are isolated, discoverable, and compatible with pytest-asyncio, pytest-benchmark, and testcontainers-based integration testing. ## Core Features & Use Cases - Fixture and scope guidance: Rules for function/module/session scopes, yield-based cleanup, conftest.py placement, and avoiding mutable session-scoped state leaks. - Parametrization and markers: pytest.param with stable ids, indirect fixture parametrization, marker registration in pyproject.toml, and skip/xfail/skipif semantics. - Async and assertion tooling: asyncio_mode=auto configuration, monkeypatch for environment isolation, caplog/capsys/capfd capture, pytest.raises with match, and fakes-over-mocks guidance. - Use Case: When reviewing a pull request that adds a for-loop inside a test or a unittest.TestCase subclass, apply this Skill to rewrite it with @pytest.mark.parametrize and plain assert, then register any custom markers in pyproject.toml. ## Quick Start Review the test file tests/test_client.py and convert any unittest.TestCase classes and for-loops into idiomatic pytest with parametrized cases and properly scoped fixtures.

Frequently Asked Questions about python-pytest-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I parametrize pytest tests instead of using a for-loop?▼

Use @pytest.mark.parametrize with pytest.param entries that include explicit id values, producing deterministic test IDs like test_is_valid[empty]. A for-loop stops at the first failure and hides remaining cases, while parametrize runs every case independently.

How do I choose pytest fixture scope for my tests?▼

Use the default function scope for anything mutable, module scope for expensive read-only setup, and session scope for testcontainers. A session-scoped fixture with mutable state, such as a client with an internal cache, can silently leak state across tests.

How do I run async tests with pytest-asyncio?▼

Set asyncio_mode = "auto" in pyproject.toml under [tool.pytest.ini_options], and every async def test_ runs without per-test decorators. Async fixtures use async def with yield, and strict mode is discouraged unless mixing sync and async tests.

Should I use unittest.mock or fakes for Python test doubles?▼

Prefer narrow in-memory fakes that implement real behavior and expose verifiable state, since consumers care about behavior rather than call patterns. Reserve unittest.mock.AsyncMock for narrow was-it-called assertions that a behavioral fake cannot express.

Why does pytest warn about unknown marks like @pytest.mark.slow?▼

Custom markers must be registered in pyproject.toml under [tool.pytest.ini_options] markers, otherwise pytest emits a PytestUnknownMarkWarning that taints CI logs. Registration also makes markers discoverable via pytest --markers and enables --strict-markers.

When should I use xfail instead of skip in pytest?▼

Use xfail when the test should run and is expected to fail, such as a known upstream bug; an unexpected pass surfaces as XPASS so you can remove the marker. Use skip when the test must not run at all, such as platform-specific tests gated by skipif.