Home > Mobile >  Python Mock: Raising Error for function of Mocked Class
Python Mock: Raising Error for function of Mocked Class

Time:01-19

I'm trying to test some code that create or changes directories and files based on some inputs. My issue is raising exceptions when a method of a mocked object is called. For example, say I have some code:

def create_if_not_exists(dest):
    dir = os.path.dirname(dest)
    if not dir:
        # create if it doesn't exist
        try:
           os.makedirs(os.path.dirname(dest))
        except OSError:
           pass # Nevermind what goes here, it's unimportant to the question

and a unit test:

@patch('my.package.os')
def test_create_dir_if_not_exists(self, mock_os):
    mock_os.path.dirname.return_value = None
    mock_os.makedirs.raiseError.side_effect = OSError()

    with self.assertRaises(OSError)
        create_if_not_exists('test')

This setup returns AssertionError: OSError not raised but my understanding is it should raise the error when the makedirs call is made in the actual (non-test) method. Is my understanding incorrect?

CodePudding user response:

Try with the following patch in the test file:

@patch('my.package.os.path.dirname')
@patch('my.package.os.makedirs')
def test_create_dir_if_not_exists(self, mock_os_makedirs, mock_os_path_dirname):
    mock_os_path_dirname.return_value = None
    mock_os_makedirs.raiseError.side_effect = OSError()

    with self.assertRaises(OSError)
        create_if_not_exists('test')
  • Related