#axum #test #test-framework

axum-test-helpers

🧪 axum 测试客户端

2 个版本

0.7.5 2024年2月10日
0.7.4 2024年2月10日

#268 in 测试


2 crates 使用

GPL-3.0-only

17KB
253

axum-test-helpers

axum-test-helpers 提供了 axum 原始的 TestClient,该客户端在 axum crate 中是私有的

有关此 crate 的更多信息,请参阅 crate 文档

高级特性

  • 提供易于使用的接口
  • 为每个调用启动不同端口的服务器
  • 处理 JSON、文本和文件响应/请求

使用示例

将此 crate 添加为 dev-dependency

[dev-dependencies]
axum-test-helpers = "0.7.4"

在您的 Router 上使用 TestClient

use axum::Router;
use axum::http::StatusCode;
use axum_test_helper::TestClient;

// you can replace this Router with your own app
let app = Router::new().route("/", get(|| async {}));

// initiate the TestClient with the previous declared Router
let client = TestClient::new(app);
let res = client.get("/").await;
assert_eq!(res.status(), StatusCode::OK);

您可以在 示例目录 中找到类似示例。

有关更多示例,请参阅 crate 文档

禁用跟踪

默认情况下,axum-test-helpers 会打印跟踪信息,如 Listening on 127.0.0.1:36457。您可以使用以下命令禁用跟踪:axum-test-helpers = { version = "0.*", default-features = false, features = ["withouttrace"] }

贡献

在提交 pull request 或从主仓库拉取后,确保所有测试通过

# Run axum-test-helpers tests
cargo test

# Test the hello-world example project
(cd examples/hello-world && cargo test)

许可证

本项目采用 GPL-3.0-only 许可。

依赖项

~8–24MB
~329K SLoC