如何测试Rust中的可选功能?

问题描述 投票:2回答:1

我有一个要添加可选功能的软件包。我在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_foocargo test --all-features testing_with_foo都不起作用。

执行此操作的正确方法是什么?

unit-testing testing rust conditional-compilation
1个回答
0
投票

您的测试不正确。引用the Cargo book

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

© www.soinside.com 2019 - 2024. All rights reserved.