CodeSampleX

Exemple

fastapi 0.141.1: Test a FastAPI app with TestClient and no network, and assert the validation error bodies it returns

Échantillon vérifié pour pypi fastapi 0.141.1: Test a FastAPI app with TestClient and no network, and assert the validation error bodies it returns. Le…

sha256:8bcaa6f98cea6edf681d2b0f76a18856106de62aab70009b1d22a5a50abb1257

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
2
Clés de signature qui l’ont compilé
2
Environnement déclaré python 3.12 linux x64 python 3.12 python pip

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
python 3.12 · linux alpine/x64 · docker ed25519:a2ec939a4c60e243 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · python@1
2026-08-14
python 3.12 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · python@1
2026-08-18

Cas

HOW
Objectif
Test a FastAPI app with TestClient and no network, and assert the validation error bodies it returns
Paquets
Symboles
  • fastapi.testclient.TestClient
  • fastapi.HTTPException
  • response_model
  • lifespan
  • pydantic.BaseModel
  • httpx2.HTTPStatusError
  • starlette.exceptions.StarletteDeprecationWarning
Environnement
python 3.12
Créé
2026-08-14T08:53:22Z

Contrat

  1. assert TestClient targets http://testserver, so the run needs no socket, no port and no DNS
  2. assert TestClient drives the real ASGI pipeline, so pure ASGI middleware runs and stamps a header on the response
  3. assert calling the endpoint function directly does no coercion, so it returns nonsense instead of a 422
  4. assert an int path parameter given text answers 422 with detail[0].type int_parsing and loc path/item_id
  5. assert every 422 entry carries exactly type, loc, msg and input, and input echoes the submitted body back to the caller
  6. assert a missing body field answers 422 with type missing and loc body/age
  7. assert a valid body answers the declared 201 and response_model removes the extra field the handler returned
  8. assert an unexpected field in the request body is ignored by the model rather than rejected
  9. assert HTTPException(404) serialises to a single detail string
  10. assert the lifespan runs only when TestClient is entered as a context manager, so a plain client leaves startup unrun
  11. assert starlette 1.6 binds httpx2 for TestClient when both httpx2 and httpx are installed, so the response is not an httpx.Response
  12. assert raise_for_status on a TestClient response raises httpx2.HTTPStatusError, which except httpx.HTTPStatusError does not catch
  13. assert fastapi.testclient.TestClient is starlette's class re-exported unchanged
  14. assert hiding httpx2 makes the import fall back to httpx with a StarletteDeprecationWarning, which is a UserWarning and not a DeprecationWarning

Fichiers

  • csx.json
  • requirements.txt
  • src/__init__.py
  • src/app.py
  • test/contract.py

Télécharger l’artefact source (tar.gz)

Seeder d'origine

anonymous