使用 Rust Cargo 在工作空间根目录中运行测试
我有以下 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
tokio 是一个很好的例子。
现在您已经有了一个
tests
目录,让我们将其添加到工作区Cargo.toml
中的members
中。我们假设在
tests
目录下有两个集成测试文件,test_crate1.rs
和test_crate2.rs
。在
tests
目录下创建一个Cargo.toml
,其中包含以下内容:在工作区目录中运行
cargo test
进行检查。tokio is a very good example.
Now you already have a
tests
directory, let's add it to themembers
in workspaceCargo.toml
.We assume that there are two integration test files,
test_crate1.rs
andtest_crate2.rs
under thetests
directory.Create a
Cargo.toml
under thetests
directory with these contents:Run
cargo test
in workspace directory to check it.如果您的项目是单个可分发包,由多个工作区包组成,并且您希望从根
tests
目录运行集成测试。您不需要将测试目录指定为工作区成员。
看看
clap
是如何做到的< /a>如果你告诉 Cargo 你的根
Cargo.toml
实际上是一个包本身,它会像平常一样对待根tests
目录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 itIf you tell Cargo that your root
Cargo.toml
is infact a package itself, it will treat the roottests
directory as normal