`#[test]` 是否意味着 `#[cfg(test)]`?

Does `#[test]` imply `#[cfg(test)]`?

通常,Rust 中的单元测试被赋予一个单独的模块,该模块有条件地编译 #[cfg(test)]:

#[cfg(test)]
mod tests {
    #[test]
    fn test1() { ... }

    #[test]
    fn test2() { ... }
}

但是,我一直在使用一种测试更内联的样式:

pub fn func1() {...}

#[cfg(test)]
#[test]
fn test_func1() {...}

pub fn func2() {...}

#[cfg(test)]
#[test]
fn test_func2() {...}

我的问题是,#[test] 是否意味着 #[cfg(test)]?也就是说,如果我用 #[test] 而不是 #[cfg(test)] 标记我的测试函数,它们在非测试构建中是否仍然正确地缺失?

My question is, does #[test] imply #[cfg(test)]? That is, if I tag my test functions with #[test] but not #[cfg(test)], will they still be correctly absent in non-test builds?

是的。如果您不使用单独的模块进行测试,则不需要使用 #[cfg(test)]。标有 #[test] 的函数已从非测试版本中排除。这可以很容易地验证:

#[test]
fn test() {}

fn main() {
    test(); // error[E0425]: cannot find function `test` in this scope
}