Skip to content
Advertisement

How to terminate a Uvicorn + FastAPI application cleanly with workers >= 2 when testing with pytest

I have an application written with Uvicorn + FastAPI. I am testing the response time using PyTest.

Referring to How to start a Uvicorn + FastAPI in background when testing with PyTest, I wrote the test. However, I found the application process alive after completing the test when workers >= 2.

I want to terminate the application process cleanly at the end of the test.

Do you have any idea?

The details are as follows.

Environment

Libraries

  • fastapi == 0.68.0
  • uvicorn == 0.14.0
  • requests == 2.26.0
  • pytest == 6.2.4

Sample Codes

  • Application: main.py
    JavaScript
  • Test: test_main.py
    JavaScript

Execution Result

JavaScript

Advertisement

Answer

I have found a solution myself.

Thanks > https://stackoverflow.com/a/27034438/16567832

Solution

After install psutil by pip install psutil, update test_main.py

JavaScript

Execution Result

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement