Skip to content

Add rust/.kunitconfig and document unit testing #935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: rust
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Documentation/rust/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ in the kernel, please read the quick-start.rst guide.
general-information
coding-guidelines
arch-support
testing

.. only:: subproject and html

Expand Down
52 changes: 52 additions & 0 deletions Documentation/rust/testing.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
.. SPDX-License-Identifier: GPL-2.0

Testing
=======

Like in any other Rust project it is possible to write and run unit tests and
documentation tests in the kernel.

Running Unit Tests
------------------

Unit tests in the kernel are identical to user-space Rust tests:

.. code-block:: rust

#[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:

.. code-block:: bash

$ make LLVM=1 rusttest

Running Documentation Tests
---------------------------

Like in user-space, it is possible to write documentation tests:

.. code-block:: rust

/// ```
/// 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:

.. code-block:: bash

$ ./tools/testing/kunit/kunit.py run --kunitconfig=rust \
--make_options LLVM=1 --arch=x86_64

For general information about KUnit and `kunit.py``, please refer to
Documentation/dev-tools/kunit/start.rst.
6 changes: 6 additions & 0 deletions rust/.kunitconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CONFIG_KUNIT=y
CONFIG_RUST=y
CONFIG_RUST_KERNEL_KUNIT_TEST=y

# UML is not supported at the moment
CONFIG_UML=n