Skip to content

Instantly share code, notes, and snippets.

@riccardo1980
Last active February 9, 2024 20:19
Show Gist options
  • Save riccardo1980/11a92a0bfac23306b91d7ea7b4104605 to your computer and use it in GitHub Desktop.
Save riccardo1980/11a92a0bfac23306b91d7ea7b4104605 to your computer and use it in GitHub Desktop.
Basic GitHub Actions for linting/testing using flake8 and pytest

linting/testing base GitHub Action (flake8 & pytest)

You will find template of required files (requirements.txt, requirements-test.txt, project_package/base.py, tests/base_test.py, python-app.yml) at the end of this gist.

  1. Add base functionality and test files:
    • project_package/base.py
    • tests/base_test.py
  2. Add test requirements
    • requirements-test.txt
  3. Check whether your folders and files structure looks like:
.
├── project_package
│   ├── base.py
│   └── __init__.py
├── .github
│   └── workflows
│       └── python-app.yml
├── requirements-test.txt
├── requirements.txt
└── tests
    └── base_test.py
  1. [Optional] do a pytest and flake local run (from root project folder):
python -m pytest
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
  1. Add workflow definition:
  • .github/workflows/python-app.yml
  1. At a fresh push/pull request on main branch, you'll see this wokflow running!

Files templates

requirements-test.txt

flake8==3.9.0
pytest==6.0.1

requirements.txt

% insert here project requirements

project_package/base.py

def runme():
    return 1

tests/base_test.py

from project_package import base


def test_base():
    return 0


def test_run():
    assert base.runme() == 1

.github/workflows/python-app.yml

# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Python application

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Set up Python 3.7
      uses: actions/setup-python@v2
      with:
        python-version: 3.7
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
    - name: Install dependencies for testing
      run: |
        if [ -f requirements-test.txt ]; then pip install -r requirements-test.txt; fi
    - name: Lint with flake8
      run: |
        # stop the build if there are Python syntax errors or undefined names
        flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
        # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
        flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
    - name: Test with pytest
      run: |
        python -m pytest
@Trivoz
Copy link

Trivoz commented Mar 3, 2023

Very helpful, cheers 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment