Consider the module some_module.py with the following content (in my actual use case, SomeClass and some_function are defined in separate modules):
class SomeClass:
def __init__(self, v):
self.some_attr = v
def some_method(self):
self.some_attr += self.some_attr
def some_function(val):
c = SomeClass(val)
c.some_method()
return c
Assume that I have already unittested SomeClass using pytest. Now I want to unittest some_function. My understanding is that I should mock everything related to SomeClass. What I tried was to add the following test function to some_module.py:
def test_some_function(mocker):
# arrange
c = mocker.MagicMock()
c.some_attr = 10
mocker.patch('some_module.SomeClass', return_value=c)
mocker.patch('some_module.SomeClass.some_method')
# act
c = some_function(10)
# assert
assert c.some_attr == 20
Running pytest some_module.py in a terminal (obviously) results in failure of the test as c.some_attr is 10. What should the test function look like instead?