unit-testing - 如何运行不在 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 上搜索过的几篇文章,但没有人提到这个问题。我错过了什么?
解决方案
推荐阅读
- amazon-web-services - Aws CDK:Api 网关、sagemaker 集成、SageMakerRuntime 类型的 AWS 服务不支持错误
- node.js - Mongoose 检查参考 id
- python - 在 Python 中运行后,如何在一段时间内禁用 Raspberry Pi GPIO 事件?
- php - 无法从 $_post 获得价值
- azure - Azure Batch 作业计划 - 重复作业完成/失败后的通知
- python - PyBytes_FromString 不同的字节序
- javascript - 在 React Native App 中使用 BLE/USB 摄像头/条形码阅读器
- c# - 参数无效 datagridview 默认错误对话框
- javascript - 我正在使用cheerio 从 https://www.nba.com/players/langston/galloway/204038 获取统计信息,但我无法显示表格数据
- c# - 如何使用c#制作字符串类型数据的字节数组