您如何将所有箱子包含在测试目录内的工作区中?

How do you include all crates in a workspace inside the test directory?

我有一个二进制 Rust 项目,它使用工作空间来管理子包。

目录结构

/myapp
  Cargo.toml
  /src
  /tests
    test.rs
  /crates
    /printer
      Cargo.toml
      /src

myapp/Cargo.toml

[package]
name = "myapp"

[workspace]
members = ["crates/printer"]

test.rs 内部,我可以编译 extern crate myapp; 以引入在 src/lib.rs 中公开的应用程序部分。这按预期工作。

然而,当试图编译 extern crate printer; 时,它会出错,说找不到它。我已确认打印机包已正确放置在顶级 Cargo.lock 文件中。

如何将我的子 crate 包含到顶层的 /tests 目录中?

工作空间甚至测试概念都没有什么特别之处。如果你想在 Rust 代码中使用 crate,你必须将它添加为依赖项:

[dependencies]
printer = { path = "crates/printer" }

另请参阅: