Integration Testing Swift with Docker: A Complete Guide
Integration testing is a key stage in developing high-quality Swift applications. It checks how different modules, services, and external dependencies interact with each other. Docker simplifies this process by providing an isolated environment that can be easily reproduced on any machine. In this article, we will look at how to set up integration testing for a Swift project using Docker, including examples for iOS and macOS.
Why use Docker for Swift integration testing?
Traditional Swift application testing often suffers from environment issues: different versions of databases, servers, and libraries. Docker solves these problems:
- Isolation: each test runs in a clean container, eliminating the influence of other processes.
- Reproducibility: the same environment locally, on the CI server, and for other developers.
- Fast deployment: containers start in seconds, not minutes.
- Scalability: easily run parallel tests with different versions of dependencies.
Docker is especially useful for testing network interactions, databases (e.g., PostgreSQL or Redis), and microservice architecture.
Setting up a Docker environment for Swift
To start, let's create a simple Swift project and set up Docker. Suppose we have an application that works with a PostgreSQL database.
1. Creating a Swift project
Create a new project using Swift Package Manager:
mkdir SwiftIntegrationTestcd SwiftIntegrationTestswift package init --type executableAdd a dependency for working with PostgreSQL. Open Package.swift and add:
// swift-tools-version:5.9import PackageDescription
let package = Package( name: "SwiftIntegrationTest", dependencies: [ .package(url: "https://github.com/vapor/postgres-nio.git", from: "1.18.0") ], targets: [ .executableTarget( name: "SwiftIntegrationTest", dependencies: [ .product(name: "PostgresNIO", package: "postgres-nio") ]), .testTarget( name: "SwiftIntegrationTestTests", dependencies: ["SwiftIntegrationTest"]) ])2. Dockerfile for Swift
Create a Dockerfile file in the project root:
FROM swift:5.9-focal
WORKDIR /app
# Copy project filesCOPY Package.swift .COPY Sources/ ./Sources/COPY Tests/ ./Tests/
# Build the project (without tests to speed up)RUN swift build -c release
# Test run commandCMD ["swift", "test", "--enable-test-discovery"]3. Docker Compose for dependencies
Create docker-compose.yml to run tests with PostgreSQL:
version: '3.8'
services: postgres: image: postgres:16 environment: POSTGRES_DB: testdb POSTGRES_USER: testuser POSTGRES_PASSWORD: testpass ports: - "5432:5432" healthcheck: test: ["CMD-SHELL", "pg_isready -U testuser -d testdb"] interval: 5s timeout: 5s retries: 5
swift-tests: build: . depends_on: postgres: condition: service_healthy environment: DATABASE_HOST: postgres DATABASE_PORT: 5432 DATABASE_NAME: testdb DATABASE_USER: testuser DATABASE_PASSWORD: testpassWriting integration tests in Swift
Now let's write a test that checks the database connection and query execution.
1. Class for working with the database
Create a file Sources/SwiftIntegrationTest/DatabaseService.swift:
Blogs
Book Recommendations
Read also