create a project scaffold or project template to get started faster with python projects
We will create a virtual environment inside our project to avoid any incompatibility issues
1 2 |
python3 -m venv ~/.scaffold source ~/.scaffold/bin/activate |
Example code
1 2 3 4 5 6 7 8 9 |
def toyou(x): return f"hi {x}" def add(x): return x + 1 def subtract(x): return x - 1 |
Example test code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
from hello import toyou, add, subtract def setup_function(function): print(f" Running Setup: {function.__name__}") function.x = 10 def teardown_function(function): print(f" Running Teardown: {function.__name__}") del function.x ### Run to see failed test #def test_hello_add(): # assert add(test_hello_add.x) == 12 def test_hello_subtract(): assert subtract(test_hello_subtract.x) == 9 |
We need then a Makefile to automate builds and tests
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
install: pip install --upgrade pip &&\ pip install -r requirements.txt format: black *.py lint: pylint --disable=R,C hello.py test: python -m pytest -vv --cov=hello test_hello.py all: install lint test |
Then we will create the requirements file
1 2 3 4 5 |
pylint pytest click black pytest-cov |
The .github/workflows/pythonapp.yml file that stores the commands to build and test the project on Github Actions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
name: Python application test with Github Actions on: [push] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python 3.8 uses: actions/setup-python@v1 with: python-version: 3.8 - name: Install dependencies run: | make install - name: Lint with pylint run: | make lint - name: Test with pytest run: | make test - name: Format code run: | make format |