This is a basic Fast-API python project with examples of proper unit test, integration tests, code coverage, CI/CD, and code scanning.
- Python: ^3.8
- Pipenv
- Docker + Docker Compose
- Terraform: ^v0.14.2 Use homebrew if on Mac
brew install terraform
To build, test, scan, and deploy a new version of the docker image you just need to push a new tag. e.g.
git tag # list tags
git tag 1.2.3 # tag my version
git push --tags # push any commits with tags
pip install pipenv # if you haven't already
pipenv install --dev # download dependencies
pipenv shell # use pipenv env in shell
pipenv run pytest . # run commands in pipenv env (like tests)
# NOTE: source AWS creds before
pipenv run uvicorn app.main:app --reload
# NOTE: source AWS sbx creds before
docker-compose up --build # use '--build' if rebuilding
Auto hooks integration allows us to run pylint, black, and pytest before committing to source control.
pylintrc
is configured based off of the Google Python Style Guideblack
will auto format code on commit (it just works out of the box)pytest
is used to run unit and integration tests
autohooks activate --mode pipenv # activate pre-commit hooks
In order to get intellisense in VS Code, install the Python Extension First, we need the python path for our virtual pipenv environment:
pipenv run which python # Save this path to python for later, will be referenced as venv_path below
To configure it properly we must setup our .vscode/settings.json
as such:
{
"python.pythonPath": "{venv_path}/bin/python/",
"python.envFile": ".env",
"python.testing.cwd": ".",
"python.testing.unittestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.pytestEnabled": true,
"python.testing.pytestPath": "{venv_path}/bin/pytest",
"python.testing.pytestArgs": [".", "-c", "pytest.ini"],
"python.testing.autoTestDiscoverOnSaveEnabled": true,
"python.linting.pylintEnabled": true
}
After doing this intellisense should work in vscode, and tests should be discoverable/runnable
We use pytest-cov
to generate test coverage reports for the python code. We integrate this into out gitlab CI adhering to this gitlab code coverage doc
This project contains some code scanning stage examples in the .gitlab-ci.yml
.
See these docs for some more detials
For SAST (fortify) scanning to work we can create a requirements.txt
in the root of the project using the following:
pipenv lock -r > requirements.txt