Follows on from this answer about disabling autouse fixtures.
If I have this fixture
@pytest.fixture(autouse=True)
def mock_logger_error(request):
with mock.patch('logging.Logger.error') as mock_error:
yield
... how would I actually make use of mock_error
(e.g. mock_error.assert_called_once_with(...)
) in a test?
def test_something_possibly_emitting_error_message():
...
if connection_ok:
mock_error.assert_not_called()
else:
mock_error.assert_called_once_with(AnyStringWithRegex(r'ERROR\..*some error messsage.*'))
---> "Undefined variable: mock_error". I.e. although the patch is doing its job here, i.e. patching Logger.error
, I don't know how to get a reference to it.
As can be seen here, it is possible to pass data from a fixture to a test, but that shows that you have to use return
to do that. If you're using yield
I don't think you can also use return
...