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 test should always be skipped.
46 static void example_skip_test(struct kunit *test)
48 /* This line should run */
49 kunit_info(test, "You should not see a line below.");
51 /* Skip (and abort) the test */
52 kunit_skip(test, "this test should be skipped");
54 /* This line should not execute */
55 KUNIT_FAIL(test, "You should not see this line.");
59 * This test should always be marked skipped.
61 static void example_mark_skipped_test(struct kunit *test)
63 /* This line should run */
64 kunit_info(test, "You should see a line below.");
66 /* Skip (but do not abort) the test */
67 kunit_mark_skipped(test, "this test should be skipped");
69 /* This line should run */
70 kunit_info(test, "You should see this line.");
74 * This test shows off all the types of KUNIT_EXPECT macros.
76 static void example_all_expect_macros_test(struct kunit *test)
78 /* Boolean assertions */
79 KUNIT_EXPECT_TRUE(test, true);
80 KUNIT_EXPECT_FALSE(test, false);
82 /* Integer assertions */
83 KUNIT_EXPECT_EQ(test, 1, 1); /* check == */
84 KUNIT_EXPECT_GE(test, 1, 1); /* check >= */
85 KUNIT_EXPECT_LE(test, 1, 1); /* check <= */
86 KUNIT_EXPECT_NE(test, 1, 0); /* check != */
87 KUNIT_EXPECT_GT(test, 1, 0); /* check > */
88 KUNIT_EXPECT_LT(test, 0, 1); /* check < */
90 /* Pointer assertions */
91 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, test);
92 KUNIT_EXPECT_PTR_EQ(test, NULL, NULL);
93 KUNIT_EXPECT_PTR_NE(test, test, NULL);
95 /* String assertions */
96 KUNIT_EXPECT_STREQ(test, "hi", "hi");
97 KUNIT_EXPECT_STRNEQ(test, "hi", "bye");
100 * There are also ASSERT variants of all of the above that abort test
101 * execution if they fail. Useful for memory allocations, etc.
103 KUNIT_ASSERT_GT(test, sizeof(char), 0);
106 * There are also _MSG variants of all of the above that let you include
107 * additional text on failure.
109 KUNIT_EXPECT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!");
110 KUNIT_ASSERT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!");
114 * Here we make a list of all the test cases we want to add to the test suite
117 static struct kunit_case example_test_cases[] = {
119 * This is a helper to create a test case object from a test case
120 * function; its exact function is not important to understand how to
121 * use KUnit, just know that this is how you associate test cases with a
124 KUNIT_CASE(example_simple_test),
125 KUNIT_CASE(example_skip_test),
126 KUNIT_CASE(example_mark_skipped_test),
127 KUNIT_CASE(example_all_expect_macros_test),
132 * This defines a suite or grouping of tests.
134 * Test cases are defined as belonging to the suite by adding them to
137 * Often it is desirable to run some function which will set up things which
138 * will be used by every test; this is accomplished with an `init` function
139 * which runs before each test case is invoked. Similarly, an `exit` function
140 * may be specified which runs after every test case and can be used to for
141 * cleanup. For clarity, running tests in a test suite would behave as follows:
144 * suite.test_case[0](test);
147 * suite.test_case[1](test);
151 static struct kunit_suite example_test_suite = {
153 .init = example_test_init,
154 .test_cases = example_test_cases,
158 * This registers the above test suite telling KUnit that this is a suite of
159 * tests that need to be run.
161 kunit_test_suites(&example_test_suite);
163 MODULE_LICENSE("GPL v2");