question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mocking open raises AttributeError

See original GitHub issue

I am trying to translate mock_open unittest’s example but I get an AttributeError:

def test_open(mocker):
    with mocker.patch('__main__.open', mocker.mock_open(read_data='bibble')) as m:
        with open('foo') as h:
            result = h.read()

    m.assert_called_once_with('foo')
    assert result == 'bibble'

Output:

    def test_open(mocker):
        open_mock = mocker.mock_open(read_data='bibble')
>       with mocker.patch('builtins.open', open_mock) as m:
E       AttributeError: __enter__

NOTE: pytest-mock==1.10.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
nicoddemuscommented, Jan 14, 2019

The purpose of the mocker fixture is to not use it as a context manager, otherwise you might as well use mock.patch directly. 😉

Try this:

def test_open(mocker):
    m = mocker.patch('__main__.open', mocker.mock_open(read_data='bibble'))
    with open('foo') as h:
        result = h.read()

    m.assert_called_once_with('foo')
    assert result == 'bibble'

See Note about usage as context manager in the README for more information.

I’m closing this but feel free to follow up with more questions. 👍

7reactions
MarcAragonescommented, Jan 14, 2019

I had to change '__main__.open' with 'builtins.open'.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Mock for context-manager fails with AttributeError: __ ...
For those really interested, I am trying to mock example 6 opened_w_error() in PEP 343 which deals with opening files and catching errors....
Read more >
Why does mocking 'open' and returning a FileNotFoundError ...
Testing by mocking open with a FileNotFoundError raises AttributeError: ... I've mocked the open command in so.main . test_so_main.py has two tests: One...
Read more >
unittest.mock — mock object library — Python 3.11.1 ...
Accessing any attribute not in this list will raise an AttributeError . If spec is an object (rather than a list of strings)...
Read more >
unittest.mock — OpenMC Documentation
_mock_methods or name in _all_magics: raise AttributeError("Mock object has no ... A helper function to create a mock to replace the use of...
Read more >
Mocking with Python! - Santex Group
If the mock has a name then it will be used in the repr of the mock. This can be useful for debugging....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found