To ensure that your application logs an error under a specific condition, use the built-in fixture called caplog in pytest.
caplog
This fixture allows you to capture log messages generated during the execution of your test.
My previous tips on testing.