我试图在我的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/