Skip to content

Commit 0d5792c

Browse files
sulixshuahkh
authored andcommitted
Documentation: kunit: Make the KUnit documentation less UML-specific
Remove some of the outmoded "Why KUnit" rationale, and move some UML-specific information to the kunit_tool page. Also update the Getting Started guide to mention running tests without the kunit_tool wrapper. Signed-off-by: David Gow <davidgow@google.com> Reviewed-by: Frank Rowand <frank.rowand@sony.com> Acked-by: Shuah Khan <skhan@linuxfoundation.org> Reviewed-by: Brendan Higgins <brendanhiggins@google.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent cb88577 commit 0d5792c

3 files changed

Lines changed: 99 additions & 28 deletions

File tree

‎Documentation/dev-tools/kunit/index.rst‎

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,23 @@ What is KUnit?
1717
==============
1818

1919
KUnit is a lightweight unit testing and mocking framework for the Linux kernel.
20-
These tests are able to be run locally on a developer's workstation without a VM
21-
or special hardware.
2220

2321
KUnit is heavily inspired by JUnit, Python's unittest.mock, and
2422
Googletest/Googlemock for C++. KUnit provides facilities for defining unit test
2523
cases, grouping related test cases into test suites, providing common
2624
infrastructure for running tests, and much more.
2725

26+
KUnit consists of a kernel component, which provides a set of macros for easily
27+
writing unit tests. Tests written against KUnit will run on kernel boot if
28+
built-in, or when loaded if built as a module. These tests write out results to
29+
the kernel log in `TAP <https://testanything.org/>`_ format.
30+
31+
To make running these tests (and reading the results) easier, KUnit offers
32+
:doc:`kunit_tool <kunit-tool>`, which builds a `User Mode Linux
33+
<http://user-mode-linux.sourceforge.net>`_ kernel, runs it, and parses the test
34+
results. This provides a quick way of running KUnit tests during development,
35+
without requiring a virtual machine or separate hardware.
36+
2837
Get started now: :doc:`start`
2938

3039
Why KUnit?
@@ -36,21 +45,20 @@ allow all possible code paths to be tested in the code under test; this is only
3645
possible if the code under test is very small and does not have any external
3746
dependencies outside of the test's control like hardware.
3847

39-
Outside of KUnit, there are no testing frameworks currently
40-
available for the kernel that do not require installing the kernel on a test
41-
machine or in a VM and all require tests to be written in userspace running on
42-
the kernel; this is true for Autotest, and kselftest, disqualifying
43-
any of them from being considered unit testing frameworks.
48+
KUnit provides a common framework for unit tests within the kernel.
49+
50+
KUnit tests can be run on most architectures, and most tests are architecture
51+
independent. All built-in KUnit tests run on kernel startup. Alternatively,
52+
KUnit and KUnit tests can be built as modules and tests will run when the test
53+
module is loaded.
4454

45-
KUnit addresses the problem of being able to run tests without needing a virtual
46-
machine or actual hardware with User Mode Linux. User Mode Linux is a Linux
47-
architecture, like ARM or x86; however, unlike other architectures it compiles
48-
to a standalone program that can be run like any other program directly inside
49-
of a host operating system; to be clear, it does not require any virtualization
50-
support; it is just a regular program.
55+
.. note::
5156

52-
Alternatively, kunit and kunit tests can be built as modules and tests will
53-
run when the test module is loaded.
57+
KUnit can also run tests without needing a virtual machine or actual
58+
hardware under User Mode Linux. User Mode Linux is a Linux architecture,
59+
like ARM or x86, which compiles the kernel as a Linux executable. KUnit
60+
can be used with UML either by building with ``ARCH=um`` (like any other
61+
architecture), or by using :doc:`kunit_tool <kunit-tool>`.
5462

5563
KUnit is fast. Excluding build time, from invocation to completion KUnit can run
5664
several dozen tests in only 10 to 20 seconds; this might not sound like a big
@@ -81,3 +89,5 @@ How do I use it?
8189
* :doc:`start` - for new users of KUnit
8290
* :doc:`usage` - for a more detailed explanation of KUnit features
8391
* :doc:`api/index` - for the list of KUnit APIs used for testing
92+
* :doc:`kunit-tool` - for more information on the kunit_tool helper script
93+
* :doc:`faq` - for answers to some common questions about KUnit

‎Documentation/dev-tools/kunit/kunit-tool.rst‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ the Linux kernel as UML (`User Mode Linux
1212
<http://user-mode-linux.sourceforge.net/>`_), running KUnit tests, parsing
1313
the test results and displaying them in a user friendly manner.
1414

15+
kunit_tool addresses the problem of being able to run tests without needing a
16+
virtual machine or actual hardware with User Mode Linux. User Mode Linux is a
17+
Linux architecture, like ARM or x86; however, unlike other architectures it
18+
compiles the kernel as a standalone Linux executable that can be run like any
19+
other program directly inside of a host operating system. To be clear, it does
20+
not require any virtualization support: it is just a regular program.
21+
1522
What is a kunitconfig?
1623
======================
1724

‎Documentation/dev-tools/kunit/start.rst‎

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,53 @@ Installing dependencies
99
KUnit has the same dependencies as the Linux kernel. As long as you can build
1010
the kernel, you can run KUnit.
1111

12-
KUnit Wrapper
13-
=============
14-
Included with KUnit is a simple Python wrapper that helps format the output to
15-
easily use and read KUnit output. It handles building and running the kernel, as
16-
well as formatting the output.
12+
Running tests with the KUnit Wrapper
13+
====================================
14+
Included with KUnit is a simple Python wrapper which runs tests under User Mode
15+
Linux, and formats the test results.
1716

1817
The wrapper can be run with:
1918

2019
.. code-block:: bash
2120
2221
./tools/testing/kunit/kunit.py run --defconfig
2322
24-
For more information on this wrapper (also called kunit_tool) checkout the
23+
For more information on this wrapper (also called kunit_tool) check out the
2524
:doc:`kunit-tool` page.
2625

2726
Creating a .kunitconfig
28-
=======================
29-
The Python script is a thin wrapper around Kbuild. As such, it needs to be
30-
configured with a ``.kunitconfig`` file. This file essentially contains the
31-
regular Kernel config, with the specific test targets as well.
32-
27+
-----------------------
28+
If you want to run a specific set of tests (rather than those listed in the
29+
KUnit defconfig), you can provide Kconfig options in the ``.kunitconfig`` file.
30+
This file essentially contains the regular Kernel config, with the specific
31+
test targets as well. The ``.kunitconfig`` should also contain any other config
32+
options required by the tests.
33+
34+
A good starting point for a ``.kunitconfig`` is the KUnit defconfig:
3335
.. code-block:: bash
3436
3537
cd $PATH_TO_LINUX_REPO
3638
cp arch/um/configs/kunit_defconfig .kunitconfig
3739
38-
Verifying KUnit Works
39-
---------------------
40+
You can then add any other Kconfig options you wish, e.g.:
41+
.. code-block:: none
42+
43+
CONFIG_LIST_KUNIT_TEST=y
44+
45+
:doc:`kunit_tool <kunit-tool>` will ensure that all config options set in
46+
``.kunitconfig`` are set in the kernel ``.config`` before running the tests.
47+
It'll warn you if you haven't included the dependencies of the options you're
48+
using.
49+
50+
.. note::
51+
Note that removing something from the ``.kunitconfig`` will not trigger a
52+
rebuild of the ``.config`` file: the configuration is only updated if the
53+
``.kunitconfig`` is not a subset of ``.config``. This means that you can use
54+
other tools (such as make menuconfig) to adjust other config options.
55+
56+
57+
Running the tests
58+
-----------------
4059

4160
To make sure that everything is set up correctly, simply invoke the Python
4261
wrapper from your kernel repo:
@@ -62,6 +81,41 @@ followed by a list of tests that are run. All of them should be passing.
6281
Because it is building a lot of sources for the first time, the
6382
``Building KUnit kernel`` step may take a while.
6483

84+
Running tests without the KUnit Wrapper
85+
=======================================
86+
87+
If you'd rather not use the KUnit Wrapper (if, for example, you need to
88+
integrate with other systems, or use an architecture other than UML), KUnit can
89+
be included in any kernel, and the results read out and parsed manually.
90+
91+
.. note::
92+
KUnit is not designed for use in a production system, and it's possible that
93+
tests may reduce the stability or security of the system.
94+
95+
96+
97+
Configuring the kernel
98+
----------------------
99+
100+
In order to enable KUnit itself, you simply need to enable the ``CONFIG_KUNIT``
101+
Kconfig option (it's under Kernel Hacking/Kernel Testing and Coverage in
102+
menuconfig). From there, you can enable any KUnit tests you want: they usually
103+
have config options ending in ``_KUNIT_TEST``.
104+
105+
KUnit and KUnit tests can be compiled as modules: in this case the tests in a
106+
module will be run when the module is loaded.
107+
108+
Running the tests
109+
-----------------
110+
111+
Build and run your kernel as usual. Test output will be written to the kernel
112+
log in `TAP <https://testanything.org/>`_ format.
113+
114+
.. note::
115+
It's possible that there will be other lines and/or data interspersed in the
116+
TAP output.
117+
118+
65119
Writing your first test
66120
=======================
67121

0 commit comments

Comments
 (0)