# Pytest

> pytest conventions — fixtures, conftest.py, parametrize, markers, async tests, and coverage. Load when working with pytest test files or pyproject.toml test configuration. Use when this capability is needed.

- Skill: `tomevault-io/pytest-11` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add tomevault-io/pytest-11`
- Raw SKILL.md: https://api.skillmd.com/api/skills/tomevault-io/pytest-11/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: tomevault-io (https://skillmd.com/u/tomevault-io)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/tomevault-io/pytest-11

---


Full standards in [pytest.md](pytest.md). Always-on summary:

**File and function naming:**
- Files: `test_<module>.py` (prefix, not suffix) — pytest discovers these automatically
- Functions: `def test_<behaviour>()` — describe the behaviour, not the method
- Classes: `class TestUserService:` — group related tests; no `__init__` method

**Fixtures:**
- Declare with `@pytest.fixture` decorator in `conftest.py` — pytest injects them automatically by parameter name
- Use the narrowest scope needed: `function` (default) → `class` → `module` → `session`
- Yield fixtures for setup/teardown: `yield` provides the value; cleanup runs after

**Parametrize:**
- `@pytest.mark.parametrize` for testing multiple inputs — never copy-paste tests
- Use `ids=` for readable test names in the output

**Assertions:**
- Plain `assert` statements — pytest rewrites them for detailed output; e.g., `assert response.status_code == 200`
- `pytest.raises(ExcType)` for exception testing — use as context manager
- `pytest.approx()` for floating-point comparisons

**Output capture:**
- Use the `capsys` fixture to capture and assert on stdout/stderr — never write raw stdout in tests

**Never:**
- `unittest.TestCase` style in new pytest code — use plain functions and fixtures
- Raw stdout debugging — use `pytest -s` flag or proper logging
- Skip tests without a reason: `@pytest.mark.skip(reason='...')` required

**Related skills:** `backend/python-fastapi` (pytest-asyncio, TestClient), `database/sqlalchemy` (session fixture patterns)

---
> Source: [manikumarkv/devrunway-claude-plugin](https://github.com/manikumarkv/devrunway-claude-plugin) — distributed by [TomeVault](https://tomevault.io).
<!-- tomevault:4.0:skill_md:2026-05-22 -->

