Skip to content

Commit

Permalink
docs: rust: document unit testing
Browse files Browse the repository at this point in the history
Explain how to run unit tests and documentation tests.

Note that the documentation uses "--arch=x86_64" to run KUnit tests
because UML is not working at the moment [1].

[1] #881
Signed-off-by: José Expósito <[email protected]>
  • Loading branch information
JoseExposito committed Dec 5, 2022
1 parent da1e6a1 commit f5ea626
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
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
unit-testing

.. only:: subproject and html

Expand Down
49 changes: 49 additions & 0 deletions Documentation/rust/unit-testing.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
.. SPDX-License-Identifier: GPL-2.0
Unit 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, thanks to the configuration file present in
``rust/.kunitconfig``, it is possible to run them using the ``kunit.py`` tool:

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

0 comments on commit f5ea626

Please sign in to comment.