Skip to content

Testing a uvicorn server #1455

Answered by euri10
arunppsg asked this question in Q&A
Apr 21, 2022 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

if you want to test your endpoints you have several options:

  1. use the Starlette built-in TestClient, the FastAPI docs should give you plenty of examples
  2. use httpx as a test client, see https://www.python-httpx.org/async/#calling-into-python-web-apps

if you really want to run uvicorn during your tests, which you probably dont most of the time, you can either run it in a thread like you did or look at our test suite here

uvicorn/tests/utils.py

Lines 9 to 18 in b8e65e4

@asynccontextmanager
async def run_server(config: Config, sockets=None):
server = Server(config=config)
cancel_handle = asyncio.ensure_future(server.serve(sockets=sockets))
await asyncio.sleep(0.1

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@arunppsg
Comment options

Answer selected by arunppsg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants