由于我们项目的结构以及测试运行方式,我们的库模块在测试模块中使用相对导入,例如:
我得到了这个错误:
有什么方法可以在这种情况下使用
from .. import foo_bar
现在,我想检查 foo_bar
模块中的一个函数是否对某些参数发出警告。我已经尝试了以下方法:
from unittest.mock import patch
@patch('foo_bar.logging')
def test_distance(mock_logging):
foo_bar.baz(None)
assert mock_logging.warn.called
我得到了这个:
target = 'foo_bar'
def _importer(target):
components = target.split('.')
import_path = components.pop(0)
> thing = __import__(import_path)
E ImportError: No module named 'foo_bar'
/tmp/envs/foo/lib/unittest/mock.py:1058: ImportError
当我尝试像这样更改补丁行:
@patch('..foo_bar.logging')
我得到了这个错误:
target = '..foo_bar'
def _importer(target):
components = target.split('.')
import_path = components.pop(0)
> thing = __import__(import_path)
E ValueError: Empty module name
/tmp/envs/foo/lib/unittest/mock.py:1058: ValueError
有什么方法可以在这种情况下使用
unittest.mock
中的patch
函数?