Brings lifespan to 100% coverage
@@ -123,11 +123,13 @@ def test_lifespan_on_with_error():
@pytest.mark.parametrize("mode", ("auto", "on"))
@pytest.mark.parametrize("raise_exception", (True, False))
-def test_lifespan_with_failed_startup(mode, raise_exception):
+def test_lifespan_with_failed_startup(mode, raise_exception, caplog):
async def app(scope, receive, send):
message = await receive()
assert message["type"] == "lifespan.startup"
- await send({"type": "lifespan.startup.failed"})
+ await send(
+ {"type": "lifespan.startup.failed", "message": "the lifespan event failed"}
+ )
if raise_exception:
# App should be able to re-raise an exception if startup failed.
raise RuntimeError()
@@ -144,6 +146,13 @@ def test_lifespan_with_failed_startup(mode, raise_exception):
loop = asyncio.new_event_loop()
loop.run_until_complete(test())
+ error_messages = [
+ record.message
+ for record in caplog.records
+ if record.name == "uvicorn.error" and record.levelname == "ERROR"
+ ]
+ assert "the lifespan event failed" in error_messages.pop(0)
+ assert "Application startup failed. Exiting." in error_messages.pop(0)
@pytest.mark.parametrize("mode", ("auto", "on"))
