Run integration tests against a real database

DevOps, Hands On30 minutesFreeSteps checked 23 November 2026By Cloud Hands On

In this task you'll test your app against a real database inside the pipeline.

Unit tests check one function with nothing around it; integration tests check that your code works with the real things it talks to, such as a database. In GitHub Actions a service container starts a database next to your job, and a health check makes the job wait until it accepts connections.

Arrange tests as a pyramid: many fast unit tests on every push, fewer integration tests (still on every pull request), and a handful of slow end-to-end tests before release. Publish test results and coverage so a failure is easy to read, and fail the build if coverage drops below an agreed floor.

Example. Quayhurst Health's appointments service passed its unit tests but failed in staging because a column name differed in the real database. An integration test against PostgreSQL in a service container now catches that on the pull request.

Before you start

You need: A free GitHub account and a public test repository (Actions minutes are free for public repositories). Time: 30 minutes.

Where to look

  1. Repository
  2. .github/workflows/integration.yml
  3. Actions tab

Steps

  1. Add .github/workflows/integration.yml from Commands: a postgres:17 service with a health check and port 5432 mapped.
  2. In the steps, create a table with psql and run a query against it.psql is already installed on GitHub's Ubuntu runners.
  3. Push and open the run. In the job's set-up, see the service container start and wait until healthy.
  4. Remove the health check options and push again: the first query can fail because the database isn't ready yet.That's why health checks matter.
  5. Put the health check back, and add pytest --cov with a minimum (--cov-fail-under=80) to the unit test job.

Commands

jobs:
  integration:
    runs-on: ubuntu-latest
    services:
      db:
        image: postgres:17
        env:
          POSTGRES_PASSWORD: example-only
        ports: ['5432:5432']
        options: >-
          --health-cmd "pg_isready -U postgres"
          --health-interval 5s --health-timeout 5s --health-retries 10
    env:
      PGHOST: localhost
      PGUSER: postgres
      PGPASSWORD: example-only
    steps:
      - run: psql -c "create table orders(id serial primary key, total numeric)"
      - run: psql -c "insert into orders(total) values (19.99)"
      - run: test "$(psql -tAc 'select count(*) from orders')" = 1

Watch out for

Cost
Free in public repositories.
Security
The database password here is a throwaway test value in the workflow file; never point integration tests at a real database or reuse real passwords.
Common mistakes
No health check, so tests fail at random, and tests that depend on each other's data.

Clean up

Nothing to clean up: the service container is removed when the job ends.

Words to know

Integration test
A test that checks your code with a real dependency, such as a database.
Service container
A container GitHub starts beside a job, such as PostgreSQL.
Health check
A command that tells GitHub when the service is ready.
Coverage
The share of your code that tests run.

Tick off each step and track your progress in the guide.

Open this task in DevOps, Hands On

Keep going