如何运行主二进制文件,然后在 Rust 中运行基于它的测试?
我已经编写了一个网络服务器,需要一些复杂的设置和拆卸,并且正在尝试编写单元测试。 Axum 确实提供了使用 Tower OneShot 功能的示例,但这些示例并不容易实现完整的设置流程。我将如何运行完整的服务器,然后运行附加代码以通过 cargo test
对其进行测试(使用 reqwest)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CLI 书展示了一个很好的方法:
然后你可以编写一个测试像这样:
主要本质是这个
Command::cargo_bin("your-binary-name")?
即 记录在
assert_cmd
crate 中,它允许您从测试中调用二进制文件,而无需先编译它,cargo 会处理它。
The CLI book has a nice approach shown:
Then you can write a test like this:
The main essence is this
Command::cargo_bin("your-binary-name")?
That is documented in the
assert_cmd
crateThat allows you to call your binary from a test, without the need to compile it first, cargo takes care of it.
请参阅此示例中的
the_real_deal
https://github.com/tokio-rs/axum/blob/main/examples/testing/src/main.rsSee
the_real_deal
in this example https://github.com/tokio-rs/axum/blob/main/examples/testing/src/main.rs