使用 Rust Cargo 在工作空间根目录中运行测试

问题描述 投票:0回答:2

我有以下 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似乎找不到它们。 有没有办法让货物来运行它们?

rust integration-testing rust-cargo
2个回答
17
投票

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
来检查。


0
投票

如果您的项目是单个可分发包,由多个工作区板条箱组成,并且您希望从根

tests
目录运行集成测试。

您不需要将测试目录指定为工作区成员。

看看

clap
是如何做到的

如果你告诉 Cargo 你的根

Cargo.toml
实际上是一个包本身,它会像平常一样对待根
tests
目录

[package]
name = "your package"
version = "0.0.0"
© www.soinside.com 2019 - 2024. All rights reserved.