Plugin adoption proposal: pytest-do-not-mock — enforce "no mocks" contracts in tests #14403
Unanswered
LifeLex
asked this question in
Show and tell
Replies: 1 comment 5 replies
-
|
I like the idea itself Markers and hooks should be used instead of the decorators that returns a wrapper |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
TL;DR
I maintain
pytest-do-not-mock, a small pytest plugin that lets authors mark tests as "no mocks allowed" (globally, or for specific functions) and fails the test ifunittest.mockis used to violate that contract. I'd like to propose moving it under thepytest-devGitHub organization for shared maintenance and discoverability, and I'm looking for feedback from the maintainers on whether the idea and the implementation are a good fit.Motivation
The plugin is a direct response to Increase Test Fidelity By Avoiding Mocks (Google Testing Blog, Feb 2024) and the "Test Doubles" chapter of Software Engineering at Google. The short version of the argument those sources make:
What it does
Two usage modes, both opt-in via the standard
@pytest.markmechanism:unittest.mockconstruction and patching for the duration of that test.protect=) or string paths (positional args) and only blockspatch/patch.objectaimed at those targets. Other mocking is untouched.@pytest.mark.do_not_mockon a class applies to all methods.pytestmark = pytest.mark.do_not_mockapplies to an entire module.Violations raise
DoNotMockErrorwith a message pointing at the offending call, so the failure is actionable without digging through tracebacks.Why it could be a good fit for pytest-dev
pytest-mockorunittest.mock@pytest.mark,iter_markers, andhookwrapper— no custom decorators or namespace injection. Markers stack across function/class/module scope; the plugin walks everydo_not_mockmarker on the item and unions them, with a bare marker at any scope winning over targeted inner markers.pytest>=7.0.Current state
ruff,mypy --strict, andpyright --strictall green.pytest-do-not-mock(0.1.x, beta).src/pytest_do_not_mock/).Links:
What I'm asking for
pytest-devplugin, or is it better left as a community/third-party plugin? I'd rather hear "no, and here's why" than push something that doesn't fit.unittest.mockentry points it intercepts, andDoNotMockErroras the failure mode. Happy to change any of it.pytest-dev/pytest-do-not-mock, handing over PyPI maintainer rights, and following whatever the org's contribution/release conventions are.Beta Was this translation helpful? Give feedback.
All reactions