24

I've based my solution on:

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?

Community
  • 1
  • 1
Drew
  • 1,687
  • 5
  • 25
  • 46

2 Answers2

40

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!')
falsetru
  • 357,413
  • 63
  • 732
  • 636
9

You can use the __enter__ magic method to simulate which:

from unittest.mock import patch, MagicMock, call, mock_open

@patch('os')
@patch('builtins.open', new_callable=mock_open())
def test_dump(self, mock_open_file, mock_os):
    data_writer = WriteData(dir='/my/path/not/exists', name='Foo')

    mock_os.path.exists.assert_called_once_with('/my/path/not/exists')
    mock_open_file.assert_called_once_with('/my/path/not/exists/output.text', 'w+')
    mock_open_file.return_value.__enter__().write.assert_called_once_with('Hello, Foo!')

Hope this helps!

denisfrm
  • 105
  • 1
  • 4
  • This didn't work for me when I trid to access the write calls with`mock_open_file.return_value.__enter__().write.mock_calls`, gave an empty list although `mock_open_file().write.mock_calls` gives the calls. – kilgoretrout Jul 20 '21 at 19:37
  • What you did is not wrong either. The __enter__() is the representation of the context input. If you are using open out of context, for example: `file = open('filename.json')` You are not in a context. Only when using this way: `with open('filename.json') as file:` – denisfrm Aug 09 '21 at 15:59