1👍
✅
You should use mock.patch
and specify the return_value
. Here’s an example where we are patching the return value of func_b()
to Fake value
on the fly:
from mock import patch
import unittest
def func_b():
return "Real value"
def func_a():
return "The result of func_b is '%s'" % func_b()
class MyTestCase(unittest.TestCase):
def test_fake_value(self):
with patch('test.func_b', return_value="Fake value") as mock_function:
self.assertEqual(func_a(), "The result of func_b is 'Fake value'")
UPD:
with patch.object(module_name, 'func_b') as mock_function:
mock_function.return_value = "Fake value"
self.assertEqual(func_a(), "The result of func_b is 'Fake value'")
Source:stackexchange.com