Stop 10 · Testing & Code QualityTopics · 8/8

CI with GitHub Actions for Python

Part of the Python Roadmap

In short

"I forgot to run the tests" should never be the reason a bug reaches users. Continuous integration runs your tests, linter and type checker on a clean machine for every push and pull request, automatically.

Continuous integration runs your tests, linter and type checker automatically on every push or pull request. A GitHub Actions workflow file sets up Python, installs dependencies and runs pytest on a fresh machine.

The jaadu stands as a security officer beside an airport conveyor belt, watching parcels roll through a scanner arch that glows green for each one.

Think of it like this

The airport security check

Every passenger at Delhi airport goes through the same scanner, no matter how trustworthy they look. If the alarm beeps, they don't board. CI is that scanner for your code: every commit goes through the same checks, and a red result blocks the merge.

Watch it happen

What runs on every pull request

Push / PR
Fresh runner
Install
Checks
Status

1/5You push a branch or open a pull request. GitHub reads .github/workflows/tests.yml.

The big ideas

What you need to know

A YAML file

Workflows live in .github/workflows/*.yml. on: says when, jobs: says what to run.

Clean machine

CI catches "works on my laptop" bugs: missing dependencies, uncommitted files, hard-coded paths.

Matrix builds

strategy.matrix runs the same job for several Python versions in parallel.

Secrets stay secret

Store API keys in repository Secrets and read them with secrets.NAME. Never write them in the YAML.

See it in code

Walk through the code

yaml
1name: tests2 3on:4  push:5    branches: [main]6  pull_request:7 8jobs:9  test:10    runs-on: ubuntu-latest11    strategy:12      matrix:13        python-version: ["3.12", "3.13"]14    steps:15      - uses: actions/checkout@v416      - uses: actions/setup-python@v517        with:18          python-version: ${{ matrix.python-version }}19          cache: pip20          cache-dependency-path: pyproject.toml21      - run: pip install -e ".[dev]"22      - run: ruff check .23      - run: ruff format --check .24      - run: mypy src25      - run: pytest --cov=kirana_billing --cov-fail-under=80

1/4Run on every push to main and on every pull request.

Save as .github/workflows/tests.yml. It assumes your dev extra includes pytest, pytest-cov, ruff and mypy.

Save yourself the pain

Common mistakes

Oops

Letting CI install whatever versions are newest that day.

Do this instead

Install from your pinned requirements or lock file so CI matches what you tested locally.

Oops

Ignoring a red CI run and merging anyway.

Do this instead

Turn on branch protection so pull requests can't merge until the checks pass.

Oops

Putting an API key directly in the workflow file.

Do this instead

Add it under Settings, Secrets and variables, and reference it as secrets.MY_KEY.

Your mission

Your first green tick

~30 min

Quick check

Your tests pass locally but fail in GitHub Actions with ModuleNotFoundError: No module named 'openpyxl'. What's the most likely cause?

Go deeper

  1. 1Add a workflow that runs pytest on every push to your repository.
  2. 2Add a ruff check step so style problems fail the build.
  3. 3Show the passing status badge in your README.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge