Sub topic 4
Running automated tests on every change
This chapter assumes you can write and run tests for a small application, use Git branches and pull requests, and read simple YAML. It covers how tests fit into a CI pipeline, how to order them, and the errors that make a test suite unreliable.
Why tests run on every change
A pipeline that only builds the code proves the code compiles. It does not prove the code works. Automated tests turn each commit into a question: "Did this change break anything we already know how to check?" The pipeline answers within minutes, while the author still remembers the change.
The test step should be a gate. If any test fails, the job fails, and later steps such as packaging or deployment do not run. A pull request can also be blocked from merging until the check passes. You set this up with a branch protection rule that requires the status check.
The test pyramid in a pipeline
Different tests trade speed for realism. A common arrangement runs the cheap tests first, so a broken change fails quickly.
| Test type | What it checks | Typical speed | Where it runs |
|---|---|---|---|
| Unit | One function or class in isolation | Milliseconds | Every commit, first |
| Integration | Several parts together, such as code and a database | Seconds | Every commit, after unit tests |
| End-to-end | The whole system through its real interface | Minutes | Later stage or before release |
Splitting tests into jobs
You can run test types as separate jobs. Jobs without dependencies run in parallel, which shortens the total time. The needs key makes a job wait for another.
name: tests
on:
push:
branches: [main]
pull_request:
jobs:
unit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm test
integration:
needs: unit
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_PASSWORD: example
ports:
- 5432:5432
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm run test:integration
env:
DATABASE_URL: postgres://postgres:example@localhost:5432/postgresThe services block starts a throwaway database container for the job. Each run gets a fresh one, so tests do not depend on leftover data. The password here is a disposable value for a container that exists only during the run. Real credentials belong in the platform's secret store.
This example assumes your package.json defines test and test:integration scripts.
What makes a test trustworthy
A test suite is only useful if the team believes its results. Watch for these common errors:
- Flaky tests pass and fail on the same code. Causes include timing assumptions, shared state between tests, and dependence on network services. Teams learn to rerun and ignore failures, and then real failures get ignored too.
- Environment drift means the test environment differs from the one on a developer's machine. Pin versions of the runtime and dependencies, and use
npm cirather thannpm installso the lockfile is respected. - Order dependence means one test only passes because another ran before it. Make each test set up its own data.
- Slow suites push developers to skip the pipeline. Parallelize, cache dependencies, and move slow tests to a later stage.
- Missing failure signal happens when a step ends with a success code despite failing tests, for example a script that swallows the exit code. Confirm that a deliberately broken test turns the job red.
Coverage is a signal, not a goal
Coverage reports show which lines the tests executed. Low coverage in a changed file is a useful warning. High coverage does not prove the assertions are meaningful, since a test can run code without checking its result. Use coverage trends to spot neglected areas, and avoid a hard percentage target that encourages empty tests.
Exercise
Your pipeline has a 12-minute end-to-end suite that runs before the 20-second unit tests, and developers wait for it on every push. What change would you make, and why?
Show answer
Run the unit tests first and make the later suites depend on them with needs, so a broken change fails in seconds. Move the end-to-end suite to a later stage, or run it only on pull requests to main or before release. Parallelize it if possible. This gives fast feedback on most failures and reserves the slow suite for changes that already passed the cheap checks.
Exercise
A test fails about one run in ten with no code change. Why is leaving it in the required checks harmful, and what should you do instead?
Show answer
Repeated false failures teach the team to rerun and ignore red builds, so real failures get missed. Quarantine the test in a non-blocking job, find the cause (often shared state, timing, or a network dependency), fix it, then return it to the required checks.