Like in any other Rust project it is possible to write and run unit tests and documentation tests in the kernel.
Unit tests in the kernel are identical to user-space Rust tests:
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
let result = 2 + 2;
assert_eq!(result, 4);
}
}And can be run using the rusttest Make target:
$ make LLVM=1 rusttestLike in user-space, it is possible to write documentation tests:
/// ```
/// let result = 2 + 2;
/// assert_eq!(result, 4);
/// ```
Documentation tests use KUnit and it is possible to run them either on boot or
using the kunit.py tool:
$ ./tools/testing/kunit/kunit.py run --kunitconfig=rust \
--make_options LLVM=1 --arch=x86_64For general information about KUnit and kunit.py`, please refer to Documentation/dev-tools/kunit/start.rst.