Currently dealing with some legacy python code. I need to test one of the functions, function foo
. Now to do that I need to mock the return value given by another function that's called by foo
which is function bar
.
However both of these are outside of my testing package and when I try to mock out bar
, foo
is unaffected. Is there a way of doing this without moving everything into a class? (which is preferred, but it's legacy code).
A simple example:
foo_and_bar_package/foo_and_bar_module.py
def bar():
return 5
def foo():
return 2 * bar()
test_foo_and_bar_module.py
from unittest import TestCase
from unittest.mock import patch
from foo_and_bar_package.foo_and_bar_module import foo
class TestFoo(TestCase):
@patch('foo_and_bar_package.foo_and_bar_module.bar')
def test_foo(self, mock_bar):
mock_bar.return_value = 2
self.assertEqual(foo.foo(), 2 * mock_bar.return_value)