Start learning free

LESSON 155 OF 188 · BACKEND: SHIPPING IT

Run the Tests on Every Push in Python

Practice

Continuous integration (CI) means your tests run by themselves every time you push. On GitHub you ask for it with a workflow: a YAML file in the folder .github/workflows/, made of key: value lines nested by their indentation. The key on says which event starts it, and each job runs its steps from top to bottom on a fresh machine, the runner.

Here is a small workflow of another kind. It starts on every pull request and runs one command:

name: Hello
on: pull_request
jobs:
  greet:
    runs-on: ubuntu-latest
    steps:
      - run: echo Hello from the runner

A step either uses a ready-made action, such as actions/checkout, which copies your repository onto the runner, or runs a command, as you would type it in a terminal. The runner starts with its own Python 3.12 and nothing of yours: no code, no Flask. So the order matters: copy the code, set up the server's Python, install requirements.txt with pip install -r, then run the tests. The job stops at the first step that fails.

You write the workflow as a Python string named workflow, one line per piece, each ending in \n. The check reads it the way GitHub would, runs the job and must print:

Workflow: Tests
Starts on: push
Job test, on a fresh ubuntu-latest machine
Step 1: uses actions/checkout@v6
  copied the repository: app.py, requirements.txt, test_app.py
Step 2: uses actions/setup-python@v6 with python-version 3.13
  Python 3.13 is ready
Step 3: run pip install -r requirements.txt
  installed Flask==3.1.3 and gunicorn==23.0.0 into Python 3.13
Step 4: run python -m unittest
  Ran 3 tests: OK
The job passed: every push now runs the tests on Python 3.13.

Your task: the starting workflow already has its name and the job test on ubuntu-latest. Add the trigger on: push and four steps: check out the code with actions/checkout@v6, set up Python '3.13' with actions/setup-python@v6, install the packages from requirements.txt, and run the tests with python -m unittest.


Assignment
Follow the Coding Tutorial to write a GitHub Actions workflow that runs the shop's tests on Python 3.13 on every push.


Hint
Indentation is the structure, and it must be spaces: the step items start 6 spaces in with a dash, with: sits 8 spaces in under its uses, and python-version 10. Write the version in quotes, '3.13', so YAML keeps it as text.


STEP 1 OF 7
    Ctrl + Enter to check

    Ship Ci in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free