- math.py
Код: Выделить всё
class Math:
def __init__(self, a, b):
self.a = a
self.b = b
def addition(self):
return self.a + self.b
- calculation.py
Код: Выделить всё
from controller.math import Math
def perform_addition():
math = Math(1, 2)
return math.addition()
- test_sample.py
Код: Выделить всё
from unittest.mock import MagicMock, patch
def test_addition():
mock_math = MagicMock()
with patch('controller.math.Math', return_value=mock_math) as MockClass:
instance = MockClass.return_value
instance.addition.return_value = 100
from controller.calculation import perform_addition
output = perform_addition()
print(output) # prints 100
post_patch_output = perform_addition()
print(post_patch_output) # prints 100 but expected 3
В документации по макету Mock.patch говорится, что при выходе из оператора function/with патч отменяется. что не так здесь похоже не тот случай
Подробнее здесь: https://stackoverflow.com/questions/774 ... -statement