I am trying to create a test for a Python application using mock and the @patch decorator.
Given the following directory structure:
|
| |
| | |
| | \
| \
| |
| | |
| | \
| \
\
If the contents of the files:
def some_function():
return 'A'
from mypackage.mymodule.somefile import some_function
def func_to_test():
return some_function()
from unittest import TestCase
from mock import patch
class TestFunc_to_test(TestCase):
def test_func_to_test(self):
from mypackage.myothermodule import func_to_test
self.assertEqual('A', func_to_test())
@patch('mypackage.mymodule.somefile.some_function')
def test_func_to_test_mocked(self, some_mock_function):
from mypackage.myothermodule import func_to_test
some_mock_function.return_value = 'B'
self.assertEqual('B', func_to_test())
The problem is that although the first test passes (test_func_to_test), the second test (test_func_to_test_mocked) fails (due to AssertionError).
I was able to mock a function from the "built-in" modules (for example, request.get, for example) using the same approach, however I cannot get @patch to work when trying to fix a function from one of my modules ...
Any help would be appreciated :)
source
share