We've recently switched from unittest to pytest. I've encountered a strange problem when using mocker.patch
as a context manager. Consider the following example.
module_a.py
class MyClass:
def value(self):
return 10
module_b.py
import module_a
class AnotherClass:
def get_value(self):
return module_a.MyClass().value()
test_module_b.py
from module_b import AnotherClass
def test_main_2(mocker):
with mocker.patch('module_a.MyClass.value', return_value=20):
value = AnotherClass().get_value()
assert value == 20
value = AnotherClass().get_value()
assert value == 10
I would expect that once the context manager exits, MyClass's value method method would be restored (return value of 10), however the test fails on the second assert statement with an assertion error 20 != 10
If I use the exact same test, but replace mocker.patch
with unittest.mock.patch
, it passes. I thought that pytest-mock shared the same API as unittest.mock, so I'm confused as to why there is a difference.