Skip to content
Back to skills

Pytest Code Review

ASecurity

Reviews pytest test code for pytest-asyncio configuration, async resource cleanup, fixture scoping, parametrize usage, and mock/patch correctness. Use when reviewing test_*.py or conftest.py files.

  • 92 stars
  • 0 votes
  • 0 copies
  • 4 views
  • Added February 8, 2026
developmentnodetestingcode-review

Security analysis

A100/100

Pro scans all 5 files and shows the line behind each finding

Scanned November 26, 2026

npx -y skills add existential-birds/beagle --skill pytest-code-review --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Pytest Code Review?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Pytest Code Review
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/existential-birds-pytest-code-review/badge)](https://www.skillsdirectory.com/skills/existential-birds-pytest-code-review)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: pytest-code-review
description: Reviews pytest test code for pytest-asyncio configuration, async resource cleanup, fixture scoping, parametrize usage, and mock/patch correctness. Use when reviewing test_*.py or conftest.py files.
---

# Pytest Code Review

## Quick Reference

| Issue Type | Reference |
|------------|-----------|
| `pytest-asyncio` `strict`/`auto` mode, `AsyncMock`, task cleanup | [references/async-testing.md](references/async-testing.md) |
| `conftest.py`, `tmp_path`, fixture scope, async `try`/`finally` cleanup | [references/fixtures.md](references/fixtures.md) |
| `@pytest.mark.parametrize`, `pytest.param`, `indirect` fixtures | [references/parametrize.md](references/parametrize.md) |
| Call-site `patch()`, `AsyncMock`, `spec_set`, `mocker` | [references/mocking.md](references/mocking.md) |

## Review gates

Work in order. Do not assert pytest-specific problems until each applicable gate passes.

1. **Scoped files** — **Pass when:** You list every `test_*.py` and any `conftest.py` you will cite; no findings for files outside that list.
2. **Async vs sync** — **Pass when:** Per scoped file, you note whether it uses `async def test_*` / `await` and check project `asyncio_mode` (`strict` vs `auto`); if async, open [references/async-testing.md](references/async-testing.md) before criticizing async usage.
3. **Fixtures** — **Pass when:** If shared setup matters, you name the `conftest.py` path(s) or state none; for yield fixtures, confirm cleanup exists before claiming resource leaks.
4. **patch / mocks** — **Pass when:** For any `patch` or mock critique, you give the import path where the symbol is **used** (call site), or mark N/A; open [references/mocking.md](references/mocking.md) when mocking is central to the review.
5. **Findings** — **Pass when:** Each finding includes a file path and line(s) or test node id, not a generic rule restatement.

## Review Checklist

- [ ] Async tests match configured `pytest-asyncio` mode (`strict` requires `@pytest.mark.asyncio` + `@pytest_asyncio.fixture`; `auto` discovers `async def` tests/fixtures automatically)
- [ ] `asyncio_default_fixture_loop_scope` configured explicitly in `pyproject.toml` / `pytest.ini`
- [ ] `AsyncMock` used for async dependencies (not `Mock`) and all coroutines/async mocks awaited
- [ ] Background tasks and async resources cleaned up in `finally` blocks
- [ ] Fixture scope appropriate (`function` for mutable state, `module`/`session` only for immutable/isolated expensive setup)
- [ ] Yield fixtures wrap cleanup in `try`/`finally` and use `tmp_path` instead of hardcoded `/tmp` paths
- [ ] `@pytest.mark.parametrize` uses `pytest.param(..., id=...)` / `indirect=` cleanly for similar cases
- [ ] `patch()` targets where the symbol is **used** (imported/looked up), not where defined
- [ ] Mocks use `spec`/`spec_set` and verify exact call arguments when behavior depends on them
- [ ] Tests remain isolated (no shared module-level mock or mutable state)

## Valid Patterns (Do NOT Flag)

- **`assert` without custom message** — `pytest` rewrites `assert` statements to render detailed diffs automatically
- **`@pytest.fixture` without explicit scope** — Default `function` scope is correct for most fixtures, including those returning fresh mutable state per test
- **`monkeypatch` instead of `unittest.mock`** — Simpler `pytest`-native patching with automatic teardown
- **`async def test_*` without `@pytest.mark.asyncio` when `asyncio_mode = "auto"`** — Intentional in `auto` mode

## When to Load References

- Reviewing async test functions or `pytest-asyncio` config → `references/async-testing.md`
- Reviewing fixtures or `conftest.py` → `references/fixtures.md`
- Reviewing parametrized tests → `references/parametrize.md`
- Reviewing mocks and patches → `references/mocking.md`

## Review Questions

1. Does async test/fixture syntax match the project's `pytest-asyncio` `asyncio_mode` (`strict` vs `auto`)?
2. Are fixtures properly scoped with `try`/`finally` cleanup and `tmp_path`?
3. Can duplicated test cases be simplified with `@pytest.mark.parametrize`?
4. Do `patch()` calls target the call-site namespace, use `AsyncMock`/`spec_set`, and verify arguments accurately?

Files in this skill

  • SKILL.md1.9 KB
  • references/async-testing.md5.3 KB
  • references/fixtures.md6.9 KB
  • references/mocking.md10 KB
  • references/parametrize.md9.2 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.