- Create Python virtual Environment
python3 -m venv ~/.venv
- Create empty files:
Makefile
,requirements.txt
,Dockerfile
,shared_code/__init__.py
- Populate
Makefile
- Improve test coverage
We create a makefile:
Advantage:
- we can incrementally check every step in the local cli before runnin the Github workflow
- eg: make test
- we simplify the steps in the workflow
install:
#install commands
pip install --upgrade pip &&\
pip install -r requirements.txt
format:
#format code
black *.py shared_code/*.py
lint:
#pylint with no refactor or convention msg's
pylint --errors-only --disable=no-self-argument --extension-pkg-whitelist='pydantic' *.py shared_code/*.py
test:
#test
python -m pytest -vv --cov=shared_code test_api.py
build:
#build container
deploy:
#deploy
all: install format lint test deploy
Goal: We want to execute, cover
, all lines of code to check execution.
Step 1:: We have following in our makefile
test:
#test
python -m pytest -vv --cov=shared_code test_api.py
Step 2: We can first run locally
make test
Execute in cli: coverage html
This will create a new foled htmlcov
and open index.html in browser (eg via liveserver)
Click: on the files in the browser to reveal the uncoverd lines
Eliminate unnecessary code or add some code for testing purpuses.
Eg adding tests: Here we added some code in the library weatherforcast.py
to cover or run every single line of code.
# Build your image with name = `solar-forecast-api`
docker build -t solar-forecast-api .
# Run a container it locally
docker run -p 8080:8080 --name -e OPENWEATHERMAP_API_KEY='<yourAPIkey>' cont_SFA solar-forecast-api