用MagicMock模拟嵌套导入Python

时间:2017-12-18 22:11:58

标签: python python-3.x python-unittest magicmock

我的档案(ensure_path.py):

import os

def ensure_path(path):
  if not os.path.exists(path):
    os.makedirs(path)
  return path

我的测试:

import unittest

from unittest.mock           import patch, MagicMock
from src.util.fs.ensure_path import ensure_path

FAKE_PATH = '/foo/bar'

class EnsurePathSpec(unittest.TestCase):

  @patch('os.path.exists', side_effect=MagicMock(return_value=False))
  @patch('os.makedirs',    side_effect=MagicMock(return_value=True))
  def test_path_exists_false(self, _mock_os_path_exists_false, _mock_os_makedirs):
    ensure_path(FAKE_PATH)
    _mock_os_path_exists_false.assert_called_with(FAKE_PATH)
    _mock_os_makedirs.assert_called_with(FAKE_PATH)

  @patch('os.path.exists', side_effect=MagicMock(return_value=True))
  @patch('os.makedirs',    side_effect=MagicMock(return_value=True))
  def test_path_exists_true(self, _mock_os_path_exists_true, _mock_os_makedirs):
    ensure_path(FAKE_PATH)
    _mock_os_path_exists_true.assert_called_with(FAKE_PATH)
    _mock_os_makedirs.assert_not_called()

这给出了失败的断言Expected call: makedirs('/foo/bar'),我认为这是有道理的,因为我认为我在错误的层面上嘲笑os.makedirs

我尝试用@patch('os.makedirs',替换@patch('src.util.fs.ensure_path.os.makedirs',以及其他几种变体,但我得到了

ImportError: No module named 'src.util.fs.ensure_path.os'; 'src.util.fs.ensure_path' is not a package

这是我的__init__.py流程:

enter image description here

我有一个明显的解决方法吗?

1 个答案:

答案 0 :(得分:2)

您的补丁参数必须与@patch装饰器的顺序相反。