Skip to content

Commit 46f1ace

Browse files
committed
add test for fastapi prometheus middleware
1 parent 89ae9e8 commit 46f1ace

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from collections.abc import AsyncIterable
2+
3+
import pytest
4+
from asgi_lifespan import LifespanManager
5+
from fastapi import FastAPI
6+
from httpx import AsyncClient
7+
from servicelib.fastapi.monitoring import setup_prometheus_instrumentation
8+
9+
10+
@pytest.fixture
11+
async def app(app: FastAPI) -> AsyncIterable[FastAPI]:
12+
"""
13+
Fixture that sets up the Prometheus middleware in the FastAPI app.
14+
"""
15+
setup_prometheus_instrumentation(app)
16+
async with LifespanManager(app):
17+
yield app
18+
19+
20+
async def test_metrics_endpoint(client: AsyncClient, app: FastAPI):
21+
"""
22+
Test that the /metrics endpoint is available and returns Prometheus metrics.
23+
"""
24+
response = await client.get("/metrics")
25+
assert response.status_code == 200
26+
assert "# HELP" in response.text
27+
assert "# TYPE" in response.text

0 commit comments

Comments
 (0)