Getting Started
Peios: Use Building Peios for the repository build, toolchain, configuration and test entry points. This article describes the underlying Linux machinery or upstream workflow. Its standalone build/install commands do not establish a qualified Peios kernel or signed release modules.
This page contains an overview of the kunit_tool and KUnit framework, teaching how to run existing tests and then how to write a simple test case, and covers common problems users face when using KUnit for the first time.
Installing Dependencies #
KUnit has the same dependencies as the Linux kernel. As long as you can build the kernel, you can run KUnit.
Running tests with kunit_tool #
kunit_tool is a Python script, which configures and builds a kernel, runs tests, and formats the test results. From the kernel repository, you can run kunit_tool:
If everything worked correctly, you should see the following:
Configuring KUnit Kernel ...
Building KUnit Kernel ...
Starting KUnit Kernel ...
The tests will pass or fail.
For detailed information on this wrapper, see: Documentation/dev-tools/kunit/run_wrapper.rst.
Selecting which tests to run #
By default, kunit_tool runs all tests reachable with minimal configuration, that is, using default values for most of the kconfig options. However, you can select which tests to run by:
-
Customizing Kconfig used to compile the kernel, or
-
Filtering tests by name to select specifically which compiled tests to run.
Customizing Kconfig #
A good starting point for the .kunitconfig is the KUnit default config. If you didn't run kunit.py run yet, you can generate it by running:
Before running the tests, kunit_tool ensures that all config options set in .kunitconfig are set in the kernel .config. It will warn you if you have not included dependencies for the options used.
There are many ways to customize the configurations:
-
a. Edit
.kunit/.kunitconfig. The file should contain the list of kconfig options required to run the desired tests, including their dependencies. You may want to remove CONFIG_KUNIT_ALL_TESTS from the.kunitconfigas it will enable a number of additional tests that you may not want. If you need to run on an architecture other than UML see Running tests on QEMU. -
b. Enable additional kconfig options on top of
.kunit/.kunitconfig. For example, to include the kernel's linked-list test you can run:./tools/testing/kunit/kunit.py run \ --kconfig_add CONFIG_LIST_KUNIT_TEST=y -
c. Provide the path of one or more .kunitconfig files from the tree. For example, to run only
FAT_FSandEXT4tests you can run:./tools/testing/kunit/kunit.py run \ --kunitconfig ./fs/fat/.kunitconfig \ --kunitconfig ./fs/ext4/.kunitconfig -
d. If you change the
.kunitconfig, kunit.py will trigger a rebuild of the.configfile. But you can edit the.configfile directly or with tools likemake menuconfig O=.kunit. As long as its a superset of.kunitconfig, kunit.py won't overwrite your changes.
Filtering tests by name #
If you want to be more specific than Kconfig can provide, it is also possible to select which tests to execute at boot-time by passing a glob filter (read instructions regarding the pattern in the manpage glob(7)). If there is a "." (period) in the filter, it will be interpreted as a separator between the name of the test suite and the test case, otherwise, it will be interpreted as the name of the test suite. For example, let's assume we are using the default config:
-
a. inform the name of a test suite, like
"kunit_executor_test", to run every test case it contains:./tools/testing/kunit/kunit.py run "kunit_executor_test" -
b. inform the name of a test case prefixed by its test suite, like
"example.example_simple_test", to run specifically that test case:./tools/testing/kunit/kunit.py run "example.example_simple_test" -
c. use wildcard characters (
*?[) to run any test case that matches the pattern, like"*.*64*"to run test cases containing"64"in the name inside any test suite:./tools/testing/kunit/kunit.py run "*.*64*"
Running Tests without the KUnit Wrapper #
If you do not want to use the KUnit Wrapper (for example: you want code under test to integrate with other systems, or use a different/ unsupported architecture or configuration), KUnit can be included in any kernel, and the results are read out and parsed manually.
Configuring the Kernel #
To enable KUnit itself, you need to enable the CONFIG_KUNIT Kconfig option (under Kernel Hacking/Kernel Testing and Coverage in menuconfig). From there, you can enable any KUnit tests. They usually have config options ending in _KUNIT_TEST.
KUnit and KUnit tests can be compiled as modules. The tests in a module will run when the module is loaded.
Running Tests (without KUnit Wrapper) #
Build and run your kernel. In the kernel log, the test output is printed out in the TAP format. This will only happen by default if KUnit/tests are built-in. Otherwise the module will need to be loaded.
Writing Your First Test #
In your kernel repository, let's add some code that we can test.
- Create a file
drivers/misc/example.h, which includes:
int ;
- Create a file
drivers/misc/example.c, which includes:
int
- Add the following lines to
drivers/misc/Kconfig:
config MISC_EXAMPLE
bool "My example"
- Add the following lines to
drivers/misc/Makefile:
obj-$(CONFIG_MISC_EXAMPLE) +=
Now we are ready to write the test cases.
- Add the below test case in
drivers/misc/example_test.c:
/* Define the test cases. */
static void
static void
static struct kunit_case misc_example_test_cases = ;
static struct kunit_suite misc_example_test_suite = ;
;
;
- Add the following lines to
drivers/misc/Kconfig:
config MISC_EXAMPLE_TEST
tristate "Test for my example" if !KUNIT_ALL_TESTS
depends on MISC_EXAMPLE && KUNIT
default KUNIT_ALL_TESTS
Note: If your test does not support being built as a loadable module (which is discouraged), replace tristate by bool, and depend on KUNIT=y instead of KUNIT.
- Add the following lines to
drivers/misc/Makefile:
obj-$(CONFIG_MISC_EXAMPLE_TEST) +=
- Add the following lines to
.kunit/.kunitconfig:
CONFIG_MISC_EXAMPLE=y
CONFIG_MISC_EXAMPLE_TEST=y
- Run the test:
You should see the following failure:
...
[16:08:57] [PASSED] misc-example:misc_example_add_test_basic
[16:08:57] [FAILED] misc-example:misc_example_test_failure
[16:08:57] EXPECTATION FAILED at drivers/misc/example-test.c:17
[16:08:57] This test never passes.
...
Congrats! You just wrote your first KUnit test.
Next Steps #
If you're interested in using some of the more advanced features of kunit.py, take a look at Documentation/dev-tools/kunit/run_wrapper.rst
If you'd like to run tests without using kunit.py, check out Documentation/dev-tools/kunit/run_manual.rst
For more information on writing KUnit tests (including some common techniques for testing different things), see Documentation/dev-tools/kunit/usage.rst