Skip to content

Commit d363ca5

Browse files
committed
docs: rust: document automated 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 Reviewed-by: David Gow <[email protected]> Signed-off-by: José Expósito <[email protected]>
1 parent bed39f1 commit d363ca5

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-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+
testing
1617

1718
.. only:: subproject and html
1819

Documentation/rust/testing.rst

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
.. SPDX-License-Identifier: GPL-2.0
2+
3+
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 it is possible to run them either on boot or
44+
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
50+
51+
For general information about KUnit and `kunit.py``, please refer to
52+
Documentation/dev-tools/kunit/start.rst.

0 commit comments

Comments
 (0)