talira

Testcontainers

Unit tests with real dependencies

open sourceMITself-hostablefree

Testcontainers is an open source library for tests that need real dependencies: from test code it starts throwaway, lightweight instances of databases, message brokers, web browsers or anything else that runs in a container, waits until they're ready and removes them afterwards. It has implementations for Java, Go, .NET, Node.js, Python, Rust, Ruby, PHP, Haskell, Clojure, Elixir, Scala and native code, and about 140 ready-made modules (PostgreSQL, Kafka, Redis, LocalStack, Ollama and more); the Java library integrates with JUnit 4, JUnit 5 and Spock. It needs a Docker-API compatible runtime: Docker Engine on Linux, Docker Desktop or Testcontainers Cloud, with Podman, Colima and Rancher Desktop usable but not actively tested. AtomicJar made it, and Docker, which acquired AtomicJar in December 2023, keeps the libraries open source: MIT for Java, Go, Node.js and .NET, Apache-2.0 for Python, MIT or Apache-2.0 for Rust.

Problems it solves

  • integration tests against a real Postgres instead of an in-memory fake like H2
  • spin up a real database in a Docker container for each integration test run and throw it away afterwards
  • spin up Kafka in a container for a test run and tear it down afterwards
  • our tests share one database and keep stepping on each other's data
  • mocks of our cloud services in tests don't catch real bugs
  • start Redis and Elasticsearch from test code without a docker-compose file
  • run Selenium browser tests in throwaway containers
  • integration tests that work the same on every developer's laptop and in CI
  • test against the exact database version we run in production
  • test our AWS code against LocalStack from the test suite
  • start a local LLM with Ollama for integration tests
  • wait until a container's service is actually ready before the test starts
  • a JUnit 5 extension that manages container lifecycles for tests
  • Go integration tests with real dependencies running in Docker

Features

  • throwaway containers started and removed from test code
  • libraries for Java, Go, .NET, Node.js, Python, Rust, Ruby, PHP, Haskell, Clojure, Elixir, Scala and native code
  • about 140 modules in 11 categories: relational and NoSQL databases, message brokers, cloud emulators, vector databases, inference engines and more
  • wait strategies that hold the test until a service is ready
  • networking, command execution, file copying, container logs and images built on the fly
  • JUnit 4, JUnit 5 (Jupiter) and Spock integrations in Java
  • reusable containers (experimental in Java)
  • image name substitution for private registries and mirrors
  • runs against Docker Engine, Docker Desktop or Testcontainers Cloud

Limitations

  • Needs a Docker-API compatible container runtime: Podman, Colima and Rancher Desktop work with extra setup but aren't actively tested1

Capabilities

Has official SDKsplan not known