一个库,用于在 Rust 中对 Docker 容器进行集成测试。
Testcontainers-rs 是 http://testcontainers.org 的官方 Rust 语言分支。
testcontainers 是核心 crate该框架提供了一个 API,用于在测试环境中处理容器。
testcontainerstestcontainers::core::Imagetestcontainers::runners::* 运行它(使用 blocking 功能来实现同步 API)blocking 功能下)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");
}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");
}使用 testcontainers 的最简单方法是依赖即用型镜像 (即模块)。
模块以社区维护的 crate 形式提供: testcontainers-modules
暂无开放 Issues,或尚未同步最近议题。