Unit testing 如何测试铁锈中的可选特性?

Unit testing 如何测试铁锈中的可选特性?,unit-testing,testing,rust,conditional-compilation,Unit Testing,Testing,Rust,Conditional Compilation,我有一个包,我想添加一个可选功能。我已在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

我有一个包,我想添加一个可选功能。我已在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
(master*=)$cargo帮助测试
货物试验
执行所有单元和集成测试,并构建本地包的示例
用法:
货物测试[选项][测试名称][-…]
选项:
-q、 --每个测试安静显示一个字符,而不是一行
...
--特征。。。以空格分隔的要激活的功能列表
--所有功能激活所有可用功能
但是,无论是
cargo-test,还是
cargo-test,所有的功能都不能用\u-foo进行测试


正确的方法是什么?

您的测试不正确。引述:

这可以通过
#[cfg(feature=“foo”)]
在代码中进行测试


解决方案就是@Jmb提出的:
assert!(cfg!(feature=“foo”)。货物登记簿上写了什么

这可以通过
#[cfg(feature=“foo”)]
在代码中进行测试


允许确认条件编译工作,但不提供可测试的布尔值。如果您想在运行时根据功能进行分支,则需要
cfg

您是否尝试过
货物测试----使用\u foo进行功能foo测试(注意
--
)。或者更接近OP想要的:
断言!(cfg!(feature=“foo”)货物登记簿上的具体位置是什么?我哪儿都找不到。”这可以通过#[cfg(feature=“foo”)]链接在代码中进行测试。链接有点过时,您可以检查-。