百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
T

testcontainers-rs

> DevOps
开源

一个库,用于在 Rust 中对 Docker 容器进行集成测试。

1.1K stars0 点赞3 次浏览
访问官网GitHub

工具介绍

一个库,用于在 Rust 中对 Docker 容器进行集成测试。

Testcontainers-rs

Testcontainers-rs 是 http://testcontainers.org 的官方 Rust 语言分支。

使用方法

testcontainers 是核心 crate

该框架提供了一个 API,用于在测试环境中处理容器。

  1. 依赖 testcontainers
  2. 为必要的 Docker 图像实现 testcontainers::core::Image
  3. 使用任何可用的运行程序 testcontainers::runners::* 运行它(使用 blocking 功能来实现同步 API)

示例:

  • 阻塞 API (在 blocking 功能下)
rust
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");
}
  • 异步 API
rust
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· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rustdockerintegration-testingrusttest-automation

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类DevOps
定价开源

> 相关工具

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理