Setting Up Automated Test Execution on Pull Request
Auto-run tests on PR—basic protection against regressions. A developer cannot merge code that breaks existing functionality. This is not a replacement for code review, but its supplement: reviewer focuses on logic, not catching obvious bugs.
Pipeline Structure
A well-structured pipeline is split into parallel jobs with fail-fast strategy:
# .github/workflows/pr.yml
name: PR Tests
on:
pull_request:
branches: [main, develop]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true # Cancel old runs on new push
jobs:
lint:
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 run lint && npm run type-check
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 -- --coverage
- uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
integration:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_DB: testdb
POSTGRES_PASSWORD: test
options: >-
--health-cmd pg_isready
--health-interval 5s
--health-timeout 5s
--health-retries 5
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: postgresql://postgres:test@localhost:5432/testdb
Dependency Caching
# Cache node_modules by package-lock.json
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm # Built-in cache in actions/setup-node
# Or explicit via actions/cache
- uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
Without cache npm ci on cold runner—60–90 seconds. With cache—5–10 seconds.
Test Matrix
If the application must work on multiple versions of Node.js or PHP:
strategy:
matrix:
node-version: [18, 20, 22]
fail-fast: false # Run all versions even if one fails
PHP / Laravel
- name: Run PHPUnit
run: php artisan test --parallel --coverage-clover=coverage.xml
env:
DB_CONNECTION: pgsql
DB_DATABASE: testing
- name: Upload coverage
uses: codecov/codecov-action@v4
with:
files: coverage.xml
--parallel runs tests in parallel via brianium/paratest. On 200+ tests speeds up 3–4 times.
Status Checks and Branch Protection
In GitHub Settings → Branches → Branch protection rules add required status checks: lint, unit, integration. Merge to main without passing these checks is impossible.
Speed Optimization
- Path filtering—run tests only when relevant files change
- Test splitting—distribute tests across multiple runners (GitHub Actions matrix)
-
Only changed modules—Jest
--changedSince, pytest--testpaths
Goal: pipeline fits in 5 minutes. Slower—developers start ignoring it.
Timeline
Setting up basic pipeline with unit and integration tests for Node.js or PHP project—1–2 days. Setting up coverage reports and badge in README—0.5 days.







