Skip to content

Commit 9e81b42

Browse files
committed
docs: rust: document unit testing
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] Rust-for-Linux/linux#881 Signed-off-by: José Expósito <[email protected]>
1 parent 8ffa3c5 commit 9e81b42

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

Documentation/rust/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ in the kernel, please read the quick-start.rst guide.
1313
general-information
1414
coding-guidelines
1515
arch-support
16+
unit-testing
1617

1718
.. only:: subproject and html
1819

Documentation/rust/unit-testing.rst

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
.. SPDX-License-Identifier: GPL-2.0
2+
3+
Unit Testing
4+
============
5+
6+
Like in any other Rust project it is possible to write and run unit tests and
7+
documentation tests in the kernel.
8+
9+
Running Unit Tests
10+
------------------
11+
12+
Unit tests in the kernel are identical to user-space Rust tests:
13+
14+
.. code-block:: rust
15+
16+
#[cfg(test)]
17+
mod tests {
18+
#[test]
19+
fn it_works() {
20+
let result = 2 + 2;
21+
assert_eq!(result, 4);
22+
}
23+
}
24+
25+
And can be run using the ``rusttest`` Make target:
26+
27+
.. code-block:: bash
28+
29+
$ make LLVM=1 rusttest
30+
31+
Running Documentation Tests
32+
---------------------------
33+
34+
Like in user-space, it is possible to write documentation tests:
35+
36+
.. code-block:: rust
37+
38+
/// ```
39+
/// let result = 2 + 2;
40+
/// assert_eq!(result, 4);
41+
/// ```
42+
43+
Documentation tests use KUnit and, thanks to the configuration file present in
44+
``rust/.kunitconfig``, it is possible to run them using the ``kunit.py`` tool:
45+
46+
.. code-block:: bash
47+
48+
$ ./tools/testing/kunit/kunit.py run --kunitconfig=rust \
49+
--make_options LLVM=1 --arch=x86_64

0 commit comments

Comments
 (0)