Comments (3)
we should mirror the mock apis with a mock prefix and forego selective undo, people can use the monkeypatch.context context manager
from pytest.
An alternative solve, would be to update pytest-mock to use monkeypatch, so that it has similar patching semantics as monkeypatch instead of using unittest patch semantics.
Could you elaborate on this? How does pytest-mock not already do what you want? I'm -1 on introducing a different way to do the same thing into the core when we already have a well maintained and popular plugin that solves the same problem.
from pytest.
Actually thinking about it more carefully - I was wrong to suggest that pytest-mock could be updated to solve this. Extending pytest-mock to allow patching with non-mock objects wouldn't make a lot of sense.
The issue with using pytest-mock as the only means of patching is that it defaults to using MagicMock. MagicMock can be problematic / difficult to work with (it has a lot of 'magic' that leads to some pitfalls for developers). As a result I think a lot of teams end up in a situation where they use a combination of monkeypatch (for simpler cases) as well as one of either pytest-mock or unittest.mock (when they need more fully featured 'mocking').
It forces developers to understand the semantics of two libraries in order to patch objects in unit tests. We shouldn't need more than one tool to solve one problem (patching).
IMO developers should have a single way of patching (monkeypatch) with better support for patching with MagicMock/AsyncMock for the cases where that is needed.
If the answer was "just use pytest-mock" or any other plugin - I would be happy. But I think we currently exist in a limbo where you end up using multiple tools to patch objects when writing tests in Pytest.
from pytest.
Related Issues (20)
- junit-xml output attribute can be too big to handle with -vv
- More granular scripting interface HOT 4
- support an alternate syntax for "not" (as in `pytest -m "not stuff"`) HOT 6
- Documentation request: types/shapes of options in `[tool.pytest.ini_options]` HOT 5
- Please make verbose XFAIL tracebacks optional HOT 17
- Let `OutcomeException` inherit from `Exception` instead of `BaseException` HOT 3
- Official Pytest Docker Image HOT 3
- I am getting error "pytest: error: unrecognized arguments: --env" while executing tests using pytest HOT 8
- Handle warnings raised at import time early in pytest process
- `monkeypatch.setattr` started failing in Pytest 8.1.2 (setuptools CI) HOT 1
- Show nicer tracebacks for BaseExceptionGroup from fixture setup/teardown
- Issue with pytest 8.2.0 and tornado async tests HOT 3
- Pytest 8.2.0 breaks Jenkins unittesting [Python 3.10] HOT 6
- Getting `AttributeError: 'FixtureDef' object has no attribute 'unittest'` in pytest 8.2.0 HOT 9
- 'FixtureDef' object has no attribute 'unittest' (an Async @ 8.2.0 issue?) HOT 1
- PyTest 8.2.0 in compatible with certain versions of pytorch, especially DDP tests HOT 1
- v8.2.0 raises `Can't instantiate abstract class` when a test class implements an abstract class HOT 4
- With 8.2.0, attempt to import docs from niece module fails HOT 1
- Since 8.2.0, automatic multiprocessing causes race conditions in my project HOT 3
- Support marker keyword arguments in marker expression (test selection via `-m`) HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pytest.