1.. SPDX-License-Identifier: GPL-2.0 2 3=============== 4Getting Started 5=============== 6 7Installing Dependencies 8======================= 9KUnit has the same dependencies as the Linux kernel. As long as you can 10build the kernel, you can run KUnit. 11 12Running tests with kunit_tool 13============================= 14kunit_tool is a Python script, which configures and builds a kernel, runs 15tests, and formats the test results. From the kernel repository, you 16can run kunit_tool: 17 18.. code-block:: bash 19 20 ./tools/testing/kunit/kunit.py run 21 22For more information on this wrapper, see: 23Documentation/dev-tools/kunit/run_wrapper.rst. 24 25Creating a ``.kunitconfig`` 26--------------------------- 27 28By default, kunit_tool runs a selection of tests. However, you can specify which 29unit tests to run by creating a ``.kunitconfig`` file with kernel config options 30that enable only a specific set of tests and their dependencies. 31The ``.kunitconfig`` file contains a list of kconfig options which are required 32to run the desired targets. The ``.kunitconfig`` also contains any other test 33specific config options, such as test dependencies. For example: the 34``FAT_FS`` tests - ``FAT_KUNIT_TEST``, depends on 35``FAT_FS``. ``FAT_FS`` can be enabled by selecting either ``MSDOS_FS`` 36or ``VFAT_FS``. To run ``FAT_KUNIT_TEST``, the ``.kunitconfig`` has: 37 38.. code-block:: none 39 40 CONFIG_KUNIT=y 41 CONFIG_MSDOS_FS=y 42 CONFIG_FAT_KUNIT_TEST=y 43 441. A good starting point for the ``.kunitconfig``, is the KUnit default 45 config. Run the command: 46 47.. code-block:: bash 48 49 cd $PATH_TO_LINUX_REPO 50 cp tools/testing/kunit/configs/default.config .kunitconfig 51 52.. note :: 53 You may want to remove CONFIG_KUNIT_ALL_TESTS from the ``.kunitconfig`` as 54 it will enable a number of additional tests that you may not want. 55 562. You can then add any other Kconfig options, for example: 57 58.. code-block:: none 59 60 CONFIG_LIST_KUNIT_TEST=y 61 62Before running the tests, kunit_tool ensures that all config options 63set in ``.kunitconfig`` are set in the kernel ``.config``. It will warn 64you if you have not included dependencies for the options used. 65 66.. note :: 67 The configuration is only updated if the ``.kunitconfig`` is not a 68 subset of ``.config``. You can use tools (for example: 69 make menuconfig) to adjust other config options. 70 71Running Tests (KUnit Wrapper) 72----------------------------- 731. To make sure that everything is set up correctly, invoke the Python 74 wrapper from your kernel repository: 75 76.. code-block:: bash 77 78 ./tools/testing/kunit/kunit.py run 79 80If everything worked correctly, you should see the following: 81 82.. code-block:: 83 84 Generating .config ... 85 Building KUnit Kernel ... 86 Starting KUnit Kernel ... 87 88The tests will pass or fail. 89 90.. note :: 91 Because it is building a lot of sources for the first time, the 92 ``Building KUnit kernel`` may take a while. 93 94Running Tests without the KUnit Wrapper 95======================================= 96If you do not want to use the KUnit Wrapper (for example: you want code 97under test to integrate with other systems, or use a different/ 98unsupported architecture or configuration), KUnit can be included in 99any kernel, and the results are read out and parsed manually. 100 101.. note :: 102 ``CONFIG_KUNIT`` should not be enabled in a production environment. 103 Enabling KUnit disables Kernel Address-Space Layout Randomization 104 (KASLR), and tests may affect the state of the kernel in ways not 105 suitable for production. 106 107Configuring the Kernel 108---------------------- 109To enable KUnit itself, you need to enable the ``CONFIG_KUNIT`` Kconfig 110option (under Kernel Hacking/Kernel Testing and Coverage in 111``menuconfig``). From there, you can enable any KUnit tests. They 112usually have config options ending in ``_KUNIT_TEST``. 113 114KUnit and KUnit tests can be compiled as modules. The tests in a module 115will run when the module is loaded. 116 117Running Tests (without KUnit Wrapper) 118------------------------------------- 119Build and run your kernel. In the kernel log, the test output is printed 120out in the TAP format. This will only happen by default if KUnit/tests 121are built-in. Otherwise the module will need to be loaded. 122 123.. note :: 124 Some lines and/or data may get interspersed in the TAP output. 125 126Writing Your First Test 127======================= 128In your kernel repository, let's add some code that we can test. 129 1301. Create a file ``drivers/misc/example.h``, which includes: 131 132.. code-block:: c 133 134 int misc_example_add(int left, int right); 135 1362. Create a file ``drivers/misc/example.c``, which includes: 137 138.. code-block:: c 139 140 #include <linux/errno.h> 141 142 #include "example.h" 143 144 int misc_example_add(int left, int right) 145 { 146 return left + right; 147 } 148 1493. Add the following lines to ``drivers/misc/Kconfig``: 150 151.. code-block:: kconfig 152 153 config MISC_EXAMPLE 154 bool "My example" 155 1564. Add the following lines to ``drivers/misc/Makefile``: 157 158.. code-block:: make 159 160 obj-$(CONFIG_MISC_EXAMPLE) += example.o 161 162Now we are ready to write the test cases. 163 1641. Add the below test case in ``drivers/misc/example_test.c``: 165 166.. code-block:: c 167 168 #include <kunit/test.h> 169 #include "example.h" 170 171 /* Define the test cases. */ 172 173 static void misc_example_add_test_basic(struct kunit *test) 174 { 175 KUNIT_EXPECT_EQ(test, 1, misc_example_add(1, 0)); 176 KUNIT_EXPECT_EQ(test, 2, misc_example_add(1, 1)); 177 KUNIT_EXPECT_EQ(test, 0, misc_example_add(-1, 1)); 178 KUNIT_EXPECT_EQ(test, INT_MAX, misc_example_add(0, INT_MAX)); 179 KUNIT_EXPECT_EQ(test, -1, misc_example_add(INT_MAX, INT_MIN)); 180 } 181 182 static void misc_example_test_failure(struct kunit *test) 183 { 184 KUNIT_FAIL(test, "This test never passes."); 185 } 186 187 static struct kunit_case misc_example_test_cases[] = { 188 KUNIT_CASE(misc_example_add_test_basic), 189 KUNIT_CASE(misc_example_test_failure), 190 {} 191 }; 192 193 static struct kunit_suite misc_example_test_suite = { 194 .name = "misc-example", 195 .test_cases = misc_example_test_cases, 196 }; 197 kunit_test_suite(misc_example_test_suite); 198 1992. Add the following lines to ``drivers/misc/Kconfig``: 200 201.. code-block:: kconfig 202 203 config MISC_EXAMPLE_TEST 204 tristate "Test for my example" if !KUNIT_ALL_TESTS 205 depends on MISC_EXAMPLE && KUNIT=y 206 default KUNIT_ALL_TESTS 207 2083. Add the following lines to ``drivers/misc/Makefile``: 209 210.. code-block:: make 211 212 obj-$(CONFIG_MISC_EXAMPLE_TEST) += example_test.o 213 2144. Add the following lines to ``.kunitconfig``: 215 216.. code-block:: none 217 218 CONFIG_MISC_EXAMPLE=y 219 CONFIG_MISC_EXAMPLE_TEST=y 220 2215. Run the test: 222 223.. code-block:: bash 224 225 ./tools/testing/kunit/kunit.py run 226 227You should see the following failure: 228 229.. code-block:: none 230 231 ... 232 [16:08:57] [PASSED] misc-example:misc_example_add_test_basic 233 [16:08:57] [FAILED] misc-example:misc_example_test_failure 234 [16:08:57] EXPECTATION FAILED at drivers/misc/example-test.c:17 235 [16:08:57] This test never passes. 236 ... 237 238Congrats! You just wrote your first KUnit test. 239 240Next Steps 241========== 242 243* Documentation/dev-tools/kunit/architecture.rst - KUnit architecture. 244* Documentation/dev-tools/kunit/run_wrapper.rst - run kunit_tool. 245* Documentation/dev-tools/kunit/run_manual.rst - run tests without kunit_tool. 246* Documentation/dev-tools/kunit/usage.rst - write tests. 247* Documentation/dev-tools/kunit/tips.rst - best practices with 248 examples. 249* Documentation/dev-tools/kunit/api/index.rst - KUnit APIs 250 used for testing. 251* Documentation/dev-tools/kunit/kunit-tool.rst - kunit_tool helper 252 script. 253* Documentation/dev-tools/kunit/faq.rst - KUnit common questions and 254 answers. 255