unit-testing - Cargo 未在顶级文件中运行测试

标签 unit-testing module rust rust-cargo

我对 Cargo 如何找到要运行的测试感到很困惑。

我创建了一个 Cargo 项目,它为我添加了一个 main.rs。如果我在其中添加 #[test] 函数,它们就会被发现并通过 cargo test 运行。如果我添加一个 foo.rs 文件作为 main.rs 的同级文件并在其中添加测试,它们将找不到并运行。

我错过了什么微妙之处?我是否必须以某种方式向 Cargo 传授新文件?

最佳答案

Cargo 不仅会编译恰好在您的源目录中的任何文件。为了让 Cargo 找到一个文件,它必须作为模块在 main.rs/lib.rs 中或从某个子模块中引用。

例如,在您的 main.rs 中:

mod foo;

就是这样。

关于unit-testing - Cargo 未在顶级文件中运行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44873527/

相关文章:

php - 如何对继承对象进行单元测试?

perl - directory/.pm 曾经是一个约定吗?它为什么存在?

python - `import directory.module` 和 `import module` 之间的区别

performance - 第一次调用 Julia 很慢

unit-testing - 创建一个 `std::env::Args` 迭代器用于测试

eclipse - 自动化 Eclipse 插件单元测试

java - 如何获取在 testng 拆卸方法中运行的测试方法的名称?

rust - 闭包参数上未使用的类型参数

enums - 如何在枚举中匹配 self ?

python - IntEnum 返回 AttributeError : can't set attribute