This commit is contained in:
Areeb455 2026-03-16 10:21:04 +00:00 committed by GitHub
commit 6a18ddf1ab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 55 additions and 9 deletions

7
check_params.py Normal file
View File

@ -0,0 +1,7 @@
from decimal import Decimal
from fastapi import Query
def create_item(price: Decimal = Query(..., max_digits=5, decimal_places=2)):
return {"price": price}

View File

@ -94,6 +94,7 @@ As the testing function is now asynchronous, you can now also call (and `await`)
/// tip
If you encounter a `RuntimeError: Task attached to a different loop` when integrating asynchronous function calls in your tests (e.g. when using <a href="https://stackoverflow.com/questions/41584243/runtimeerror-task-attached-to-a-different-loop" class="external-link" target="_blank">MongoDB's MotorClient</a>), remember to instantiate objects that need an event loop only within async functions, for example, using a lifespan async context manager.
If you encounter a `RuntimeError: Task attached to a different loop` when integrating asynchronous function calls in your tests (e.g. when using [MongoDB's MotorClient](https://stackoverflow.com/questions/41584243/runtimeerror-task-attached-to-a-different-loop)), remember to instantiate objects that need an event loop only within async functions, e.g. an `@app.on_event("startup")` callback.
///

View File

@ -0,0 +1,30 @@
from contextlib import asynccontextmanager
from typing import Any
from fastapi import FastAPI
from fastapi.testclient import TestClient
# Use typing.Dict for compatibility with older Python versions
items: dict[str, Any] = {}
@asynccontextmanager
async def lifespan(app: FastAPI):
items["foo"] = {"name": "Fighters"}
items["bar"] = {"name": "Tenders"}
yield
app = FastAPI(lifespan=lifespan)
@app.get("/items/{item_id}")
async def read_items(item_id: str):
return items[item_id]
def test_read_items():
with TestClient(app) as client:
response = client.get("/items/foo")
assert response.status_code == 200
assert response.json() == {"name": "Fighters"}

View File

@ -1,15 +1,19 @@
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.testclient import TestClient
app = FastAPI()
items = {}
@app.on_event("startup")
async def startup_event():
@asynccontextmanager
async def lifespan(app: FastAPI):
items["foo"] = {"name": "Fighters"}
items["bar"] = {"name": "Tenders"}
yield
app = FastAPI(lifespan=lifespan)
@app.get("/items/{item_id}")

View File

@ -1,7 +1,11 @@
import pytest
from docs_src.app_testing import tutorial003, tutorial003_py310
def test_main():
with pytest.warns(DeprecationWarning):
from docs_src.app_testing.tutorial003_py310 import test_read_items
test_read_items()
def test_tutorial003():
# This covers the base version (tutorial003.py)
tutorial003.test_read_items()
def test_tutorial003_py310():
# This covers the modern version (tutorial003_py310.py)
tutorial003_py310.test_read_items()