AttributeError: <module '__main__' from [..] does not have the attribute 'open'

删除回忆录丶 提交于 2019-12-11 02:39:34

问题


I'm working on writing a test for a module for docker-py, but I can't seem to get the test to work properly.

The function I'm testing looks as follows:

def parse_env_file(env_file):
    """
    Reads a line-separated environment file.
    The format of each line should be "key=value".
    """
    environment = []

    if os.path.isfile(env_file):
        with open(env_file, 'r') as f:
            # We can't use f.readlines() here as it's not implemented in Mock
            for line in f.read().split('\n'):
                parse_line = line.strip().split('=')
                if len(parse_line) == 2 and line[0] != '#':
                    k = parse_line[0]
                    v = parse_line[1]
                    environment.append('{0}={1}'.format(k, v))

    return environment

The test then looks like this:

def test_parse_env_file_proper(self):
    with mock.patch('os.path.isfile', return_value=True):
        mock_env_file = 'USER=jdoe\nPASS=secret'
        with mock.patch('{}.open'.format(__name__), mock.mock_open(read_data=mock_env_file)):
            get_parse_env_file = parse_env_file('foobar')
    self.assertEqual(get_parse_env_file, ['USER=jdoe', 'PASS=secret'])

When I run the test however, I get the following error:

======================================================================
ERROR: test_parse_env_file_proper (__main__.UtilsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "tests/utils_test.py", line 102, in test_parse_env_file_proper
    with mock.patch('{}.open'.format(__name__), mock.mock_open(read_data=mock_env_file)):
  File "/Users/mvip/code/private/github/docker-py/.tox/py27/lib/python2.7/site-packages/mock.py", line 1268, in __enter__
    original, local = self.get_original()
  File "/Users/mvip/code/private/github/docker-py/.tox/py27/lib/python2.7/site-packages/mock.py", line 1242, in get_original
    "%s does not have the attribute %r" % (target, name)
AttributeError: <module '__main__' from 'tests/utils_test.py'> does not have the attribute 'open'

Any pointers here would be helpful.


回答1:


The problem is that, as a builtin, open isn't directly found in the module, but rather as fallback in the builtins module.

To get around this you should include create=True when patching.

from unittest import mock

with mock.patch(__name__+".open", mock.mock_open(read_data="data"), create=True):
    with open("somefile") as f:
        assert f.read() == "data"

However, this only patches open in the current module (the module running the test, not the module under test).

So you'd be better off doing:

import unittest
from unittest.mock import mock_open, patch

import module_under_test


def func():
    with open("somefile") as f:
        return f.read()


class MyTestCase(unittest.TestCase):

    def test_open(self):
        data = "some data"
        with patch.object(module_under_test, "open", mock_open(read_data=data), create=True):
            result = module_under_test.func()

        self.assertEqual(result, data)


if __name__ == "__main__":
    unittest.main()


来源:https://stackoverflow.com/questions/31705057/attributeerror-module-main-from-does-not-have-the-attribute-open

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!