如何定义仅测试依赖项?

我有一个 Rust 库,它实现了一个 lint 插件。我想包括 compiletest,但不要求它以外的测试。指定依赖项仅用于测试的正确方法是什么?

13333 次浏览

Yes. Use dev-dependencies. From the Cargo docs:

You can add a [dev-dependencies] section to your Cargo.toml whose format is equivalent to [dependencies]:

[dev-dependencies]
tempdir = "0.3"

Dev-dependencies are not used when compiling a package for building, but are used for compiling tests, examples, and benchmarks.

When possible, you should also use Cargo's resolver version 2 to better handle complicated dev-dependency cases.