Automated Testing in CI Pipelines Explained
You push a one-line fix and open a pull request. A few minutes later a red cross appears next to it, and the repository will not let you merge. A check failed, but which one, and why does it stop you? This lesson explains testing in CI: which checks run and in what order, how CI decides pass or fail, how to keep tests fast and trustworthy, and how to read a failed run.
What a pipeline checks, cheapest first
An automated test is code that runs part of your program and checks the result, for example that a discount function returns 90 for a price of 100 and a 10 percent discount. A typical pipeline runs five kinds of checks, roughly fastest first:
- Linting and formatting. A linter reads code without running it and flags likely mistakes, such as a variable used but never defined. A format check fails if the layout breaks the team's style.
- Unit tests, from lesson 4, test one piece of code, such as a function, with no network or database.
- Integration tests check that parts work together, for example that your code really saves a row in a database.
- End-to-end tests drive the whole running app as a user would: open a browser, log in, place an order.
- Security and dependency scans. A scanner such as pip-audit compares your libraries' versions with a public list of known vulnerabilities and exits with code 1 if it finds one.
In that order, a misspelt variable fails the run in seconds, before anyone waits for browser tests. Teams often run lint and unit tests side by side, but slow checks start only after cheap ones pass.
Why so few end-to-end tests? Each is slow, needs the whole system running, and can fail for reasons unrelated to your change. The test pyramid is a rule of thumb for the mix: many unit tests, fewer integration tests, very few end-to-end tests. Notice its two arrows point opposite ways: the higher a test sits, the more it proves, and the fewer you can afford.
How CI decides pass or fail: the exit code
Every step in a pipeline is a command, such as pytest, or ruff check . for the Ruff linter. When a program finishes, it hands back an exit code, a small whole number: 0 means success, anything else means failure. CI reads that number, not the test report.
GitHub Actions follows this rule. A failed step fails its job, the job's later steps are skipped, and the log ends with this line from the runner:
Error: Process completed with exit code 1.
pytest, a popular Python test runner, returns 1 when some tests failed and 5 when it found no tests at all, so an empty run never counts as a pass.
One trap: a shell's exit code is that of its last command, so pytest; echo done exits with 0 even when tests fail. GitHub Actions runs run: steps on Linux and macOS with bash -e, and -e stops the shell at the first failing command. The try-it shows both.
Keeping the suite fast and trustworthy
A slow suite tempts people to skip it, and one that fails at random teaches them to ignore it. Four habits prevent both.
Run in parallel. GitHub Actions runs a workflow's jobs at the same time unless you link them with needs:; a job whose needed job failed is skipped. Within a job, the pytest-xdist plugin's pytest -n auto starts one test worker per CPU.
Cache dependencies. Every run starts on a fresh machine, so installing libraries can outlast the tests. A cache keeps downloaded packages between runs, keyed on a hash (a fingerprint) of your dependency file, so they download again only when it changes.
In this trimmed workflow, lint and unit-tests start together, cache: 'pip' caches Python packages, and integration-tests waits for both:
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/setup-python@v7
with:
python-version: '3.12'
cache: 'pip'
- run: pip install -r requirements.txt
- run: ruff check .
unit-tests:
runs-on: ubuntu-latest
steps:
# same setup steps as lint
- run: pytest -n auto tests/unit
integration-tests:
needs: [lint, unit-tests]
runs-on: ubuntu-latest
steps:
# same setup steps as lint
- run: pytest tests/integration
Each run: line is a gate with its own exit code, and requirements.txt must list pytest-xdist for -n auto to work. You will write real workflows from lesson 12 on.
Quarantine flaky tests. A flaky test passes and fails on the same code, often because of timing, shared data or a network call. Re-running until green teaches everyone that red means nothing. Move it at once into a job that is not required for merging, then fix or delete it.
Keep tests independent. Each test should create its own data and clean up. If test B passes only because test A left a row behind, B breaks when the order changes. The pytest-randomly plugin shuffles the order on every run and prints a seed that --randomly-seed can replay.
Make red block the merge: required status checks
A red cross is only advice until you make it a rule. A status check is the pass or fail result GitHub shows for a commit; each Actions job reports one. A protected branch is a branch with rules set by a repository admin, such as "no merge until these checks pass".
On GitHub, open Settings, then Branches, and click Add classic branch protection rule. Enter main as the Branch name pattern, select Require status checks to pass before merging, search for the jobs to require, such as lint, and click Create.
Now the chain below holds: exit code 1, failed job, red cross, merge blocked. Without the rule, people can merge despite the red cross.
A required check that never reports, for example because a path filter skipped its workflow, stays pending and blocks the merge too. And admins can bypass the rule unless Do not allow bypassing the above settings is selected.
Reading a failed run
- Find the first red step. Open the failed check from the pull request, or the run in the Actions tab, and click the failed job; GitHub expands failed steps. Later failures are often knock-on effects.
- Read the log upwards from the exit-code line: the tool's own message sits just above it. The Search logs box finds words such as
FAILED. - Reproduce it locally with the exact command from the step and the same language version or container image.
Here is the end of a failed pytest step (trimmed; the test name and counts are illustrative):
FAILED tests/unit/test_price.py::test_discount - assert 95 == 90
========================= 1 failed, 41 passed in 0.82s =========================
Error: Process completed with exit code 1.
Bottom up: the runner reports exit code 1, pytest counts one failure, and the FAILED line names the test and the values compared. If it passes on your laptop but fails in CI, look for what differs: an environment variable, a version, or test order.
Check yourself
A teammate wants the slow end-to-end suite to run first, "because it catches the most bugs". What does the team lose?
Fast feedback: a lint error that would fail in seconds now waits behind the slowest stage, and every change pays for the expensive run.
A CI tool runs pytest; echo "tests finished" with plain sh -c, and a test fails. Why does the step pass?
The shell's exit code is its last command's, and echo succeeded, so the step reports 0. Adding set -e lets pytest's exit code of 1 fail the step.
Branch protection on main requires a check called unit-tests, and someone renames that job to tests. What happens to new pull requests?
They cannot merge: no job reports unit-tests any more, so the required check stays pending. An admin must update the rule to require tests.