首页 > 解决方案 > 如何运行不在 lib.rs 或 main.rs 中的测试?

问题描述

我正在学习用 rust 编写单元测试。src/lib.rs但是,当它们不在or中时,我无法运行我的测试src/main.rs

我创建了一个具有以下结构的项目:

├── Cargo.lock
├── Cargo.toml
├── src
    ├── lib.rs
    └── main.rs
    └── functions.rs

并添加了以下代码:

main.rs

#[cfg(test)]
mod test {
    #[test]
    fn test_main() {}
}

库文件

#[cfg(test)]
mod test {
    #[test]
    fn test_lib() {}
}

函数.rs

#[cfg(test)]
mod test {
    #[test]
    fn test_functions() { }
}

当我运行时cargo test,这就是我得到的:

Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running target\debug\deps\rust_unittests-b0ed92af162e0288.exe

running 1 test
test test::test_lib ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target\debug\deps\rust_unittests-c4edac234e8e3d9a.exe

running 1 test
test test::test_main ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests rust_unittests

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

main.rs 和 lib.rs 中的测试函数按预期运行,但 functions.rs 中的测试函数没有。

我阅读了 Rust 的书和我在 Google 上搜索过的几篇文章,但没有人提到这个问题。我错过了什么?

标签: unit-testingrust

解决方案


推荐阅读