5 The kernel contains a set of "self tests" under the tools/testing/selftests/
6 directory. These are intended to be small tests to exercise individual code
7 paths in the kernel. Tests are intended to be run after building, installing
10 On some systems, hot-plug tests could hang forever waiting for cpu and
11 memory to be ready to be offlined. A special hot-plug target is created
12 to run the full range of hot-plug tests. In default mode, hot-plug tests run
13 in safe mode with a limited scope. In limited mode, cpu-hotplug test is
14 run on a single cpu as opposed to all hotplug capable cpus, and memory
15 hotplug test is run on 2% of hotplug capable memory instead of 10%.
17 kselftest runs as a userspace process. Tests that can be written/run in
18 userspace may wish to use the `Test Harness`_. Tests that need to be
19 run in kernel space may wish to use a `Test Module`_.
21 Running the selftests (hotplug tests are run in limited mode)
22 =============================================================
26 $ make -C tools/testing/selftests
30 $ make -C tools/testing/selftests run_tests
32 To build and run the tests with a single command, use::
36 Note that some tests will require root privileges.
38 Build and run from user specific object directory (make O=dir)::
40 $ make O=/tmp/kselftest kselftest
42 Build and run KBUILD_OUTPUT directory (make KBUILD_OUTPUT=)::
44 $ make KBUILD_OUTPUT=/tmp/kselftest kselftest
46 The above commands run the tests and print pass/fail summary to make it
47 easier to understand the test results. Please find the detailed individual
48 test results for each test in /tmp/testname file(s).
50 Running a subset of selftests
51 =============================
53 You can use the "TARGETS" variable on the make command line to specify
54 single test to run, or a list of tests to run.
56 To run only tests targeted for a single subsystem::
58 $ make -C tools/testing/selftests TARGETS=ptrace run_tests
60 You can specify multiple tests to build and run::
62 $ make TARGETS="size timers" kselftest
64 Build and run from user specific object directory (make O=dir)::
66 $ make O=/tmp/kselftest TARGETS="size timers" kselftest
68 Build and run KBUILD_OUTPUT directory (make KBUILD_OUTPUT=)::
70 $ make KBUILD_OUTPUT=/tmp/kselftest TARGETS="size timers" kselftest
72 The above commands run the tests and print pass/fail summary to make it
73 easier to understand the test results. Please find the detailed individual
74 test results for each test in /tmp/testname file(s).
76 See the top-level tools/testing/selftests/Makefile for the list of all
79 Running the full range hotplug selftests
80 ========================================
82 To build the hotplug tests::
84 $ make -C tools/testing/selftests hotplug
86 To run the hotplug tests::
88 $ make -C tools/testing/selftests run_hotplug
90 Note that some tests will require root privileges.
96 You can use the kselftest_install.sh tool to install selftests in the
97 default location, which is tools/testing/selftests/kselftest, or in a
98 user specified location.
100 To install selftests in default location::
102 $ cd tools/testing/selftests
103 $ ./kselftest_install.sh
105 To install selftests in a user specified location::
107 $ cd tools/testing/selftests
108 $ ./kselftest_install.sh install_dir
110 Running installed selftests
111 ===========================
113 Kselftest install as well as the Kselftest tarball provide a script
114 named "run_kselftest.sh" to run the tests.
116 You can simply do the following to run the installed Kselftests. Please
117 note some tests will require root privileges::
122 Contributing new tests
123 ======================
125 In general, the rules for selftests are
127 * Do as much as you can if you're not root;
129 * Don't take too long;
131 * Don't break the build on any architecture, and
133 * Don't cause the top-level "make run_tests" to fail if your feature is
136 Contributing new tests (details)
137 ================================
139 * Use TEST_GEN_XXX if such binaries or files are generated during
142 TEST_PROGS, TEST_GEN_PROGS mean it is the executable tested by
145 TEST_CUSTOM_PROGS should be used by tests that require custom build
146 rules and prevent common build rule use.
148 TEST_PROGS are for test shell scripts. Please ensure shell script has
149 its exec bit set. Otherwise, lib.mk run_tests will generate a warning.
151 TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests.
153 TEST_PROGS_EXTENDED, TEST_GEN_PROGS_EXTENDED mean it is the
154 executable which is not tested by default.
155 TEST_FILES, TEST_GEN_FILES mean it is the file which is used by
158 * First use the headers inside the kernel source and/or git repo, and then the
159 system headers. Headers for the kernel release as opposed to headers
160 installed by the distro on the system should be the primary focus to be able
163 * If a test needs specific kernel config options enabled, add a config file in
164 the test directory to enable them.
166 e.g: tools/testing/selftests/android/config
171 Kselftest tests the kernel from userspace. Sometimes things need
172 testing from within the kernel, one method of doing this is to create a
173 test module. We can tie the module into the kselftest framework by
174 using a shell script test runner. ``kselftest_module.sh`` is designed
175 to facilitate this process. There is also a header file provided to
176 assist writing kernel modules that are for use with kselftest:
178 - ``tools/testing/kselftest/kselftest_module.h``
179 - ``tools/testing/kselftest/kselftest_module.sh``
184 Here we show the typical steps to create a test module and tie it into
185 kselftest. We use kselftests for lib/ as an example.
187 1. Create the test module
189 2. Create the test script that will run (load/unload) the module
190 e.g. ``tools/testing/selftests/lib/printf.sh``
192 3. Add line to config file e.g. ``tools/testing/selftests/lib/config``
194 4. Add test script to makefile e.g. ``tools/testing/selftests/lib/Makefile``
200 # Assumes you have booted a fresh build of this kernel tree
201 cd /path/to/linux/tree
204 sudo make modules_install
205 make TARGETS=lib kselftest
210 A bare bones test module might look like this:
214 // SPDX-License-Identifier: GPL-2.0+
216 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
218 #include "../tools/testing/selftests/kselftest_module.h"
220 KSTM_MODULE_GLOBALS();
223 * Kernel module for testing the foobinator
226 static int __init test_function()
231 static void __init selftest(void)
233 KSTM_CHECK_ZERO(do_test_case("", 0));
236 KSTM_MODULE_LOADERS(test_foo);
237 MODULE_AUTHOR("John Developer <jd@fooman.org>");
238 MODULE_LICENSE("GPL");
246 # SPDX-License-Identifier: GPL-2.0+
247 $(dirname $0)/../kselftest_module.sh "foo" test_foo
253 The kselftest_harness.h file contains useful helpers to build tests. The
254 test harness is for userspace testing, for kernel space testing see `Test
257 The tests from tools/testing/selftests/seccomp/seccomp_bpf.c can be used as
263 .. kernel-doc:: tools/testing/selftests/kselftest_harness.h
270 .. kernel-doc:: tools/testing/selftests/kselftest_harness.h
271 :functions: TH_LOG TEST TEST_SIGNAL FIXTURE FIXTURE_DATA FIXTURE_SETUP
272 FIXTURE_TEARDOWN TEST_F TEST_HARNESS_MAIN
277 .. kernel-doc:: tools/testing/selftests/kselftest_harness.h
280 .. kernel-doc:: tools/testing/selftests/kselftest_harness.h
281 :functions: ASSERT_EQ ASSERT_NE ASSERT_LT ASSERT_LE ASSERT_GT ASSERT_GE
282 ASSERT_NULL ASSERT_TRUE ASSERT_NULL ASSERT_TRUE ASSERT_FALSE
283 ASSERT_STREQ ASSERT_STRNE EXPECT_EQ EXPECT_NE EXPECT_LT
284 EXPECT_LE EXPECT_GT EXPECT_GE EXPECT_NULL EXPECT_TRUE
285 EXPECT_FALSE EXPECT_STREQ EXPECT_STRNE