1 // SPDX-License-Identifier: GPL-2.0
3 * Example KUnit test to show how to use KUnit.
5 * Copyright (C) 2019, Google LLC.
6 * Author: Brendan Higgins <brendanhiggins@google.com>
9 #include <kunit/test.h>
12 * This is the most fundamental element of KUnit, the test case. A test case
13 * makes a set EXPECTATIONs and ASSERTIONs about the behavior of some code; if
14 * any expectations or assertions are not met, the test fails; otherwise, the
17 * In KUnit, a test case is just a function with the signature
18 * `void (*)(struct kunit *)`. `struct kunit` is a context object that stores
19 * information about the current test.
21 static void example_simple_test(struct kunit *test)
24 * This is an EXPECTATION; it is how KUnit tests things. When you want
25 * to test a piece of code, you set some expectations about what the
26 * code should do. KUnit then runs the test and verifies that the code's
27 * behavior matched what was expected.
29 KUNIT_EXPECT_EQ(test, 1 + 1, 2);
33 * This is run once before each test case, see the comment on
34 * example_test_suite for more information.
36 static int example_test_init(struct kunit *test)
38 kunit_info(test, "initializing\n");
44 * This is run once before all test cases in the suite.
45 * See the comment on example_test_suite for more information.
47 static int example_test_init_suite(struct kunit_suite *suite)
49 kunit_info(suite, "initializing suite\n");
55 * This test should always be skipped.
57 static void example_skip_test(struct kunit *test)
59 /* This line should run */
60 kunit_info(test, "You should not see a line below.");
62 /* Skip (and abort) the test */
63 kunit_skip(test, "this test should be skipped");
65 /* This line should not execute */
66 KUNIT_FAIL(test, "You should not see this line.");
70 * This test should always be marked skipped.
72 static void example_mark_skipped_test(struct kunit *test)
74 /* This line should run */
75 kunit_info(test, "You should see a line below.");
77 /* Skip (but do not abort) the test */
78 kunit_mark_skipped(test, "this test should be skipped");
80 /* This line should run */
81 kunit_info(test, "You should see this line.");
85 * This test shows off all the types of KUNIT_EXPECT macros.
87 static void example_all_expect_macros_test(struct kunit *test)
89 /* Boolean assertions */
90 KUNIT_EXPECT_TRUE(test, true);
91 KUNIT_EXPECT_FALSE(test, false);
93 /* Integer assertions */
94 KUNIT_EXPECT_EQ(test, 1, 1); /* check == */
95 KUNIT_EXPECT_GE(test, 1, 1); /* check >= */
96 KUNIT_EXPECT_LE(test, 1, 1); /* check <= */
97 KUNIT_EXPECT_NE(test, 1, 0); /* check != */
98 KUNIT_EXPECT_GT(test, 1, 0); /* check > */
99 KUNIT_EXPECT_LT(test, 0, 1); /* check < */
101 /* Pointer assertions */
102 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, test);
103 KUNIT_EXPECT_PTR_EQ(test, NULL, NULL);
104 KUNIT_EXPECT_PTR_NE(test, test, NULL);
105 KUNIT_EXPECT_NULL(test, NULL);
106 KUNIT_EXPECT_NOT_NULL(test, test);
108 /* String assertions */
109 KUNIT_EXPECT_STREQ(test, "hi", "hi");
110 KUNIT_EXPECT_STRNEQ(test, "hi", "bye");
113 * There are also ASSERT variants of all of the above that abort test
114 * execution if they fail. Useful for memory allocations, etc.
116 KUNIT_ASSERT_GT(test, sizeof(char), 0);
119 * There are also _MSG variants of all of the above that let you include
120 * additional text on failure.
122 KUNIT_EXPECT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!");
123 KUNIT_ASSERT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!");
127 * Here we make a list of all the test cases we want to add to the test suite
130 static struct kunit_case example_test_cases[] = {
132 * This is a helper to create a test case object from a test case
133 * function; its exact function is not important to understand how to
134 * use KUnit, just know that this is how you associate test cases with a
137 KUNIT_CASE(example_simple_test),
138 KUNIT_CASE(example_skip_test),
139 KUNIT_CASE(example_mark_skipped_test),
140 KUNIT_CASE(example_all_expect_macros_test),
145 * This defines a suite or grouping of tests.
147 * Test cases are defined as belonging to the suite by adding them to
150 * Often it is desirable to run some function which will set up things which
151 * will be used by every test; this is accomplished with an `init` function
152 * which runs before each test case is invoked. Similarly, an `exit` function
153 * may be specified which runs after every test case and can be used to for
154 * cleanup. For clarity, running tests in a test suite would behave as follows:
156 * suite.suite_init(suite);
158 * suite.test_case[0](test);
161 * suite.test_case[1](test);
163 * suite.suite_exit(suite);
166 static struct kunit_suite example_test_suite = {
168 .init = example_test_init,
169 .suite_init = example_test_init_suite,
170 .test_cases = example_test_cases,
174 * This registers the above test suite telling KUnit that this is a suite of
175 * tests that need to be run.
177 kunit_test_suites(&example_test_suite);
179 MODULE_LICENSE("GPL v2");