我有一个要添加可选功能的软件包。我在Cargo.toml中添加了适当的部分:
[features]
foo = []
我为cfg!
宏的基本功能编写了一个实验性测试:
#[test]
fn testing_with_foo() {
assert!(cfg!(foo));
}
好像我可以在测试期间通过选项--features
或--all-features
中的任何一个激活功能:
(master *=) $ cargo help test
cargo-test
Execute all unit and integration tests and build examples of a local package
USAGE:
cargo test [OPTIONS] [TESTNAME] [-- <args>...]
OPTIONS:
-q, --quiet Display one character per test instead of one line
...
--features <FEATURES>... Space-separated list of features to activate
--all-features Activate all available features
不过cargo test --features foo testing_with_foo
和cargo test --all-features testing_with_foo
都不起作用。
执行此操作的正确方法是什么?
您的测试不正确。引用the Cargo book:
可以通过
#[cfg(feature = "foo")]
在代码中进行测试。