Python FastApi Project with VisualCode, testing and debug

In this topic I would like to show you have to use Visual Code to create project on Python 3.9, with environment variables, debug and unittest.
- The environment variables are good if you need transfer the python project maybe to other PC with different S.O
- The Debug is an amazing tool that help you, check every step or line from you code.
- The unit test help you to change your code for new features and be sure that the old functions still works.
- FastApi is a framework that help you to create apis very fast.
So lets do it.
- Create folder.
- Open folder en VSCODE.
- Open you terminal, be sure that the path is the folder that you will use.
python3 -m venv .env
4. Check version of python and pip.
python3 --versionpip3 --version
5. Create simple file main.py with one line
print("hello world")
6 . On you terminar copy and see the result Hello World
python3 main.py
>>>
Hello world
7. install next extension:
Name: Python
Id: ms-python.python
Description: IntelliSense (Pylance), Linting, Debugging (multi-threaded, remote), Jupyter Notebooks, code formatting, refactoring, unit tests, and more.
Version: 2022.8.0
Publisher: Microsoft
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-python.python
8. Install FastAPi, open you terminal
pip3 install “fastapi[all]”
8. on main.py file paste
from fastapi import FastAPIapp = FastAPI()@app.get(“/”)
async def root():
return {“message”: “Hello World”}
9.Create a debug file from python-fast-api. Check in the root path that a new folder is created with one file inside it.

10. Run the debug and the result will be:


11. Configure the unit test. be sure that the icon is in VScode

12. Create folder and file.
import unittest
class test_main(unittest.TestCase): def test_main(self):
print("ready") if __name__ == '__main__':
unittest.main()
13. configure the unittest for python. Select folder and the path of the files.



Happy TESTING, DEBUGGING AND CODING !!