Following shows simple and cleary way to write unittest in python, and how to run them before launching a fastapi project.
write a unittest
Suppose you have the following class in some_class.py
# some_class.py |
Then it is easy to write an unitttest file like the following “test_some_class.py” as:
# test_some_class.py |
run your test jobs in a project
Now you have your unitttest setup, now how to you run the tests?
For example, you might have a fastapi project, but before deployment, you want to run some unittest.
my_fastapi_project/ |
Here is how you would run the tests using the standard unittest
module:
# Navigate to the project directory |
If the tests are successful and you’re satisfied with the results, you can then start your FastAPI application:
# Start the FastAPI application |
Alternatively, if you have pytest
installed, which provides more features and a nicer output, you can run:
# Navigate to the project directory (if not already there) |