Reputation: 1120
I have a project structure like this:
py_test
|
+---my_module
| |
| +--- __init__.py (empty)
| |
| +--- futil.py
|
+---test
|
+--- __init__.py (empty)
|
+--- futil_test.py
In futil.py I have the following:
from os import path
def check_exists(file_path):
return path.exists(file_path)
In futil_test.py I'm trying to implement a unit test like this:
import mock
import unittest
from my_module.futil import check_exists
class TestExists(unittest.TestCase):
@mock.patch('my_module.futil.os.path') # <---leads to error, as well as my_module.os.path
def test_exists(self, mock_path):
mock_path.exists.return_value = True
self.assertTrue(check_exists('ba'))
if __name__ == '__main__':
unittest.main()
When I try to launch the unit test, it fails with the error:
Error
Traceback (most recent call last):
File "/usr/lib64/python2.7/unittest/case.py", line 367, in run
testMethod()
File "/home/antonio/devel/py/py_test/venv/lib/python2.7/site-packages/mock/mock.py", line 1322, in patched
arg = patching.__enter__()
File "/home/antonio/devel/py/py_test/venv/lib/python2.7/site-packages/mock/mock.py", line 1378, in __enter__
self.target = self.getter()
File "/home/antonio/devel/py/py_test/venv/lib/python2.7/site-packages/mock/mock.py", line 1548, in <lambda>
getter = lambda: _importer(target)
File "/home/antonio/devel/py/py_test/venv/lib/python2.7/site-packages/mock/mock.py", line 1235, in _importer
thing = _dot_lookup(thing, comp, import_path)
File "/home/antonio/devel/py/py_test/venv/lib/python2.7/site-packages/mock/mock.py", line 1224, in _dot_lookup
__import__(import_path)
ImportError: No module named os
In the example here the similar construction seems to be working. What am I doing wrong?
Upvotes: 0
Views: 341
Reputation: 1005
"from os import path", will make path function part of futil. Instead of mocking "my_module.futil.os.path'" just mock "my_module.futil.path'", that should work.
I have found this article very useful in the past http://alexmarandon.com/articles/python_mock_gotchas/
Upvotes: 1