The general case would be to use patch
from mock
. Consider the following:
utils.py
def get_content():
return 'stuff'
mymodule.py
from util import get_content
class MyClass(object):
def func(self):
return get_content()
test.py
import unittest
from mock import patch
from mymodule import MyClass
class Test(unittest.TestCase):
@patch('mymodule.get_content')
def test_func(self, get_content_mock):
get_content_mock.return_value="mocked stuff"
my_class = MyClass()
self.assertEqual(my_class.func(), 'mocked stuff')
self.assertEqual(get_content_mock.call_count, 1)
get_content_mock.assert_called_once()
Note how get_content
is mocked, it is not util.get_content
, rather mymodule.get_content
since we are using it in mymodule
.
Above has been tested with mock v2.0.0, nosetests v1.3.7 and python v2.7.9.