Giter VIP home page Giter VIP logo

Comments (3)

RonnyPfannschmidt avatar RonnyPfannschmidt commented on May 27, 2024

we should mirror the mock apis with a mock prefix and forego selective undo, people can use the monkeypatch.context context manager

from pytest.

The-Compiler avatar The-Compiler commented on May 27, 2024

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.

soceanainn avatar soceanainn commented on May 27, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.