Pytest conventions for Python test suites — fixtures and scopes, parametrization, mocking with pytest-mock, built-in fixtures (`tmp_path`, `monkeypatch`, `capsys`, `caplog`), markers, exception testing, and async tests. Use when writing, reviewing, or refactoring Python tests, `conftest.py` fixtures, or pytest configuration in `pytest.ini` / `pyproject.toml`. Does not cover general Python syntax or typing — see modern-python-development — nor FastAPI `TestClient` and app-level test setup — see fastapi-best-practices.
80
100%
Does it follow best practices?
Run evals on this skill
Adds up to 20 points to the overall score
Passed
No findings from the security scan
<essential_principles>
Test Independence
Naming Conventions
test_*.py or *_test.pytest_<description>()Test<ClassName>lowercase_with_underscoresDirectory Structure
tests/
├── conftest.py # Shared fixtures
├── unit/
│ └── test_module.py
├── integration/
│ └── test_api.py
└── fixtures/ # Test data filesCore Testing Rules
assert statements (pytest provides detailed failure messages)</essential_principles>
<quick_reference>
| Pattern | Use Case |
|---|---|
@pytest.fixture | Setup/teardown, dependency injection |
@pytest.mark.parametrize | Run test with multiple inputs |
@pytest.mark.skip | Skip test temporarily |
@pytest.mark.xfail | Expected failure (known bug) |
pytest.raises(Exception) | Test exception raising |
pytest.approx(value) | Float comparison |
mocker.patch() | Mock dependencies |
conftest.py | Share fixtures across modules |
Built-in fixtures — reach for these before writing your own; pytest already isolates and reverts them per test.
| Fixture | Use Case |
|---|---|
tmp_path | Per-test temporary directory (pathlib.Path) |
monkeypatch | Env vars, attributes, sys.path, cwd — auto-reverted |
capsys | Assert on captured stdout/stderr |
caplog | Assert on captured log records |
Common Commands
pytest -v # Verbose
pytest -x # Stop on first failure
pytest --lf # Run last failed
pytest -k "pattern" # Match test names
pytest -m "marker" # Run marked tests
pytest --cov=src # Coverage report
pytest -n auto # Parallel across CPU cores (pytest-xdist)</quick_reference>
Based on what you're doing, read the relevant reference:
| Task | Reference |
|---|---|
| Setting up fixtures, scopes, factories, built-in fixtures | references/fixtures.md |
| Parametrizing tests, multiple inputs | references/parametrization.md |
| Mocking, patching, faking dependencies | references/mocking.md |
| Markers, exceptions, assertions, async | references/patterns.md |
provectus/awos-recruitment
ce6a3ea
If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.