Skip to content

mocker.patch.object appears to be broken #164

@binarymason

Description

@binarymason

Python version: 3.7.3

Packages on my machine:

  • pytest: 5.2.1
  • pytest-mock: 1.11.1

Testing a patched object method is called will fail using pytest_mock:

import pytest
from pytest_mock import mocker

def test_process_is_saved_to_db(mocker):
    with mocker.patch.object(Process, 'save', return_value="it works") as mock_save:
        thing = Process()
        thing.save()

    mock_save.assert_called_once()

AssertionError: Expected 'mock' to have been called once. Called 0 times.


But will pass with the exact same signature using unittest.mock:

import pytest
from unittest.mock import patch


def test_process_is_saved_to_db():
    with patch.object(Process, 'save', return_value="it works") as mock_save:
        thing = Process()
        thing.save()

    mock_save.assert_called_once()

All tests passed

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions