I am trying to mock an entire class using patch.
Imagine that class is
MyClass() with methods
bar() both of which return a boolean.
The way I was attempting it was by including fixture in the
conftest.py for example
@pytest.fixture(scope="session") def mock_myclass() -> MagicMock: with patch("module.file.MyClass") as myclass_mocked: myclass_mocked.foo.return_value = True myclass_mocked.bar.return_value = True yield myclass_mocked
Then I have a test called
test_some_other_function for example
def test_some_other_function(mock_myclass: MagicMock, x: int, y:float): some_other_function(x,y)
some_other_function is not a member of
MyClass but uses an object from
MyClass and calls both
bar methods. However when I try either
mock_myclass.bar.asseert_called() I always get the error below, even though I am positive the test is reaching the places where
bar are called.
AssertionError: Expected 'foo' to have been called.
I hope this was clear. Thanks!