I have an REST-API app written with Uvicorn+FastAPI
Which I want to test using PyTest.
I want to start the server in a fixture when I start the tests, so when the test complete, the fixture will kill the app.
FastAPI Testing shows how to test the API app,
from fastapi import FastAPI
from starlette.testclient import TestClient
app = FastAPI()
@app.get("/")
async def read_main():
return {"msg": "Hello World"}
client = TestClient(app)
def test_read_main():
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"msg": "Hello World"}
This doesn't bring the server online in the usual way. It seems that the specific functionality that is triggered by the client.get command is the only thing that runs.
I found these additional resources, but I can't make them work for me:
https://medium.com/@hmajid2301/pytest-with-background-thread-fixtures-f0dc34ee3c46
How to run server as fixture for py.test
How would you run the Uvicorn+FastAPI app from PyTest, so it goes up and down with the tests?