37 releases (22 breaking)

0.23.3 Feb 19, 2025
0.23.1 Sep 26, 2024
0.21.0 Jul 30, 2024
0.15.0 Oct 5, 2023
0.3.1 Oct 23, 2018

#17 in Testing

Download history 56370/week @ 2024-12-05 58248/week @ 2024-12-12 34923/week @ 2024-12-19 20634/week @ 2024-12-26 43648/week @ 2025-01-02 56477/week @ 2025-01-09 55442/week @ 2025-01-16 56582/week @ 2025-01-23 63201/week @ 2025-01-30 68430/week @ 2025-02-06 60345/week @ 2025-02-13 71653/week @ 2025-02-20 67252/week @ 2025-02-27 78197/week @ 2025-03-06 78555/week @ 2025-03-13 63258/week @ 2025-03-20

300,336 downloads per month
Used in 216 crates (165 directly)

MIT/Apache

225KB
5K SLoC

Testcontainers-rs

Continuous Integration Crates.io Docs.rs Slack

Testcontainers-rs is the official Rust language fork of http://testcontainers.org.

Usage

testcontainers is the core crate

The crate provides an API for working with containers in a test environment.

  1. Depend on testcontainers
  2. Implement testcontainers::core::Image for necessary docker-images
  3. Run it with any available runner testcontainers::runners::* (use blocking feature for synchronous API)

Example:

  • Blocking API (under blocking feature)
use testcontainers::{core::{IntoContainerPort, WaitFor}, runners::SyncRunner, GenericImage, ImageExt};

#[test]
fn test_redis() {
    let container = GenericImage::new("redis", "7.2.4")
        .with_exposed_port(6379.tcp())
        .with_wait_for(WaitFor::message_on_stdout("Ready to accept connections"))
        .with_network("bridge")
        .with_env_var("DEBUG", "1")
        .start()
        .expect("Failed to start Redis");
}
  • Async API
use testcontainers::{core::{IntoContainerPort, WaitFor}, runners::AsyncRunner, GenericImage, ImageExt};

#[tokio::test]
async fn test_redis() {
    let container = GenericImage::new("redis", "7.2.4")
        .with_exposed_port(6379.tcp())
        .with_wait_for(WaitFor::message_on_stdout("Ready to accept connections"))
        .with_network("bridge")
        .with_env_var("DEBUG", "1")
        .start()
        .await
        .expect("Failed to start Redis");
}

Ready-to-use images

The easiest way to use testcontainers is to depend on ready-to-use images (aka modules).

Modules are available as a community-maintained crate: testcontainers-modules

License

Licensed under either of

at your option.

Dependencies

~22–39MB
~710K SLoC