[英]How does one test optional features in Rust?
我有一個 package 我想添加一個可選功能。 我在 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
testing_with_foo 都不起作用。
這樣做的正確方法是什么?
你的測試不正確。 引用貨物書:
這可以通過
#[cfg(feature = "foo")]
在代碼中進行測試。
解決方案是@Jmb 提出的: assert;(cfg!(feature = "foo"));
. 貨物手冊中的內容
這可以通過
#[cfg(feature = "foo")]
在代碼中進行測試。
允許人們確認條件編譯有效,但不提供可以測試的 boolean 值。 如果你想在運行時基於特性進行分支,你需要cfg!
.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.