10 个版本 (5 个稳定版)
2.0.0 | 2024 年 3 月 16 日 |
---|---|
1.0.3 | 2022 年 12 月 8 日 |
1.0.2 | 2022 年 3 月 29 日 |
1.0.1 | 2021 年 6 月 25 日 |
0.0.2 | 2021 年 6 月 19 日 |
#152 in 测试
每月下载量 345
在 2 个包中使用(通过 testcall)
17KB
248 代码行
测试由 bin 包构建的可执行文件。
描述
cargo test
默认不支持在构建的可执行文件上运行测试。这个包绕过这个缺陷。
工作原理
需要克服 cargo 的一些限制。
- 运行 cargo 测试默认不依赖于要构建的可执行文件,它们在测试时不会被编译。
- 没有标准设施来在测试中定位和执行它们。
BinTest 通过在测试时运行 cargo build
来解决这些问题,并解析其输出以识别和定位构建的可执行文件。根据请求,它创建一个用于二进制的 std::process::Command,可以用于任何进一步的测试。
lib.rs
:
示例
在简单情况下,您可以直接调用 BinTest::new()
来构建当前包中的所有可执行文件,并获取一个用于操作的 BinTest
单例引用。
#[test]
fn test() {
// BinTest::new() will run 'cargo build' and registers all build executables
let executables: &'static BinTest = BinTest::new();
// List the executables build
for (k,v) in executables.list_executables() {
println!("{} @ {}", k, v);
}
// BinTest::command() looks up executable by its name and creates a process::Command from it
let command = executables.command("name");
// this command can then be used for testing
command.arg("help").spawn();
}
在更复杂的情况下,您可以使用 BinTest::with()
来使用 BinTestBuilder
配置构建过程,然后调用它上的 .build()
来获取 BinTest
单例引用。
另请参阅
testcall 包 使用此功能在 bintest 创建的命令之上构建测试和断言。 testpath 包 允许您在专门创建的临时目录中运行测试,为测试提供一个文件系统环境。
依赖
~1–1.7MB
~37K SLoC