pytest

A set of guidelines for pytest, a Python tool that runs automated tests. It covers test files, fixtures for shared setup, data-driven tests, markers, and test organization.

In plain words
What is it for?
Use it when creating pytest tests, arranging setup and cleanup, testing many inputs with parametrization, grouping tests, and marking slow or special cases.
Why use it?
It helps make Python tests easy to find, read, reuse, and run across different test cases and environments.

Cursor rule

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 rules/nedcodes-ok/cursor-doctor/pytest
Clone the repo
git clone --depth 1 https://github.com/nedcodes-ok/cursor-doctor
Per session 529 This file is loaded in full into every session.
When invoked 529 The same file — it is already loaded in full.
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.00529 $0.00529
Opus 5 $0.00264 $0.00264
Sonnet 5 $0.00106 $0.00106
Haiku 4.5 $0.00053 $0.00053

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

Security

Grade A, and why

pytest 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.

pro-kit/templates/tools/pytest.mdc · 47 lines

How it starts

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

pytest Cursor Rules

You are an expert in pytest testing. Follow these rules:

Structure

  • Name test files test_*.py or test.py. Name test functions test
  • Group related tests in classes (class TestUserAuth:) — no init needed
  • Use descriptive names: test_login_fails_with_expired_token over test_login_3
  • One assertion concept per test — multiple asserts are fine if testing one behavior

Fixtures

  • Define fixtures in conftest.py for shared setup — pytest discovers them automatically
  • Use @pytest.fixture with appropriate scope: function (default), class, module, session
  • Yield fixtures for setup/teardown: yield resource then cleanup after
  • Use factory fixtures (fixture that returns a factory function) for parameterized setup
  • Request fixtures by name in test parameters — no imports needed

Parametrize

  • Use @pytest.mark.parametrize("input,expected", [...]) for data-driven tests
  • Use ids parameter for readable test names: ids=["empty", "single", "overflow"]
  • Stack multiple parametrize decorators for combinatorial testing
  • Use pytest.param(..., marks=pytest.mark.xfail) for expected failures in param sets

Markers

  • Use @pytest.mark.slow, @pytest.mark.integration — register in pytest.ini/pyproject.toml
  • Run subsets with -m "not slow" or -m integration
  • Use @pytest.mark.skipif(condition, reason="...") over bare skip
  • Use @pytest.mark.usefixtures("db") when you need the fixture but not its value

Assertions & Patterns

  • Use plain assert — pytest rewrites them for detailed failure output
  • Use pytest.raises(ExceptionType, match="pattern") as context manager
  • Use pytest.approx() for float comparisons: assert result == pytest.approx(3.14, abs=1e-2)
  • Use tmp_path fixture for temp files, monkeypatch for env vars and attributes

Configuration

  • Configure in pyproject.toml [tool.pytest.ini_options] — not pytest.ini or setup.cfg
  • Set testpaths, markers, addopts (e.g., -ra --strict-markers)
  • Use --strict-markers to catch typos in marker names
  • Use conftest.py at project root for shared fixtures, in subdirs for scoped ones

Read the full file on GitHub · 47 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 · 47 lines · 529 tokens per session scan A b4e15391035b

Subscribe to this mod's changes

pytest is a cursor rule published in the GitHub repository nedcodes-ok/cursor-doctor (9 stars, last pushed 5mo ago), licensed MIT. It adds 529 tokens to every session, about $0.0026 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-31.