CtrlK
BlogDocsLog inGet started
Tessl Logo
SkillPublic

pytest-best-practices

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

Quality

100%

Does it follow best practices?

Run evals on this skill

Adds up to 20 points to the overall score

View guide
SecuritybySnyk

Passed

No findings from the security scan

SKILL.md
Quality
Evals
Security

<essential_principles>

Test Independence

  • Each test must run in isolation - no shared state between tests
  • Use fixtures for setup/teardown, never class-level mutable state
  • Tests should pass regardless of execution order

Naming Conventions

  • Files: test_*.py or *_test.py
  • Functions: test_<description>()
  • Classes: Test<ClassName>
  • Fixtures: descriptive lowercase_with_underscores

Directory Structure

tests/
├── conftest.py          # Shared fixtures
├── unit/
│   └── test_module.py
├── integration/
│   └── test_api.py
└── fixtures/            # Test data files

Core Testing Rules

  • Use plain assert statements (pytest provides detailed failure messages)
  • One logical assertion per test when practical
  • Test edge cases: empty inputs, boundaries, invalid data, errors
  • Keep tests focused and readable

</essential_principles>

<quick_reference>

PatternUse Case
@pytest.fixtureSetup/teardown, dependency injection
@pytest.mark.parametrizeRun test with multiple inputs
@pytest.mark.skipSkip test temporarily
@pytest.mark.xfailExpected failure (known bug)
pytest.raises(Exception)Test exception raising
pytest.approx(value)Float comparison
mocker.patch()Mock dependencies
conftest.pyShare fixtures across modules

Built-in fixtures — reach for these before writing your own; pytest already isolates and reverts them per test.

FixtureUse Case
tmp_pathPer-test temporary directory (pathlib.Path)
monkeypatchEnv vars, attributes, sys.path, cwd — auto-reverted
capsysAssert on captured stdout/stderr
caplogAssert 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:

TaskReference
Setting up fixtures, scopes, factories, built-in fixturesreferences/fixtures.md
Parametrizing tests, multiple inputsreferences/parametrization.md
Mocking, patching, faking dependenciesreferences/mocking.md
Markers, exceptions, assertions, asyncreferences/patterns.md
```bash pip install pytest pytest-asyncio pytest-mock pytest-cov pytest-xdist ```
Repository

provectus/awos-recruitment

Path

registry/skills/pytest-best-practices/SKILL.md

Last updated
First committed

Is this your skill?

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.