A library for integration testing against docker containers from within Rust.
This crate is the official Rust language fork of
Tests should be self-contained and isolated. While this is usually easy for unit-tests, integration-tests typically require a more complex environment. The testcontainers ecosystem facilitates self-contained and isolated integration tests. It allows to easily spin up Docker containers from within your tests and removes them afterwards.
A very typical usecase for testcontainers are integration-tests of persistence layers. These require an actual database to be present. Using testcontainers, your tests can spin up database containers themselves, without the need for any other setup.
- Run integration tests in parallel (because each test sets up its own environment)
- Run integration tests the same way you run unit tests (
cargo testand you are fine)
Unsurprisingly, working with testcontainers is very similar to working with Docker itself.
First you choose a
Client. Given a client instance, you can
Images. This gives you back a
Container. Containers implement
Drop. As soon as they go out of scope, the underlying docker container is removed.
Usage in production code
Although nothing inherently prevents testcontainers from being used in production code, the library itself was not designed with that in mind. For example, many methods will panic if something goes wrong but because the usage is intended to be within tests, this is deemed acceptable.
The testcontainers ecosystem is split into multiple crates, however, the
testcontainers crate itself is a meta-crate, re-exporting the others. Usually, depending on
testcontainers should be sufficient for most users needs.