A pre-commit / prek hook that restricts mocking in Python tests:
- Blocks:
unittest.mockimports and references.mocker.patchandmocker.patch.*usage from pytest-mock- All
monkeypatchpytest methods except those permitted below.
- Permits:
monkeypatch.chdirmonkeypatch.setenvmonkeypatch.delenvmonkeypatch.undomocker.Mock,mocker.spy, and other pytest-mock APIs besidespatch.
For HTTP tests, prefer recording requests with pytest-recording.
Check failures explain the policy to developers and LLM coding agents.
Ruff does not support custom lint plugins, so this hook adds these testing policies as a separate check alongside Ruff.
Requires Python 3.11 or newer. Dependencies are installed automatically. Choose one configuration format, replacing the revision with a release tag or commit SHA.
For .pre-commit-config.yaml (pre-commit or prek):
repos:
- repo: https://github.com/i-VRESSE/python-mock-hooks
rev: v0.2.0
hooks:
- id: python-mock-hooksFor prek.toml (prek):
[[repos]]
repo = "https://github.com/i-VRESSE/python-mock-hooks"
rev = "v0.2.0"
hooks = [{ id = "python-mock-hooks" }]Run prek run python-mock-hooks --all-files (or use pre-commit instead of prek).
Checks Python files under tests/ by default. For another layout, set
files on the hook:
- YAML:
files: ^(tests|test)/.*\.py$ - TOML:
files = '^(tests|test)/.*\.py$'
See CONTRIBUTING.md for development and implementation details.
Rule and rule tests adapted from protein-quest. Inspired by PFCCLab/ast-grep-pre-commit-mirror.