为集成测试和基准测试共享实用程序函数的惯用方法是什么?
What is an idiomatic way to have shared utility functions for integration tests and benchmarks?
我有一个包含集成测试(在 /tests
目录中)和基准测试(在 /benches
目录中)的 Rust 项目。我在测试和工作台中需要一些实用函数,但它们与我的板条箱本身无关,所以我不能只将它们放在 /utils
目录中。
处理这种情况的惯用方法是什么?
您可以将这些实用程序功能添加到主箱内的 pub
模块中,并使用 #[doc(hidden)]
或 #![doc(hidden)]
属性将它们从文档生成器中隐藏起来。额外的评论将指导 reader 为什么他们在那里。
创建共享箱(首选)
如评论中所述,创建一个新的箱子。您 不必将 crate 发布到 crates.io。就 inside your project and mark it as a .
这最好与 version 2 of the Cargo resolver 一起使用。
.
├── Cargo.toml
├── src
│ └── lib.rs
├── tests
│ └── integration.rs
└── utilities
├── Cargo.toml
└── src
└── lib.rs
Cargo.toml
# ...
[dev-dependencies]
utilities = { path = "utilities" }
utilities/src/lib.rs
pub fn shared_code() {
println!("I am shared code");
}
tests/integration.rs
extern crate utilities;
#[test]
fn a_test() {
utilities::shared_code();
}
一个仅供测试的模块
您可以在您的 crate 中放置一个模块,该模块仅在通过特定功能时才编译。这与用于单元测试的概念相同。这样做的好处是它可以访问您的库代码的内部结构。它的缺点是每次 运行 代码时都需要传递标志。
这最好与 version 2 of the Cargo resolver 一起使用。
Cargo.toml
# ...
[features]
test-utilities = []
src/lib.rs
#[cfg(feature = "test-utilities")]
pub mod test_utilities {
pub fn shared_code() {
println!("I'm inside the library")
}
}
tests/integration.rs
extern crate the_library;
#[test]
fn a_test() {
the_library::test_utilities::shared_code();
}
执行
cargo test --features=test-utilities
这最好与 version 2 of the Cargo resolver 一起使用。
使用来自任意文件路径的模块
这对我来说太丑了,真的不正常。
utilities.rs
pub fn shared_code() {
println!("This is just sitting out there");
}
tests/integration.rs
#[path = "../utilities.rs"]
mod utilities;
#[test]
fn a_test() {
utilities::shared_code();
}
另请参阅:
虽然这对基准测试没有帮助,但我来这里是为了寻找一种通过多个集成测试来做到这一点的方法,后来发现您可以为集成测试执行以下操作:
Modules with common code follow the ordinary modules rules, so it's ok to create common module as tests/common/mod.rs.
来源:https://doc.rust-lang.org/rust-by-example/testing/integration_testing.html
我有一个包含集成测试(在 /tests
目录中)和基准测试(在 /benches
目录中)的 Rust 项目。我在测试和工作台中需要一些实用函数,但它们与我的板条箱本身无关,所以我不能只将它们放在 /utils
目录中。
处理这种情况的惯用方法是什么?
您可以将这些实用程序功能添加到主箱内的 pub
模块中,并使用 #[doc(hidden)]
或 #![doc(hidden)]
属性将它们从文档生成器中隐藏起来。额外的评论将指导 reader 为什么他们在那里。
创建共享箱(首选)
如评论中所述,创建一个新的箱子。您 不必将 crate 发布到 crates.io。就
这最好与 version 2 of the Cargo resolver 一起使用。
.
├── Cargo.toml
├── src
│ └── lib.rs
├── tests
│ └── integration.rs
└── utilities
├── Cargo.toml
└── src
└── lib.rs
Cargo.toml
# ...
[dev-dependencies]
utilities = { path = "utilities" }
utilities/src/lib.rs
pub fn shared_code() {
println!("I am shared code");
}
tests/integration.rs
extern crate utilities;
#[test]
fn a_test() {
utilities::shared_code();
}
一个仅供测试的模块
您可以在您的 crate 中放置一个模块,该模块仅在通过特定功能时才编译。这与用于单元测试的概念相同。这样做的好处是它可以访问您的库代码的内部结构。它的缺点是每次 运行 代码时都需要传递标志。
这最好与 version 2 of the Cargo resolver 一起使用。
Cargo.toml
# ...
[features]
test-utilities = []
src/lib.rs
#[cfg(feature = "test-utilities")]
pub mod test_utilities {
pub fn shared_code() {
println!("I'm inside the library")
}
}
tests/integration.rs
extern crate the_library;
#[test]
fn a_test() {
the_library::test_utilities::shared_code();
}
执行
cargo test --features=test-utilities
这最好与 version 2 of the Cargo resolver 一起使用。
使用来自任意文件路径的模块
这对我来说太丑了,真的不正常。
utilities.rs
pub fn shared_code() {
println!("This is just sitting out there");
}
tests/integration.rs
#[path = "../utilities.rs"]
mod utilities;
#[test]
fn a_test() {
utilities::shared_code();
}
另请参阅:
虽然这对基准测试没有帮助,但我来这里是为了寻找一种通过多个集成测试来做到这一点的方法,后来发现您可以为集成测试执行以下操作:
Modules with common code follow the ordinary modules rules, so it's ok to create common module as tests/common/mod.rs.
来源:https://doc.rust-lang.org/rust-by-example/testing/integration_testing.html