11# pytest-do-not-mock
22
3- A pytest plugin that prevents mocking of specific classes and functions .
3+ Pytest plugin to prevent mocking of critical functions in tests .
44
55## Installation
66
77``` bash
88pip install pytest-do-not-mock
99```
1010
11+ The plugin activates automatically once installed — no configuration needed.
12+
13+ ## Usage
14+
15+ ### Block all mocking in a test
16+
17+ Use ` @pytest.do_not_mock ` with no arguments to prevent any mocking:
18+
19+ ``` python
20+ import pytest
21+
22+ @pytest.do_not_mock
23+ def test_payment_integration ():
24+ """ This test must use real implementations, no mocks allowed."""
25+ result = process_payment(100.0 )
26+ assert result is True
27+ ```
28+
29+ Any attempt to use ` Mock() ` , ` MagicMock() ` , ` patch() ` , or similar will raise ` DoNotMockError ` .
30+
31+ ### Protect specific functions
32+
33+ Pass functions (or string paths) to only block mocking of those targets:
34+
35+ ``` python
36+ from myapp import process_payment, send_email
37+
38+ @pytest.do_not_mock (process_payment)
39+ def test_selective ():
40+ """ process_payment cannot be mocked, but other functions can."""
41+ with patch(" myapp.send_email" ): # this is fine
42+ result = process_payment(100.0 )
43+ assert result is True
44+ ```
45+
46+ Multiple functions and string paths are supported:
47+
48+ ``` python
49+ @pytest.do_not_mock (process_payment, validate_user)
50+ def test_multiple ():
51+ ...
52+
53+ @pytest.do_not_mock (" myapp.payments.charge" )
54+ def test_string_path ():
55+ ...
56+ ```
57+
58+ ### What gets blocked
59+
60+ ** No-args mode** (` @pytest.do_not_mock ` ):
61+ - ` Mock() ` , ` MagicMock() ` , ` AsyncMock() `
62+ - ` patch() ` as decorator, context manager, or ` start() ` /` stop() `
63+ - ` patch.object() ` , ` patch.dict() `
64+ - ` create_autospec() `
65+
66+ ** Targeted mode** (` @pytest.do_not_mock(func) ` ):
67+ - ` patch() ` targeting the protected function
68+ - ` patch.object() ` targeting the protected function
69+ - Other mocking is allowed
70+
71+ ## Testing
72+
73+ ``` bash
74+ tox
75+ ```
76+
77+ Runs tests across Python 3.10–3.13, plus ruff linting and type checking (mypy + pyright).
78+
79+ ``` bash
80+ tox -e py313 # single Python version
81+ tox -e linting # ruff check + format
82+ tox -e typing # mypy + pyright
83+ pytest tests/ -v # run tests directly
84+ ```
85+
1186## Development
1287
1388``` bash
@@ -16,5 +91,19 @@ cd pytest-do-not-mock
1691python3 -m venv .venv
1792source .venv/bin/activate
1893pip install -e " .[dev]"
19- pytest
2094```
95+
96+ ### Project structure
97+
98+ ```
99+ src/pytest_do_not_mock/
100+ ├── __init__.py # Public API: do_not_mock, DoNotMockError
101+ └── plugin.py # Decorator + pytest hooks
102+
103+ tests/
104+ └── test_do_not_mock.py
105+ ```
106+
107+ ## License
108+
109+ MIT
0 commit comments