我的项目布局如下所示:
src/
int_rle.rs
lib.rs
tests/
test_int_rle.rs
该项目编译为cargo build
,但我无法运行测试cargo test
。我收到错误
error[E0432]: unresolved import `int_rle`. There is no `int_rle` in the crate root
--> tests/test_int_rle.rs:1:5
|
1 | use int_rle;
| ^^^^^^^
error[E0433]: failed to resolve. Use of undeclared type or module `int_rle`
--> tests/test_int_rle.rs:7:9
|
7 | int_rle::IntRle { values: vec![1, 2, 3] }
| ^^^^^^^^^^^^^^^ Use of undeclared type or module `int_rle`
error: aborting due to 2 previous errors
error: Could not compile `minimal_example_test_directories`.
My code:
// src/lib.rs
pub mod int_rle;
// src/int_rle.rs
#[derive(Debug, PartialEq)]
pub struct IntRle {
pub values: Vec<i32>,
}
// tests/test_int_rle.rs
use int_rle;
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
int_rle::IntRle { values: vec![1, 2, 3] }
}
}
// Cargo.toml
[package]
name = "minimal_example_test_directories"
version = "0.1.0"
authors = ["Johann Gambolputty de von Ausfern ... von Hautkopft of Ulm"]
[dependencies]
有关的:如何在 Rust 中编译多文件 crate? https://stackoverflow.com/questions/17340985/how-do-i-compile-a-multi-file-crate-in-rust(如果测试文件和源文件位于同一文件夹中,该怎么做。)
文件src/int_rle.rs
and src/lib.rs
形成您的图书馆,并一起称为crate.
您的测试和示例文件夹不被视为包的一部分。这很好,因为当有人使用您的库时,他们不需要您的测试,他们只需要您的库。
您可以通过添加行来解决您的问题extern crate minimal_example_test_directories;
到顶部tests/test_int_rle.rs
.
您可以在书中阅读有关 Rust 的板条箱和模块结构的更多信息,here https://doc.rust-lang.org/book/crates-and-modules.html.
这应该是测试文件的工作版本:
// tests/test_int_rle.rs
extern crate minimal_example_test_directories;
pub use minimal_example_test_directories::int_rle;
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
super::int_rle::IntRle { values: vec![1, 2, 3] };
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)