I'm working on writing tests for a flask application using pytest. I'm trying to write a unit test for this code. Basically I want to mock open()
so that it throws a FileNotFoundError when I call open the first time and check that write()
is called once.
try:
with open("config.json", "r") as file:
config = json.loads(file.read())
print(config)
except FileNotFoundError:
with open("config.json", "w") as file:
file.write(json.dumps({}))
This is my test:
import pytest
import mock
@pytest.fixture
def client(mocker):
return app.test_client()
@pytest.fixture
def mock_no_config(mocker):
m = mock.mock_open()
m.side_effect = [FileNotFoundError, None]
mock.patch("builtins.open", m)
return m
def test_no_config(client, mock_database_exists, mock_no_config):
response = client.get("/install/")
mock_no_config().write.assert_called_once_with("{}")
This is the output from pytest
====================================================================== FAILURES =======================================================================
___________________________________________________________________ test_no_config ____________________________________________________________________
client = <FlaskClient <Flask 'main'>>, mock_database_exists = None
mock_no_config = <MagicMock name='open' spec='builtin_function_or_method' id='139985038428816'>
def test_no_config(client, mock_database_exists, mock_no_config):
response = client.get("/install/")
> mock_no_config().write.assert_called_once_with("{}")
tests/test_web_installer.py:44:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
venv/lib/python3.10/site-packages/mock/mock.py:1100: in __call__
return _mock_self._mock_call(*args, **kwargs)
venv/lib/python3.10/site-packages/mock/mock.py:1104: in _mock_call
return _mock_self._execute_mock_call(*args, **kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_mock_self = <MagicMock name='open' spec='builtin_function_or_method' id='139985038428816'>, args = (), kwargs = {}
self = <MagicMock name='open' spec='builtin_function_or_method' id='139985038428816'>, effect = <list_iterator object at 0x7f50ce47f880>
result = <class 'FileNotFoundError'>
def _execute_mock_call(_mock_self, *args, **kwargs):
self = _mock_self
# separate from _increment_mock_call so that awaited functions are
# executed separately from their call, also AsyncMock overrides this method
effect = self.side_effect
if effect is not None:
if _is_exception(effect):
raise effect
elif not _callable(effect):
result = next(effect)
if _is_exception(result):
> raise result
E FileNotFoundError
venv/lib/python3.10/site-packages/mock/mock.py:1165: FileNotFoundError
---------------------------------------------------------------- Captured stdout call -----------------------------------------------------------------
<THE CONTENTS OF THE REAL config.json FILE HERE>
=============================================================== short test summary info ===============================================================
FAILED tests/test_web_installer.py::test_no_config - FileNotFoundError
It looks like the test is failing because a FileNotFoundError is being raised somewhere I'm not expecting it to, but since it's printing the contents of the real file, I would've assumed the mock wasn't taking effect.
What am I doing wrong?
CodePudding user response:
I ended up just writing my own class. This did the trick for me:
class FakeOpen():
def __init__(self, filename, mode):
if filename == "config.json" and mode == "r":
raise FileNotFoundError()
self.filename = filename
self.mode = mode
self.write_calls = 0
def read(self):
return b"{}"
def write(self, write_string):
self.write_calls = 1
assert self.write_calls <= 1
assert write_string == "{}"
def close(self):
if self.mode == "w":
assert self.write_calls == 1
def __enter__(self):
return self
def __exit__(self, *args, **kwargs):
pass
mocker.patch("builtins.open", FakeOpen)
It raises the FileNotFoundError when I need it to and checks that write is called properly. There's probably a better way to do this, but this does the trick!
CodePudding user response:
Just to give an alternative without a separate class:
@pytest.fixture
def mock_no_config():
open_mock = MagicMock() # mock for open
file_mock = MagicMock() # mock for file returned from open
open_mock.return_value.__enter__.side_effect = [
FileNotFoundError, file_mock
]
with mock.patch("builtins.open", open_mock):
yield file_mock
def test_no_config(client, mock_database_exists, mock_no_config):
response = client.get("/install/")
mock_no_config.write.assert_called_once_with("{}")
Note that there are a few things in the original code that won't work:
- the mocking was reversed as soon as the fixture was returned - using the context manager version of
patch
together withyield
prevents this (usingmocker
is another possibility) - as you use a context manager with
open
, you have to add__enter__
to the mock (this is called by the context manager) - using
None
as side effect won't work, as in this casewrite
would be called onNone
- thus the second mock used forfile