pytest_unit_test_rules

A set of rules for writing unit tests with pytest, a Python testing tool. It covers test names, imports, mocking, file size, coverage, and asynchronous code.

In plain words
What is it for?
Use it when adding or changing Python unit tests, fixtures, mocks, async tests, or test file organization.
Why use it?
It gives tests a consistent structure and helps ensure success cases, errors, and boundary conditions are checked without calling outside services directly.

Cursor rule for Cursor

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/modelengine-group/nexent/pytest_unit_test_rules
Clone the repo
git clone --depth 1 https://github.com/ModelEngine-Group/nexent

Made for: Cursor.

Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 394 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.00000 $0.00394
Opus 5 $0.00000 $0.00197
Sonnet 5 $0.00000 $0.00079
Haiku 4.5 $0.00000 $0.00039

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

Security

Grade A, and why

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

.cursor/rules/pytest_unit_test_rules.mdc · 52 lines

What it actually says

Pytest Unit Test Rules (Concise)

Framework

  • Use pytest exclusively; prefer fixtures; use pytest assert statements.

Naming

  • Files test_*; classes Test*; functions test_*.

Imports

  • Order: standard library, third‑party, project.
  • Import only the unit under test; mock collaborators using pytest-mock.

Import and Mock Rules

  • Do not directly import external interfaces/clients/services into tests to exercise collaborators.
  • Patch where the dependency is imported (lookup site) using a fully‑qualified path.
  • Use side_effect to cover error paths when appropriate.
from pytest_mock import MockFixture
from backend.apps.some_app import create_resource

def test_create_resource_success(mocker: MockFixture):
    mocker.patch(
        "backend.services.model_provider_service.ModelProviderService.create",
        return_value={"id": "res-1"},
    )
    assert create_resource({...})["id"] == "res-1"

Structure and Size

  • Keep files under 500 lines or split by feature; include __init__.py in split directories; use test_<module>_<feature>.py names.

Coverage and Async

  • Cover success/error flows and boundaries; use @pytest.mark.parametrize for variants.
  • Use @pytest.mark.asyncio for async tests.

Isolation

  • Use autouse=True fixtures to reset state; fully mock external I/O and APIs.

Checklist

  • pytest only (no unittest)
  • naming conventions followed
  • collaborators mocked with pytest-mock
  • import/mocking rules followed
  • async tests decorated when needed
  • clear, specific assertions
  • adequate coverage (normal and exception paths)
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 · 52 lines · 0 tokens per session scan A f51c66663f80

Subscribe to this mod's changes

pytest_unit_test_rules is a cursor rule published in the GitHub repository ModelEngine-Group/nexent (5,841 stars, last pushed 3d ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 394 tokens. 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.