我有以下 Rust 项目布局:
project_name
├── crate_1
│ ├── src
│ │ ...
│ │ └── main.rs
│ └── Cargo.toml
├── crate_2
│ ├── src
│ │ ...
│ │ └── lib.rs
│ └── Cargo.toml
├── tests
│ └── tests.rs <-- run tests in here
└── Cargo.toml
我想使用cargo在
tests
目录中运行测试,但是cargo似乎找不到它们。
有没有办法让货物来运行它们?
tokio 就是一个很好的例子。
现在您已经有了一个
tests
目录,让我们将其添加到工作区 members
中的 Cargo.toml
中。
[workspace]
members = [
"crate1",
"crate2",
"tests"
]
我们假设
test_crate1.rs
目录下有两个集成测试文件,test_crate2.rs
和tests
。
在
Cargo.toml
目录下创建一个 tests
,其中包含以下内容:
[package]
name = "tests"
version = "0.1.0"
edition = "2021"
publish = false
[dev-dependencies]
crate1 = { path = "../crate1" }
crate2 = { path = "../crate2" }
[[test]]
name = "test_crate1"
path = "test_crate1.rs"
[[test]]
name = "test_crate2"
path = "test_crate2.rs"
在工作区目录下运行
cargo test
来检查。