1 .. SPDX-License-Identifier: GPL-2.0
7 Installing dependencies
8 =======================
9 KUnit has the same dependencies as the Linux kernel. As long as you can build
10 the kernel, you can run KUnit.
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.
17 The wrapper can be run with:
21 ./tools/testing/kunit/kunit.py run
23 For more information on this wrapper (also called kunit_tool) check out the
24 :doc:`kunit-tool` page.
26 Creating a .kunitconfig
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.
34 A good starting point for a ``.kunitconfig`` is the KUnit defconfig:
38 cd $PATH_TO_LINUX_REPO
39 cp arch/um/configs/kunit_defconfig .kunitconfig
41 You can then add any other Kconfig options you wish, e.g.:
45 CONFIG_LIST_KUNIT_TEST=y
47 :doc:`kunit_tool <kunit-tool>` will ensure that all config options set in
48 ``.kunitconfig`` are set in the kernel ``.config`` before running the tests.
49 It'll warn you if you haven't included the dependencies of the options you're
53 Note that removing something from the ``.kunitconfig`` will not trigger a
54 rebuild of the ``.config`` file: the configuration is only updated if the
55 ``.kunitconfig`` is not a subset of ``.config``. This means that you can use
56 other tools (such as make menuconfig) to adjust other config options.
59 Running the tests (KUnit Wrapper)
60 ---------------------------------
62 To make sure that everything is set up correctly, simply invoke the Python
63 wrapper from your kernel repo:
67 ./tools/testing/kunit/kunit.py run
70 You may want to run ``make mrproper`` first.
72 If everything worked correctly, you should see the following:
76 Generating .config ...
77 Building KUnit Kernel ...
78 Starting KUnit Kernel ...
80 followed by a list of tests that are run. All of them should be passing.
83 Because it is building a lot of sources for the first time, the
84 ``Building KUnit kernel`` step may take a while.
86 Running tests without the KUnit Wrapper
87 =======================================
89 If you'd rather not use the KUnit Wrapper (if, for example, you need to
90 integrate with other systems, or use an architecture other than UML), KUnit can
91 be included in any kernel, and the results read out and parsed manually.
94 KUnit is not designed for use in a production system, and it's possible that
95 tests may reduce the stability or security of the system.
99 Configuring the kernel
100 ----------------------
102 In order to enable KUnit itself, you simply need to enable the ``CONFIG_KUNIT``
103 Kconfig option (it's under Kernel Hacking/Kernel Testing and Coverage in
104 menuconfig). From there, you can enable any KUnit tests you want: they usually
105 have config options ending in ``_KUNIT_TEST``.
107 KUnit and KUnit tests can be compiled as modules: in this case the tests in a
108 module will be run when the module is loaded.
111 Running the tests (w/o KUnit Wrapper)
112 -------------------------------------
114 Build and run your kernel as usual. Test output will be written to the kernel
115 log in `TAP <https://testanything.org/>`_ format.
118 It's possible that there will be other lines and/or data interspersed in the
122 Writing your first test
123 =======================
125 In your kernel repo let's add some code that we can test. Create a file
126 ``drivers/misc/example.h`` with the contents:
130 int misc_example_add(int left, int right);
132 create a file ``drivers/misc/example.c``:
136 #include <linux/errno.h>
140 int misc_example_add(int left, int right)
145 Now add the following lines to ``drivers/misc/Kconfig``:
147 .. code-block:: kconfig
152 and the following lines to ``drivers/misc/Makefile``:
156 obj-$(CONFIG_MISC_EXAMPLE) += example.o
158 Now we are ready to write the test. The test will be in
159 ``drivers/misc/example-test.c``:
163 #include <kunit/test.h>
166 /* Define the test cases. */
168 static void misc_example_add_test_basic(struct kunit *test)
170 KUNIT_EXPECT_EQ(test, 1, misc_example_add(1, 0));
171 KUNIT_EXPECT_EQ(test, 2, misc_example_add(1, 1));
172 KUNIT_EXPECT_EQ(test, 0, misc_example_add(-1, 1));
173 KUNIT_EXPECT_EQ(test, INT_MAX, misc_example_add(0, INT_MAX));
174 KUNIT_EXPECT_EQ(test, -1, misc_example_add(INT_MAX, INT_MIN));
177 static void misc_example_test_failure(struct kunit *test)
179 KUNIT_FAIL(test, "This test never passes.");
182 static struct kunit_case misc_example_test_cases[] = {
183 KUNIT_CASE(misc_example_add_test_basic),
184 KUNIT_CASE(misc_example_test_failure),
188 static struct kunit_suite misc_example_test_suite = {
189 .name = "misc-example",
190 .test_cases = misc_example_test_cases,
192 kunit_test_suite(misc_example_test_suite);
194 Now add the following to ``drivers/misc/Kconfig``:
196 .. code-block:: kconfig
198 config MISC_EXAMPLE_TEST
199 bool "Test for my example"
200 depends on MISC_EXAMPLE && KUNIT=y
202 and the following to ``drivers/misc/Makefile``:
206 obj-$(CONFIG_MISC_EXAMPLE_TEST) += example-test.o
208 Now add it to your ``.kunitconfig``:
212 CONFIG_MISC_EXAMPLE=y
213 CONFIG_MISC_EXAMPLE_TEST=y
215 Now you can run the test:
219 ./tools/testing/kunit/kunit.py run
221 You should see the following failure:
226 [16:08:57] [PASSED] misc-example:misc_example_add_test_basic
227 [16:08:57] [FAILED] misc-example:misc_example_test_failure
228 [16:08:57] EXPECTATION FAILED at drivers/misc/example-test.c:17
229 [16:08:57] This test never passes.
232 Congrats! You just wrote your first KUnit test!
236 * Check out the :doc:`usage` page for a more
237 in-depth explanation of KUnit.