我想了解#[cfg(test)]和#[cfg(feature = "test")]之间的区别,最好通过示例演示。
发布于 2022-07-08 10:01:17
#[cfg(test)]只在启用test配置选项时标记要编译的内容,而#[cfg(feature = "test")]只在启用feature = "test"配置选项时标记要编译的内容。
“启用测试cfg选项”的标志(除其他外)。 --通常用于只在您想要运行测试时有条件地编译测试模块。
试着跑:
#[cfg(feature="test")]
mod fake_test {
#[test]
fn fake_test() {
panic!("This function is NOT tested");
}
}
#[cfg(test)]
mod test {
#[test]
fn test() {
panic!("This function is tested");
}
}产出将是:
running 1 test
test test::test ... FAILED
failures:
---- test::test stdout ----
thread 'test::test' panicked at 'This function is tested', src/lib.rs:13:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
test::test
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s这表明假测试模块没有启用。
https://stackoverflow.com/questions/72908829
复制相似问题