pytest-patterns

A guide to testing Python programs with pytest, a testing framework that discovers and runs tests by convention. It covers reusable setup fixtures, parameterised tests, markers, plugins, mocking, and advanced patterns.

In plain words
What is it for?
Use it to write, review, and debug unit or integration tests with fixtures, conftest files, parametrisation, markers, plugins, and mocks.
Why use it?
It helps keep Python tests consistent and reduces repeated setup code while making it easier to check the same behaviour with many inputs.

Skill for Claude CodeCodex

Part of the qa-essentials plugin — 10 skills shipped together

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add skills/pramoddutta/qaskills/pytest-patterns
Any agent
npx skills add PramodDutta/qaskills --skill pytest-patterns
Clone the repo
git clone --depth 1 https://github.com/PramodDutta/qaskills

Made for: Claude Code, Codex.

Or install qa-essentials, the plugin that ships this one along with the rest of its 10 skills.

Per session 31 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 3,467 The whole file, excluding the scripts and references it only reads on demand.
Security scan C 2 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00031 $0.03467
Opus 5 $0.00015 $0.01733
Sonnet 5 $0.00006 $0.00693
Haiku 4.5 $0.00003 $0.00347

Measured 3d ago against content hash a90db54d9325, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade C, and why

pytest-patterns scanned grade C with 2 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 3d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Harvests environment variableshighData exfiltration

Enumerating or grepping the environment for keys collects credentials unrelated to what the mod says it does.

original_env = os.environ.copy()

Makes network callslowCapability

Not a fault in itself. Listed so you know the mod talks to something, and to what.

mock_get = mocker.patch("requests.get")
packs/qa-essentials/skills/pytest-patterns/SKILL.md · 572 lines

How it starts

The opening of the file, as written. The whole thing — 572 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Pytest Patterns Skill

You are an expert Python developer specializing in testing with pytest. When the user asks you to write, review, or debug pytest tests, follow these detailed instructions.

Core Principles

  1. Convention over configuration -- pytest discovers tests automatically by naming conventions.
  2. Fixtures for setup -- Use fixtures instead of setUp/tearDown methods.
  3. Parametrize for coverage -- Use @pytest.mark.parametrize for data-driven tests.
  4. Descriptive test names -- Function names should describe the expected behavior.
  5. Minimal test scope -- Each test verifies one behavior.

Project Structure

project/
  src/
    myapp/
      __init__.py
      services/
        user_service.py
        order_service.py
      models/
        user.py
      utils/
        validators.py
  tests/
    __init__.py
    conftest.py
    unit/
      __init__.py
      test_user_service.py
      test_validators.py
    integration/
      __init__.py
      conftest.py
      test_user_api.py
    fixtures/
      user_fixtures.py
  pyproject.toml
  pytest.ini

Configuration

# pytest.ini
[pytest]
testpaths = tests
python_files = test_*.py
python_classes = Test*
python_functions = test_*
addopts = -v --tb=short --strict-markers
markers =
    slow: marks tests as slow (deselect with '-m "not slow"')
    integration: marks integration tests
    smoke: marks smoke tests
    unit: marks unit tests
# pyproject.toml
[tool.pytest.ini_options]
testpaths = ["tests"]
addopts = "-v --tb=short --strict-markers --cov=src --cov-report=term-missing"
markers = [
    "slow: marks tests as slow",
    "integration: marks integration tests",
    "smoke: marks smoke tests",
]

[tool.coverage.run]
source = ["src"]
omit = ["tests/*", "*/__init__.py"]

[tool.coverage.report]
fail_under = 80
show_missing = true

Fixtures

Basic Fixtures

# conftest.py
import pytest
from myapp.services.user_service import UserService
from myapp.models.user import User


@pytest.fixture
def sample_user():
    """Create a sample user for testing."""
    return User(
        id="user-123",
        email="[email protected]",
        name="Test User",
        role="user",
    )


@pytest.fixture
def admin_user():
    """Create an admin user for testing."""
    return User(
        id="admin-123",
        email="[email protected]",
        name="Admin User",
        role="admin",
    )


@pytest.fixture
def user_service(mock_user_repo, mock_email_service):
    """Create UserService with mocked dependencies."""
    return UserService(
        user_repo=mock_user_repo,
        email_service=mock_email_service,
    )

Read the full file on GitHub · 572 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 3d ago First seen · 572 lines · 31 tokens per session scan C a90db54d9325

Subscribe to this mod's changes

pytest-patterns is a skill published in the GitHub repository PramodDutta/qaskills (214 stars, last pushed 3d ago), licensed MIT. It adds 31 tokens to every session and 3,467 once invoked, about $0.0002 per session on Opus 5. A static security scan graded it C with 2 findings (harvests environment variables, makes network calls). No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.