testing - 在集成测试中, cargo 测试无法引用目标 crate 内的任何公共(public)元素。单元测试也找不到测试用例

标签 testing rust rust-cargo rust-no-std

我试图在我的no_std模块上做一些测试,但是我不能让集成和单元测试都工作。我认为货物不能使其他人看到功能和模块。
该项目位于:https://github.com/ShisoftResearch/Nulloc/tree/e2e15646da79651ddb8c29e0526bad0d60690bec
运行cargo test,我得到:

➜ cargo test
   Compiling nulloc v0.1.0 (/Users/shisoft/Dropbox/Code/OSS Projects/Nulloc)
error[E0432]: unresolved import `nulloc::bump_heap`
 --> tests/bump_heap.rs:3:14
  |
3 |  use nulloc::bump_heap::BumpAllocator;
  |              ^^^^^^^^^ could not find `bump_heap` in `nulloc`

error[E0432]: unresolved import `nulloc::bump_heap`
 --> tests/bump_heap.rs:3:14
  |
3 |  use nulloc::bump_heap::BumpAllocator;
  |              ^^^^^^^^^ could not find `bump_heap` in `nulloc`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0432`.
error: aborting due to previous error

For more information about this error, try `rustc --explain E0432`.
error: could not compile `nulloc`.
warning: build failed, waiting for other jobs to finish...
error: could not compile `nulloc`.

To learn more, run the command again with --verbose.

但我已经把要在tests.rs中测试的板条箱放在外面了,bump_heap已经在lib.rs中标记为公共。
当我试图在bump_heap.rs内运行单元测试时,cargo找不到测试用例。
我不知道这是怎么回事

最佳答案

问题是Rust的测试框架隐式地使用了依赖于标准库的内置测试库。这意味着我们不能为我们的#[no_std]内核使用默认的测试框架。
Phillip Oppermann's blog
我的理解是你需要一个定制的框架。我的答案来自引用的绅士博客。
注释掉或删除tests目录中的测试。
使用以下代码创建src/main.rs

#![feature(custom_test_frameworks)]
#![test_runner(crate::test_runner)]
extern crate nulloc;
//pub mod bump_heap;

fn main() {}

#[cfg(test)]
fn test_runner(tests: &[&dyn Fn()]) {
    println!("Running {} tests", tests.len());
    for test in tests {
        test();
    }
}

#[test_case]
fn fails() {
    assert_eq!(true, false);
}

仅供参考,我注释掉了pub mod bump_heap,因为有编译器错误。
每晚用于运行测试。
如果你每晚都没有安装铁锈:
rustup install nightly
每晚运行测试:
rustup run nightly cargo test
单元和集成测试
如果你阅读了链接的博客,它会详细介绍如何进行集成测试和什么不做。我想暂时你可以在main.rs中使用mod或其他一些用于组织的东西。
mod integration {
    use super::*;

    #[test_case]
    fn works() {
        assert!(true);
    }   
}

mod bump_heap_unit {
    use super::bump_heap;

    #[test_case]
    fn works() {
        assert!(true);
    }   
}   

关于testing - 在集成测试中, cargo 测试无法引用目标 crate 内的任何公共(public)元素。单元测试也找不到测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58441073/

相关文章:

rust - 在两个 HashMap 之间交换特征

closures - 关闭需要唯一的访问权限

rust - 如何使用 Cargo 运行项目示例?

rust - 我如何指定在 Cargo 工作区的根目录中默认运行哪个 crate `cargo run`?

caching - 是否可以配置 Cargo 缓存它下载的 crate 的目录?

java - Maven surefire 和 JDK 11

rust - 当值将在启动时写入一次然后仅读取时,嵌入式 Rust 中的 Mutex 是否有轻量级替代方案?

go - 如何使用 ginkgo/gomega 测试无限循环/递归?

testing - 获取日志为提供了以下所需功能,但 Appium 无法识别

testing - Maven 不运行配置文件中指定的测试