1 .. SPDX-License-Identifier: GPL-2.0
3 ============================
4 Tips For Writing KUnit Tests
5 ============================
7 Exiting early on failed expectations
8 ------------------------------------
10 ``KUNIT_EXPECT_EQ`` and friends will mark the test as failed and continue
11 execution. In some cases, it's unsafe to continue and you can use the
12 ``KUNIT_ASSERT`` variant to exit on failure.
16 void example_test_user_alloc_function(struct kunit *test)
18 void *object = alloc_some_object_for_me();
20 /* Make sure we got a valid pointer back. */
21 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, object);
22 do_something_with_object(object);
28 Where you would use ``kzalloc``, you should prefer ``kunit_kzalloc`` instead.
29 KUnit will ensure the memory is freed once the test completes.
31 This is particularly useful since it lets you use the ``KUNIT_ASSERT_EQ``
32 macros to exit early from a test without having to worry about remembering to
39 void example_test_allocation(struct kunit *test)
41 char *buffer = kunit_kzalloc(test, 16, GFP_KERNEL);
42 /* Ensure allocation succeeded. */
43 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buffer);
45 KUNIT_ASSERT_STREQ(test, buffer, "");
49 Testing static functions
50 ------------------------
52 If you don't want to expose functions or variables just for testing, one option
53 is to conditionally ``#include`` the test file at the end of your .c file, e.g.
59 static int do_interesting_thing();
61 #ifdef CONFIG_MY_KUNIT_TEST
62 #include "my_kunit_test.c"
65 Injecting test-only code
66 ------------------------
68 Similarly to the above, it can be useful to add test-specific logic.
74 #ifdef CONFIG_MY_KUNIT_TEST
75 /* Defined in my_kunit_test.c */
76 void test_only_hook(void);
78 void test_only_hook(void) { }
81 This test-only code can be made more useful by accessing the current kunit
84 Accessing the current test
85 --------------------------
87 In some cases, you need to call test-only code from outside the test file, e.g.
88 like in the example above or if you're providing a fake implementation of an
90 There is a ``kunit_test`` field in ``task_struct``, so you can access it via
91 ``current->kunit_test``.
93 Here's a slightly in-depth example of how one could implement "mocking":
97 #include <linux/sched.h> /* for current */
101 int want_foo_called_with;
104 static int fake_foo(int arg)
106 struct kunit *test = current->kunit_test;
107 struct test_data *test_data = test->priv;
109 KUNIT_EXPECT_EQ(test, test_data->want_foo_called_with, arg);
110 return test_data->foo_result;
113 static void example_simple_test(struct kunit *test)
115 /* Assume priv is allocated in the suite's .init */
116 struct test_data *test_data = test->priv;
118 test_data->foo_result = 42;
119 test_data->want_foo_called_with = 1;
121 /* In a real test, we'd probably pass a pointer to fake_foo somewhere
122 * like an ops struct, etc. instead of calling it directly. */
123 KUNIT_EXPECT_EQ(test, fake_foo(1), 42);
127 Note: here we're able to get away with using ``test->priv``, but if you wanted
128 something more flexible you could use a named ``kunit_resource``, see
129 Documentation/dev-tools/kunit/api/test.rst.
131 Failing the current test
132 ------------------------
134 But sometimes, you might just want to fail the current test. In that case, we
135 have ``kunit_fail_current_test(fmt, args...)`` which is defined in ``<kunit/test-bug.h>`` and
136 doesn't require pulling in ``<kunit/test.h>``.
138 E.g. say we had an option to enable some extra debug checks on some data structure:
142 #include <kunit/test-bug.h>
144 #ifdef CONFIG_EXTRA_DEBUG_CHECKS
145 static void validate_my_data(struct data *data)
150 kunit_fail_current_test("data %p is invalid", data);
152 /* Normal, non-KUnit, error reporting code here. */
155 static void my_debug_function(void) { }
159 Customizing error messages
160 --------------------------
162 Each of the ``KUNIT_EXPECT`` and ``KUNIT_ASSERT`` macros have a ``_MSG`` variant.
163 These take a format string and arguments to provide additional context to the automatically generated error messages.
168 generate_sha1_hex_string(some_str);
170 /* Before. Not easy to tell why the test failed. */
171 KUNIT_EXPECT_EQ(test, strlen(some_str), 40);
173 /* After. Now we see the offending string. */
174 KUNIT_EXPECT_EQ_MSG(test, strlen(some_str), 40, "some_str='%s'", some_str);
176 Alternatively, one can take full control over the error message by using ``KUNIT_FAIL()``, e.g.
181 KUNIT_EXPECT_EQ(test, some_setup_function(), 0);
183 /* After: full control over the failure message. */
184 if (some_setup_function())
185 KUNIT_FAIL(test, "Failed to setup thing for testing");
189 * Optional: see the Documentation/dev-tools/kunit/usage.rst page for a more
190 in-depth explanation of KUnit.