如何定义仅测试依赖项? [英] How to define test-only dependencies?
问题描述
我有一个实现 lint 插件的 Rust 库.我想包含 compiletest
,但在测试之外不需要它.指定依赖项仅用于测试的正确方法是什么?
I have a Rust library that implements a lint plugin. I want to include compiletest
, but not require it outside of testing. What is the correct way to specify that the dependency is for testing only?
推荐答案
是的.使用 dev-dependencies
.来自 货物文档:
Yes. Use dev-dependencies
. From the Cargo docs:
您可以将 [dev-dependencies]
部分添加到您的 Cargo.toml
中,其格式等同于 [dependencies]
:>
You can add a
[dev-dependencies]
section to yourCargo.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.
如果可能,您还应该使用 Cargo 的 resolver version 2 更好地处理复杂的开发依赖情况.
When possible, you should also use Cargo's resolver version 2 to better handle complicated dev-dependency cases.
这篇关于如何定义仅测试依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!