Search code examples
pythonpython-3.xpython-unittestmagicmock

Mock nested import in Python with MagicMock


My file (ensure_path.py):

import os

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

My test:

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()

This is giving the failed assertion Expected call: makedirs('/foo/bar') which I think makes sense because I think I'm mocking os.makedirs at the wrong level.

I've tried replacing @patch('os.makedirs', with @patch('src.util.fs.ensure_path.os.makedirs', and a couple variations of that but I get

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

Here is my __init__.py flow :

enter image description here

Is there an obvious fix I'm missing?


Solution

  • Your patch arguments need to be in the reverse order of the @patch decorators.