Testing

发布时间 2023-10-08 11:37:21作者: ImreW

Apr 27, 2019

This post explores unit and integration testing in no_std executables. We will use Rust’s support for custom test frameworks to execute test functions inside our kernel. To report the results out of QEMU, we will use different features of QEMU and the bootimage tool.

这篇文章探讨了 no_std 可执行文件中的单元和集成测试。 我们将使用 Rust 对自定义测试框架的支持来在内核中执行测试函数。 为了报告 QEMU 的结果,我们将使用 QEMU 和 bootimage 工具的不同功能。

This blog is openly developed on GitHub. If you have any problems or questions, please open an issue there. You can also leave comments at the bottom. The complete source code for this post can be found in the post-04 branch.

该博客是在 GitHub 上公开开发的。 如果您有任何问题或疑问,请在那里提出问题。 也可以在底部留言评论。 这篇文章的完整源代码可以在 post-04 分支中找到。

Requirements

This post replaces the (now deprecated) Unit Testing and Integration Tests posts. It assumes that you have followed the A Minimal Rust Kernel post after 2019-04-27. Mainly, it requires that you have a .cargo/config.toml file that sets a default target and defines a runner executable.

这篇文章取代了(现已弃用)单元测试和集成测试文章。 它假设您在 2019 年 4 月 27 日之后关注了 A Minimal Rust Kernel 帖子。 主要是,它要求您有一个 .cargo/config.toml 文件来设置默认目标并定义运行程序可执行文件。

Testing in Rust

Rust has a built-in test framework that is capable of running unit tests without the need to set anything up. Just create a function that checks some results through assertions and add the #[test] attribute to the function header. Then cargo test will automatically find and execute all test functions of your crate.

Rust 有一个内置的测试框架,无需进行任何设置即可运行单元测试。 只需创建一个通过断言检查某些结果的函数,并将 #[test] 属性添加到函数头即可。 然后,cargo test 会自动查找并执行你的 crate 的所有测试函数。

Unfortunately, it’s a bit more complicated for no_std applications such as our kernel. The problem is that Rust’s test framework implicitly uses the built-in test library, which depends on the standard library. This means that we can’t use the default test framework for our #[no_std] kernel.

不幸的是,对于 no_std 应用程序(例如我们的内核)来说,情况要复杂一些。 问题在于 Rust 的测试框架隐式使用内置测试库,该库依赖于标准库。 这意味着我们不能使用 #[no_std] 内核的默认测试框架。