How to run automated tests in GitHub Actions
npm test, pytest, test runner integration, step ordering, exit codes, job failure on test failure, test output in logs
Why Run Tests in CI?
The core value of CI is catching broken code before it reaches main. Every push should run your full test suite automatically. If tests fail, the workflow fails, the PR is blocked, and developers fix the issue immediately.
Node.js Example
name: CI
on:
pull_request:
branches: [main]
jobs:
test:
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 testHow Failure Propagates
npm test exits with code 1 when tests fail. GitHub Actions treats any non-zero exit code as a step failure, which fails the job, which fails the workflow. The PR will show a red X, preventing merge if branch protection rules are configured.
npm ci is preferred over npm install in CI because it installs exactly what is in package-lock.json, fails if the lockfile is out of sync, and is faster because it skips the dependency resolution step. Always use npm ci in CI pipelines.
