Skip to content

Automating Tests with GitHub Actions

diagram what a CI run does with a push mermaid
The value of CI is not that it runs your tests -- you can do that -- but that it runs them somewhere you did not configure, from a clean checkout, on every push. Most of what it catches is not a broken test; it is a missing dependency, a file that was never committed, or something that only worked because of state on one machine.

GitHub Actions runs workflows on:

  • push
  • pull_request
  • schedules

Create .github/workflows/tests.yml:

yaml
name: tests
 
on:
  pull_request:
  push:
 
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
 
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.11"
 
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
 
      - name: Run tests
        run: |
          pytest -q
  • cache pip if workflows are slow
  • upload coverage/html reports as artifacts
  • run lint/type checks as separate jobs

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading