Unit tests with real dependencies

Testcontainers is an open source library for providing throwaway, lightweight instances of databases, message brokers, web browsers, or just about anything that can run in a Docker container.
How it works

Test dependencies as code

No more need for mocks or complicated environment configurations. Define your test dependencies as code, then simply run your tests and containers will be created and then deleted.

With support for many languages and testing frameworks, all you need is Docker.

GenericContainer redis = new GenericContainer("redis:5.0.3-alpine")
    .withExposedPorts(6379);
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
    ContainerRequest: testcontainers.ContainerRequest{
        Image:        "redis:5.0.3-alpine",
        ExposedPorts: []string{"6379/tcp"},
        WaitingFor:   wait.ForLog("Ready to accept connections"),
    },
    Started:          true,
})
RedisContainer redisContainer = new RedisBuilder().Build();
await redisContainer.StartAsync();
const redis = await new GenericContainer("redis:5.0.3-alpine")
    .withExposedPorts(6379)
    .withWaitStrategy(Wait.forLogMessage("Ready to accept connections"))
    .start();
redis = (
    DockerContainer("redis:5.0.3-alpine")
        .with_exposed_ports(6379)
)
redis.start()
wait_for_logs(redis, "Ready to accept connections")
let redis = GenericImage::new("redis", "7.2.4")
    .with_exposed_port(6379)
    .with_wait_for(WaitFor::message_on_stdout("Ready to accept connections"))
    .start();
redis <-
  run $
    containerRequest (TestContainers.fromTag "redis:5.0.3-alpine")
      & setExpose ["6379/tcp"]
      & setWaitingFor (waitUntilMappedPortReachable "6379/tcp")
redis = Testcontainers::DockerContainer.new("redis:5.0.3-alpine").with_exposed_port(6379)
redis.start
(def container (-> (tc/create {:image-name "redis:5.0.3-alpine")
                               :exposed-ports [6379]})
                   (tc/start!)))
{:ok, _} = Testcontainers.start_link()
config = %Testcontainers.Container{image: "redis:5.0.3-alpine"}
{:ok, container} = Testcontainers.start_container(config)
$container = (new RedisContainer())->start();

$redis = new \Redis();
$redis->connect($container->getHost(), $container->getFirstMappedPort());
int requestId = tc_new_container_request(DEFAULT_IMAGE);
tc_with_exposed_tcp_port(requestId, 8080);
tc_with_wait_for_http(requestId, 8080, "/__admin/mappings");
tc_with_file(requestId, "test_data/hello.json", "/home/wiremock/mappings/hello.json");
struct tc_run_container_return ret = tc_run_container(requestId);
int containerId = ret.r0;
if (!ret.r1) {
    printf("Failed to run the container: %s\n", ret.r2);
    return -1;
}
Modules

Test Anything You Can Containerize: Database, Message Broker, And More

Use cases

How Testcontainers® can help you

Data access layer integration tests