Teststack
Teststack is a Rust utility crate that simplifies the setup and management of reusable test containers using the testcontainers library. It starts each container once per test suite and shares it across tests, reducing overhead and complexity.
Features
- Singleton containers per image type across tests
- Automatic cleanup at the end of the test run (or on Ctrl+C)
- Pluggable container support: PostgreSQL, MySQL, and custom containers
- Easy integration with test frameworks (sqlx::test, rstest)
Getting Started
Add teststack to your Cargo.toml:
[]
= { = "0.1", = ["postgres"] }
Example: Shared Container Across Tests
In the example below, both tests share the same Postgres container instance. This reduces startup overhead and speeds up test execution. The container is automatically shut down at the end of the test harness.
use stack;
async
Custom Containers and Configuration
The following example demonstrates how to run a RabbitMq container using teststack. It shows how to customize an existing testcontainers_module image with the ImageExt trait for advanced configuration.
use RabbitMq;
use ;
use DbContainer;
use ;
async
;
If you don't need any custom configuration, you can simplify the container setup by returning the image directly:
use RabbitMq;
use ;
async
Design Overview
All containers are shared per image type. Cleanup is performed once, when the test process exits or receives a Ctrl+C signal.
Cleanup Strategy
Test containers are gracefully cleaned up on process exit using ctor and dtor, with support for both async and blocking cleanup.
Features
postgres– enable PostgreSQL test container supportmysql– enable MySQL test container support