按照惯例,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)]标记,那么它们在非测试构建中仍然正确地缺失吗?
发布于 2019-05-05 19:47:56
我的问题是,
#[test]是否意味着#[cfg(test)]?也就是说,如果我用#[test]标记我的测试函数,而不是用#[cfg(test)]标记,那么它们在非测试构建中仍然正确地缺失吗?
是。如果您没有使用单独的模块进行测试,那么就不需要使用#[cfg(test)]。标记为#[test]的函数已经被排除在非测试构建之外。这一点很容易得到证实:
#[test]
fn test() {}
fn main() {
test(); // error[E0425]: cannot find function `test` in this scope
}https://stackoverflow.com/questions/55995061
复制相似问题