Integration Testing C++ with Docker: A Practical Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Integration Testing C++ with Docker



Modern C++ development requires not only writing quality code, but also ensuring its reliable operation in real-world conditions. Integration testing is a key stage that allows you to check the interaction of system components: databases, network services, file systems. Using Docker for these purposes provides an isolated, reproducible environment that is ideal for CI/CD pipelines. In this article, we will look at how to set up integration testing of C++ applications using Docker, and provide practical examples.



Why Docker for C++ Integration Testing?



Traditional approaches to C++ integration testing often face problems:

  • Environment dependency: tests may work on the developer's machine but fail on the build server due to different library versions.
  • Setup complexity: testing with a real database or external API requires manual configuration.
  • Parallel execution: multiple test runs can conflict due to shared resources.

Docker solves these problems by providing lightweight, isolated containers. You can spin up a test database, run your C++ service, and execute tests — all within a single docker-compose file. This guarantees that the environment will be identical on any machine.



Project Setup: Basic Example



Let's consider a simple C++ service that saves data to PostgreSQL. For integration testing we will need:

  • The C++ code itself with tests (using Google Test).
  • A Dockerfile for building and running tests.
  • A docker-compose.yml for orchestrating containers.


Project structure:

my_cpp_project/├── src/│   ├── main.cpp│   ├── database.cpp│   └── database.h├── tests/│   └── test_integration.cpp├── CMakeLists.txt├── Dockerfile└── docker-compose.yml


Dockerfile:

FROM gcc:latest AS builder

WORKDIR /appCOPY . .

RUN apt-get update && apt-get install -y cmake libpq-devRUN cmake -B build -DCMAKE_BUILD_TYPE=ReleaseRUN cmake --build build

FROM ubuntu:22.04

RUN apt-get update && apt-get install -y libpq-devCOPY --from=builder /app/build/tests /app/testsCOPY --from=builder /app/build/my_app /app/my_app

ENTRYPOINT ["/app/tests"]


docker-compose.yml:

version: '3.8'services:  db:    image: postgres:15    environment:      POSTGRES_USER: testuser      POSTGRES_PASSWORD: testpass      POSTGRES_DB: testdb    ports:      - "5432:5432"    healthcheck:      test: ["CMD-SHELL", "pg_isready -U testuser -d testdb"]      interval: 5s      timeout: 5s      retries: 5

test: build: . depends_on: db: condition: service_healthy environment: DB_HOST: db DB_PORT: 5432 DB_USER: testuser DB_PASSWORD: testpass DB_NAME: testdb


Writing Integration Tests in C++



Now let's create a test that checks writing to and reading from the database. We'll use Google Test and the libpqxx library for working with PostgreSQL.



tests/test_integration.cpp:

#include <gtest/gtest.h>#include <pqxx/pqxx>#include <string>

class DatabaseTest : public ::testing::Test {protected: void SetUp() override { conn = new pqxx::connection( "host=" + std::string(std::getenv("DB_HOST")) + " port=" + std::string(std::getenv("DB_PORT")) + " user=" + std::string(std::getenv("DB_USER")) + " password=" + std::string(std::getenv("DB_PASSWORD")) + " dbname=" + std::string(std::getenv("DB_NAME"))); }

void TearDown() override { delete conn; }

pqxx::connection* conn;};

Blogs

Book Recommendations