pytest-patterns

A collection of recommended patterns for writing Python tests with pytest, a tool for running automated Python tests.

In plain words
What is it for?
Use it when creating or improving pytest tests, especially fixtures, parameterized tests, markers, cleanup, and test naming.
Why use it?
It gives a consistent way to organize test files, shared setup, repeated test cases, and assertions.

Skill for Claude CodeCodex

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/vladkesler/initrunner/pytest-patterns
Any agent
npx skills add vladkesler/initrunner --skill pytest-patterns
Clone the repo
git clone --depth 1 https://github.com/vladkesler/initrunner

Made for: Claude Code, Codex.

Per session 24 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 807 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 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.00024 $0.00807
Opus 5 $0.00012 $0.00404
Sonnet 5 $0.00005 $0.00161
Haiku 4.5 $0.00002 $0.00081

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

Security

Grade A, and why

pytest-patterns scanned grade A with 0 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 2d 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.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

examples/roles/unit-tester/skills/pytest-patterns/SKILL.md · 90 lines

How it starts

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

Pytest patterns and best practices for Python test suites.

When to activate

Use this skill when the project has pyproject.toml, setup.cfg, pytest.ini, conftest.py, or a tests/ directory containing *_test.py or test_*.py files.

Test file structure

  • Name test files test_<module>.py to mirror the source module they cover.
  • Group related tests inside a class (class TestParseConfig:) when they share setup or conceptually belong together.
  • Name test functions test_<behavior_under_test> -- the name should read like a specification (e.g. test_parse_config_raises_on_missing_key).

Fixtures

  • Place shared fixtures in conftest.py so they are automatically discovered by pytest.
  • Scope controls lifetime: function (default, one per test), class, module, session.
  • Use yield fixtures for setup/teardown -- code after yield runs as cleanup even if the test fails.
  • Fixture factories (a fixture that returns a callable) let tests customize object creation per-call.
  • Parametrized fixtures (@pytest.fixture(params=[...])) run every dependent test once per parameter value.

Parametrize

  • @pytest.mark.parametrize("x,expected", [(1, 2), (3, 6)]) runs the test once per tuple.
  • Multiple @pytest.mark.parametrize decorators stack -- pytest generates the cartesian product.
  • Use indirect=True to route parameter values through a fixture before they reach the test.

Markers

  • @pytest.mark.slow -- tag long-running tests so they can be skipped with pytest -m "not slow".
  • @pytest.mark.skip(reason="...") -- unconditionally skip.
  • @pytest.mark.xfail(reason="...") -- expect failure; test still runs but does not fail the suite.
  • Register custom markers in pyproject.toml under [tool.pytest.ini_options] to avoid warnings.
  • Filter with pytest -m "marker_name".

Assertions

  • Use plain assert -- pytest rewrites it to show detailed diffs on failure.
  • with pytest.raises(ValueError, match=r"expected .* pattern"): for exception type and message verification.
  • pytest.approx(0.1 + 0.2, abs=1e-9) for floating-point comparisons.
  • Use match= on pytest.raises to verify the error message, not just the exception type.

Read the full file on GitHub · 90 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. 2d ago First seen · 90 lines · 24 tokens per session scan A aca9c14195dc

Subscribe to this mod's changes

pytest-patterns is a skill published in the GitHub repository vladkesler/initrunner (41 stars, last pushed 4d ago), licensed Apache-2.0. It adds 24 tokens to every session and 807 once invoked, about $0.0001 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.