两种解决方案的权衡截然不同:
单独的公用设施板条箱
将您的测试实用程序而不是测试放入新的箱子中crate-0-testutils
.
Make crate-0
开发依赖crate-0-testutils
; crate-0-testutils
取决于crate-0
; crate-1
开发依赖crate-0-testutils
并依赖于 crate-0。
这不会创建循环依赖关系,因为开发依赖关系不会传播。由于开发依赖性,您在 crate-0 中的测试仍然可以使用 crate-0-testutils 中的内容。
如果您的所有创建都具有此类实用程序,则这最多会使您的库箱数量增加一倍。
在所有板条箱中使用测试功能
在中创建一个特征Cargo.toml
ofcrate-0
这不依赖于任何东西:
[features]
testing = []
还有一在crate-1
这取决于上述情况:
testing = ["crate-0/testing"]
然后,在未启用该功能时,使每个板条箱上的第一次测试失败,以便更容易理解错误:
#[cfg(all(not(feature = "testing"),test))]
mod testusage {
#[test]
fn panic_without_testing_feature() {
panic!("Please use the feature testing when running tests.\n\nUse: cargo test --features testing\n\n");
}
}
仅当通过为其添加防护来启用该功能时才编译实用程序:
#[cfg(feature = "testing")]
fn testing_utility() {
}
无论它依赖多少个测试功能,这只会在每个包中添加一个功能,但缺点是需要特殊调用测试。
避免在任何 crate 范围的依赖声明中添加此功能。包括来自的测试功能crate-0
在开发依赖性中crate-1
where crate-1
也有正常的依赖crate-0
货物建造的结果crate-0
即使对于构建目标也具有该功能crate-1
即使在释放模式下。