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

发布于 2025-01-14 05:54:27 字数 396 浏览 0 评论 0原文

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

I've got the following rust project layout:

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

I want to run the tests in the tests directory using cargo, however cargo can't seem to find them.
Is there a way to get cargo to run them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剩余の解释 2025-01-21 05:54:27

tokio 是一个很好的例子。

现在您已经有了一个 tests 目录,让我们将其添加到工作区 Cargo.toml 中的 members 中。

[workspace]

members = [
    "crate1",
    "crate2",

    "tests"
]

我们假设在tests目录下有两个集成测试文件,test_crate1.rstest_crate2.rs

tests 目录下创建一个 Cargo.toml,其中包含以下内容:

[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 进行检查。

tokio is a very good example.

Now you already have a tests directory, let's add it to the members in workspace Cargo.toml.

[workspace]

members = [
    "crate1",
    "crate2",

    "tests"
]

We assume that there are two integration test files, test_crate1.rs and test_crate2.rs under the tests directory.

Create a Cargo.toml under the tests directory with these contents:

[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"

Run cargo test in workspace directory to check it.

在风中等你 2025-01-21 05:54:27

如果您的项目是单个可分发包,由多个工作区包组成,并且您希望从根 tests 目录运行集成测试。

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

看看 clap 是如何做到的< /a>

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

[package]
name = "your package"
version = "0.0.0"

If your project is a single distributable package, comprised of several workspace crates, and you want to run integration tests from a root tests directory.

You don't need to specify the tests directory as a workspace member.

Have a look at how clap does it

If you tell Cargo that your root Cargo.toml is infact a package itself, it will treat the root tests directory as normal

[package]
name = "your package"
version = "0.0.0"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文