我如何模拟一个开放的(...write() 没有收到'No such file or directory'错误?



我的解决方案基于:

  • 我如何模拟与语句中使用的open(使用Python中的mock框架)?
  • AttributeError:
  • http://www.voidspace.org.uk/python/mock/helpers.html mock.mock_open

我有一个类,我可以实例化它,它写文件。我正在试着测试它,但是我在嘲笑open()时遇到了问题。我使用以下代码作为最小的代码片段,它可以

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

当我运行这个时,我得到以下错误:

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'

我如何模拟open(),使它只返回一个file_pointer,而不尝试与文件系统交互?

mock_open:

模拟builtins.open(或module.open, module =包含WriteData的模块名)
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!')

您可以使用__enter__魔术方法来模拟以下情况:

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!')

希望这对你有帮助!

相关内容

最新更新