Module 2 · Intermediate · CI/CD pipelines

Run tests and linters automatically in CI

Run tests, linters and formatters automatically in CI with GitHub Actions for Python and Node.js: workflows, coverage, speed and required status checks.

By theprogrammers team · · 6 min read

Tests only protect you if they actually run. On a busy team, people forget, run only the tests they think matter, or test on a laptop that's set up differently from the server. Continuous integration (CI) fixes this by running every check automatically on every push and pull request, on a clean machine, and showing a clear pass or fail before anything is merged.

The GitHub Actions CI/CD tutorial built a first workflow. This lesson goes further: it shows complete CI workflows for Python and Node.js that run linters, formatters, type checks and tests, explains how to keep CI fast, and how to make main refuse code that fails.

What should CI check?

Order the checks from fastest to slowest, so obvious mistakes fail in seconds:

CheckCatchesPython toolsNode.js tools
FormattingInconsistent styleRuff format, BlackPrettier
LintingLikely bugs, unused imports, bad patternsRuff, Flake8ESLint
Type checkingWrong types passed aroundmypy, PyrightTypeScript (tsc)
Unit testsLogic errors in small piecespytestJest, Vitest, node --test
Integration testsPieces not working together, such as with a databasepytestJest, Supertest
BuildCode that doesn't compile or bundlepython -m buildnpm run build

A linter reads your code without running it and flags problems, like a grammar checker. A formatter rewrites the code into one consistent style, so code reviews can focus on logic instead of spaces and quotes.

A Python CI workflow

Save this as .github/workflows/ci.yml in a Python project that has a requirements.txt and pytest tests:

name: CI

on:
  push:
    branches: [main]
  pull_request:

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-python@v6
        with:
          python-version: "3.13"
      - run: pip install ruff
      - run: ruff format --check .
      - run: ruff check .

  test:
    runs-on: ubuntu-latest
    needs: lint
    strategy:
      matrix:
        python-version: ["3.12", "3.13"]
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-python@v6
        with:
          python-version: ${{ matrix.python-version }}
          cache: pip
      - run: pip install -r requirements.txt pytest pytest-cov
      - run: pytest --cov=src --cov-report=term-missing

What each part does:

  • Two jobs. lint runs first; needs: lint makes test wait for it, so a style error fails fast without spending minutes on tests.
  • ruff format --check fails if any file isn't formatted, without changing it. Developers run ruff format . locally to fix it.
  • ruff check is the linter.
  • The matrix runs the tests on two Python versions in parallel. Note the versions are quoted, because 3.10-style numbers otherwise lose their trailing zero, as explained in YAML for beginners.
  • cache: pip reuses downloaded packages between runs.
  • --cov measures test coverage: which lines the tests actually ran. term-missing lists the lines no test touched.

As with any action, check the GitHub Marketplace page of actions/setup-python for its latest major version.

A Node.js CI workflow

For a Node.js project whose package.json has lint, format:check and test scripts:

name: CI

on:
  push:
    branches: [main]
  pull_request:

jobs:
  check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-node@v6
        with:
          node-version: 24
          cache: npm
      - run: npm ci
      - run: npm run format:check
      - run: npm run lint
      - run: npm test
      - run: npm run build --if-present

The matching scripts in package.json:

{
  "scripts": {
    "format:check": "prettier --check .",
    "lint": "eslint .",
    "test": "node --test"
  }
}

Keeping the commands in package.json (or a Makefile) means developers run exactly the same checks locally as CI does: npm run lint on a laptop and in the pipeline are the same thing.

Tests that need a database

Integration tests often need a real database. GitHub Actions can start one as a service container next to your job:

  integration:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:17
        env:
          POSTGRES_PASSWORD: test
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 5s
          --health-retries 10
    env:
      DATABASE_URL: postgresql://postgres:test@localhost:5432/postgres
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-python@v6
        with:
          python-version: "3.13"
      - run: pip install -r requirements.txt pytest
      - run: pytest tests/integration

The health options make the job wait until PostgreSQL is ready. The password here is a throwaway for a temporary test database, so it's fine in the file; real passwords belong in secrets.

Make main refuse failing code

CI is only useful if a red result actually stops a merge. On GitHub, go to Settings → Branches (or Rules) and add a rule for main:

  • Require a pull request before merging, with at least one approval.
  • Require status checks to pass, and select your CI jobs (such as lint and test).
  • Require branches to be up to date before merging, so checks ran against the latest main.

Now nobody, including admins if you choose, can merge code whose tests fail.

Keep CI fast

Slow CI gets ignored. Aim for under about 10 minutes.

  • Cache dependencies (cache: pip or cache: npm).
  • Fail fast: cheap checks first, in a separate job.
  • Run jobs in parallel: lint, unit tests and the build don't need to wait for each other unless one depends on another.
  • Split slow test suites across several parallel jobs.
  • Skip work that isn't needed, for example with paths: filters so a docs-only change doesn't run the full test suite.
  • Cancel outdated runs when a new commit is pushed to the same branch:
concurrency:
  group: ci-${{ github.ref }}
  cancel-in-progress: true

Flaky tests

A flaky test passes sometimes and fails sometimes with no code change, often because of timing, test order, network calls or shared data. Flaky tests are dangerous: people learn to click "re-run" and ignore red results, and then real failures slip through. Fix them quickly, or temporarily quarantine them with a ticket to fix, but don't let them become normal.

Run the same checks before you push

Catch problems before CI does:

  • Run ruff check . or npm run lint before committing.
  • Use pre-commit hooks (the pre-commit tool for Python, or Husky for Node.js) to run the formatter and linter automatically on each commit.
  • Set your editor to format on save.

CI is the safety net; local checks are the habit.

Practice

  1. Your tests take 12 minutes and developers push without waiting. Name three ways to speed CI up.
Show the answer

Cache dependencies, split tests into parallel jobs, run linting in a separate fast job first, cancel superseded runs with concurrency, and skip unaffected work with path filters.

  1. A PR's tests pass, but main breaks right after merging. How can that happen, and which setting helps?
Show the answer

The PR was tested against an old version of main; another change merged in between, and the two conflict in behaviour. "Require branches to be up to date before merging" (or a merge queue) makes checks run against the latest main.

  1. What does ruff format --check . do differently from ruff format .?
Show the answer

--check only reports files that aren't formatted and exits with an error, without changing anything, which is what CI needs. Without it, Ruff rewrites the files.

Interview questions

What is continuous integration?

The practice of merging small changes into a shared branch frequently, with every change automatically built and tested on a clean environment, so problems are found within minutes of being introduced.

What checks would you put in a CI pipeline?

Formatting, linting, type checks, unit tests, integration tests and a build, ordered fastest first, with coverage reporting and security scanning of dependencies as useful extras.

How do you prevent broken code from being merged?

Branch protection rules that require passing status checks, code review approval and an up-to-date branch before merging into main.

What is a flaky test and how do you handle it?

A test that fails intermittently without code changes, due to timing, ordering, shared state or external services. Track it, fix the root cause (isolation, mocks, deterministic waits), and quarantine it temporarily if needed, rather than habitually re-running.

What to learn next

  1. Handle API keys and passwords safely in secrets and environment variables in GitHub Actions.
  2. Ship what passes CI safely with deployment strategies.