Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I mock an open(...).write() without getting a 'No such file or directory' error?

I've based my solution on:

  • How do I mock an open used in a with statement (using the Mock framework in Python)?,
  • AttributeError: <module '__main__' from [..] does not have the attribute 'open',
  • http://www.voidspace.org.uk/python/mock/helpers.html#mock.mock_open

I have a class, which I can instantiate, which writes to a file. I'm trying to test it, but I'm having problems mocking open(). I'm using the following as the smallest piece of code, which can

import os
import unittest
from unittest.mock import mock_open, patch

__author__ = 'drews'


class MockPathExists(object):
    def __init__(self, return_value):
        self.received_args = None
        self.return_value = return_value

    def __call__(self, *args, **kwargs):
        self.received_args = args
        return self.return_value


class WriteData:
    def __init__(self, dir, name='World'):
        self.name = name
        self.dir = dir

    def dump(self):
        if os.path.exists(self.dir):
            with open('{0}/output.text'.format(self.dir), 'w+') as fp:
                fp.write('Hello, {0}!'.format(self.name))


class TestListWindowsPasswords(unittest.TestCase):
    def setUp(self):
        self._orig_pathexists = os.path.exists
        os.path.exists = MockPathExists(True)

    def test_dump(self):
        m = mock_open()
        with patch.object(WriteData, 'open', m, create=True):
            data_writer = WriteData(
                dir='/my/path/not/exists',
                name='Foo'
            )
            data_writer.dump()

        self.assertEqual(os.path.exists.received_args[0], '/my/path/not/exists/output.text')
        m.assert_called_once_with('/my/path/not/exists/output.text', 'w+')
        handle = m()
        handle.write.assert_called_once_with('Hello, Foo!')



    def tearDown(self):
        os.path.exists = self._orig_pathexists

When I run this, I get the following error:

Error
Traceback (most recent call last):
  File "/Users/drews/Development/tool/tests/test_mockopen.py", line 41, in test_dump
    data_writer.dump()
  File "/Users/drews/Development/tool/tests/test_mockopen.py", line 25, in dump
    with open('{0}/output.text'.format(self.dir), 'w+') as fp:
FileNotFoundError: [Errno 2] No such file or directory: '/my/path/not/exists/output.text'

How can I mock open(), so that it just returns a file_pointer, and doesn't try to interact with the file system at all?

like image 598
Drew Avatar asked Oct 17 '15 08:10

Drew


People also ask

What is mock and patch in Python?

patch() unittest. mock provides a powerful mechanism for mocking objects, called patch() , which looks up an object in a given module and replaces that object with a Mock . Usually, you use patch() as a decorator or a context manager to provide a scope in which you will mock the target object.

What does mock mock () do?

The Mockito. mock() method allows us to create a mock object of a class or an interface. We can then use the mock to stub return values for its methods and verify if they were called.


1 Answers

Mock builtins.open (or module.open, module = the module name that contains WriteData) with the mock_open:

import builtins

class TestListWindowsPasswords(unittest.TestCase):
    def setUp(self):
        self._orig_pathexists = os.path.exists
        os.path.exists = MockPathExists(True)

    def test_dump(self):
        with patch('builtins.open', unittest.mock.mock_open()) as m:
            data_writer = WriteData(
                dir='/my/path/not/exists',
                name='Foo'
            )
            data_writer.dump()

        self.assertEqual(os.path.exists.received_args[0], '/my/path/not/exists')  # fixed
        m.assert_called_once_with('/my/path/not/exists/output.text', 'w+')
        handle = m()
        handle.write.assert_called_once_with('Hello, Foo!')
like image 174
falsetru Avatar answered Oct 25 '22 10:10

falsetru