The problem: flaky tests. They erode trust, waste time, and make CI a nightmare.
The solution: enforce hermeticity by category. Small tests can't access the network, filesystem, or databases. Medium tests are limited to localhost. Each size has time limits (1s, 5min, 15min).
When a test violates its constraints, you get an actionable error:
[TC001] Network Access Violation
Test: test_api.py::test_fetch_user [SMALL]
What happened:
Attempted network connection to api.example.com:443
To fix this:
• Mock with responses/httpretty/respx
• Use dependency injection
• Change to @pytest.mark.medium
Mocking libraries (responses, pyfakefs) still work - they intercept before the blocking layer.The goal is maintaining a healthy test pyramid: 80% small, 15% medium, 5% large.
GitHub: https://github.com/mikelane/pytest-test-categories Docs: https://pytest-test-categories.readthedocs.io PyPI: https://pypi.org/project/pytest-test-categories/