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.

  1. The environment variables are good if you need transfer the python project maybe to other PC with different S.O

https://fastapi.tiangolo.com

So lets do it.

  1. Create folder.
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 !!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store