Python Project Scaffold

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

python3 -m venv ~/.scaffold
source ~/.scaffold/bin/activate

Example code

def toyou(x):
    return f"hi {x}"

def add(x):
    return x + 1

def subtract(x):
    return x - 1
    

Example test code

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

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

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:

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
    
 

Leave a comment

Your email address will not be published. Required fields are marked *