Skip to content

Mocks created by mocker.patch.multiple are not handled like other patches #638

Open

Description

@nicoddemus

mocker.patch.multiple() returns a dict of the mocks it created, and pytest-mock only registers that dict. #629 made resetall() reset the mocks inside it, but two other features still treat the dict as the mock:

  1. No context-manager warning. The PytestMockWarning hook is only installed on the dict (where it does nothing), not on the mocks:

    def test_multiple_warning(mocker):
        mocks = mocker.patch.multiple(Target, a=mocker.DEFAULT)
        with pytest.warns(PytestMockWarning):  # fails: DID NOT WARN
            with mocks["a"]:
                pass

    with mocker.patch.object(Target, "a"): does warn.

  2. mocker.stop() can't stop a single mock.

    def test_multiple_stop_one(mocker):
        mocks = mocker.patch.multiple(Target, a=mocker.DEFAULT, b=mocker.DEFAULT)
        mocker.stop(mocks["a"])  # ValueError: This mock object is not registered

    Only mocker.stop(mocks) works, which undoes all of them. Supporting single mocks would need each attribute's patcher to be stopped on its own.

Both reproduced on 3.16.1 (Target is any class with attributes a and b).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions