【问题标题】:Rust tests fail to even runRust 测试甚至无法运行
【发布时间】:2021-03-17 09:45:54
【问题描述】:

我正在编写一个项目来学习如何使用 Rust,我正在调用我的项目future-finance-labs。在编写了一些基本功能并验证可以构建应用程序之后,我想包含一些测试,位于aggregates/mod.rs。 [根据文档,测试与实际代码位于同一文件中。] 尽管尽我所能遵循documentation,但我无法运行测试。我尝试使用 PowerShell 和 Bash 构建项目。 [它也无法在 Fedora Linux 上运行]

这是我在 Bash 上的输出:

~/future-finance-labs$ cargo test -- src/formatters/mod.rs
    Finished test [unoptimized + debuginfo] target(s) in 5.98s
     Running target/debug/deps/future_finance_labs-16ed066e1ea3b9a1

running 0 tests

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

使用 PowerShell 我得到相同的输出,但出现如下错误:

error: failed to remove C:\Users\jhale\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState\rootfs\home\jhale\future-finance-labs\target\debug\build\mime_guess-890328c8763afc22\build_script_build-890328c8763afc22.build_script_build.c22di3i8-cgu.0.rcgu.o: The system cannot find the path specified. (os error 3)

在对编写一些第一次尝试通过的测试的前景感到兴奋之后,我很快意识到所有的绿色都是指示性的;相反,甚至无法运行测试。我只想运行单元测试。单独运行 cargo test 而没有单独的文件也会失败。为什么我不能使用我当前的设置在这个项目中运行任何测试?

【问题讨论】:

  • 如果我没看错你的问题,那就是cargo test --test <test_name>
  • @Mihir,这给了我一个错误,这比没有解释地不运行测试更有用。 $ cargo test --test src/aggregates/mod.rs error: no test target named src/aggregates/mod.rs
  • 我编辑了这个问题,以明确我的测试模块与它测试的代码在同一个文件中,文档似乎建议这样做。

标签: unit-testing rust rust-cargo


【解决方案1】:

它找不到您的测试,因为 rust 编译器不知道它。您需要将mod aggregates 添加到main。

mod aggregates;

fn main() {
    println!("Hello, world!");
}

在你这样做之后,你会发现你的 aggregates/mod.rs 由于很多原因没有编译。


正如 Mihir 所说,您需要使用 test 的名称,而不是 file 的名称来运行特定的测试:

cargo test min_works
cargo test aggregates

另见:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-07
    • 1970-01-01
    相关资源
    最近更新 更多