xref: /openbmc/linux/lib/kunit/kunit-example-test.c (revision 7b3391057fa1921ba84ef17852a6998c0af27e4d)
1c475c77dSAlan Maguire // SPDX-License-Identifier: GPL-2.0
2c475c77dSAlan Maguire /*
3c475c77dSAlan Maguire  * Example KUnit test to show how to use KUnit.
4c475c77dSAlan Maguire  *
5c475c77dSAlan Maguire  * Copyright (C) 2019, Google LLC.
6c475c77dSAlan Maguire  * Author: Brendan Higgins <brendanhiggins@google.com>
7c475c77dSAlan Maguire  */
8c475c77dSAlan Maguire 
9c475c77dSAlan Maguire #include <kunit/test.h>
10c475c77dSAlan Maguire 
11c475c77dSAlan Maguire /*
12c475c77dSAlan Maguire  * This is the most fundamental element of KUnit, the test case. A test case
13c475c77dSAlan Maguire  * makes a set EXPECTATIONs and ASSERTIONs about the behavior of some code; if
14c475c77dSAlan Maguire  * any expectations or assertions are not met, the test fails; otherwise, the
15c475c77dSAlan Maguire  * test passes.
16c475c77dSAlan Maguire  *
17c475c77dSAlan Maguire  * In KUnit, a test case is just a function with the signature
18c475c77dSAlan Maguire  * `void (*)(struct kunit *)`. `struct kunit` is a context object that stores
19c475c77dSAlan Maguire  * information about the current test.
20c475c77dSAlan Maguire  */
21c475c77dSAlan Maguire static void example_simple_test(struct kunit *test)
22c475c77dSAlan Maguire {
23c475c77dSAlan Maguire 	/*
24c475c77dSAlan Maguire 	 * This is an EXPECTATION; it is how KUnit tests things. When you want
25c475c77dSAlan Maguire 	 * to test a piece of code, you set some expectations about what the
26c475c77dSAlan Maguire 	 * code should do. KUnit then runs the test and verifies that the code's
27c475c77dSAlan Maguire 	 * behavior matched what was expected.
28c475c77dSAlan Maguire 	 */
29c475c77dSAlan Maguire 	KUNIT_EXPECT_EQ(test, 1 + 1, 2);
30c475c77dSAlan Maguire }
31c475c77dSAlan Maguire 
32c475c77dSAlan Maguire /*
33c475c77dSAlan Maguire  * This is run once before each test case, see the comment on
34c475c77dSAlan Maguire  * example_test_suite for more information.
35c475c77dSAlan Maguire  */
36c475c77dSAlan Maguire static int example_test_init(struct kunit *test)
37c475c77dSAlan Maguire {
38c475c77dSAlan Maguire 	kunit_info(test, "initializing\n");
39c475c77dSAlan Maguire 
40c475c77dSAlan Maguire 	return 0;
41c475c77dSAlan Maguire }
42c475c77dSAlan Maguire 
43c475c77dSAlan Maguire /*
44d99ea675SDavid Gow  * This test should always be skipped.
45d99ea675SDavid Gow  */
46d99ea675SDavid Gow static void example_skip_test(struct kunit *test)
47d99ea675SDavid Gow {
48d99ea675SDavid Gow 	/* This line should run */
49d99ea675SDavid Gow 	kunit_info(test, "You should not see a line below.");
50d99ea675SDavid Gow 
51d99ea675SDavid Gow 	/* Skip (and abort) the test */
52d99ea675SDavid Gow 	kunit_skip(test, "this test should be skipped");
53d99ea675SDavid Gow 
54d99ea675SDavid Gow 	/* This line should not execute */
55d99ea675SDavid Gow 	KUNIT_FAIL(test, "You should not see this line.");
56d99ea675SDavid Gow }
57d99ea675SDavid Gow 
58d99ea675SDavid Gow /*
59d99ea675SDavid Gow  * This test should always be marked skipped.
60d99ea675SDavid Gow  */
61d99ea675SDavid Gow static void example_mark_skipped_test(struct kunit *test)
62d99ea675SDavid Gow {
63d99ea675SDavid Gow 	/* This line should run */
64d99ea675SDavid Gow 	kunit_info(test, "You should see a line below.");
65d99ea675SDavid Gow 
66d99ea675SDavid Gow 	/* Skip (but do not abort) the test */
67d99ea675SDavid Gow 	kunit_mark_skipped(test, "this test should be skipped");
68d99ea675SDavid Gow 
69d99ea675SDavid Gow 	/* This line should run */
70d99ea675SDavid Gow 	kunit_info(test, "You should see this line.");
71d99ea675SDavid Gow }
72*7b339105SDaniel Latypov 
73*7b339105SDaniel Latypov /*
74*7b339105SDaniel Latypov  * This test shows off all the types of KUNIT_EXPECT macros.
75*7b339105SDaniel Latypov  */
76*7b339105SDaniel Latypov static void example_all_expect_macros_test(struct kunit *test)
77*7b339105SDaniel Latypov {
78*7b339105SDaniel Latypov 	/* Boolean assertions */
79*7b339105SDaniel Latypov 	KUNIT_EXPECT_TRUE(test, true);
80*7b339105SDaniel Latypov 	KUNIT_EXPECT_FALSE(test, false);
81*7b339105SDaniel Latypov 
82*7b339105SDaniel Latypov 	/* Integer assertions */
83*7b339105SDaniel Latypov 	KUNIT_EXPECT_EQ(test, 1, 1); /* check == */
84*7b339105SDaniel Latypov 	KUNIT_EXPECT_GE(test, 1, 1); /* check >= */
85*7b339105SDaniel Latypov 	KUNIT_EXPECT_LE(test, 1, 1); /* check <= */
86*7b339105SDaniel Latypov 	KUNIT_EXPECT_NE(test, 1, 0); /* check != */
87*7b339105SDaniel Latypov 	KUNIT_EXPECT_GT(test, 1, 0); /* check >  */
88*7b339105SDaniel Latypov 	KUNIT_EXPECT_LT(test, 0, 1); /* check <  */
89*7b339105SDaniel Latypov 
90*7b339105SDaniel Latypov 	/* Pointer assertions */
91*7b339105SDaniel Latypov 	KUNIT_EXPECT_NOT_ERR_OR_NULL(test, test);
92*7b339105SDaniel Latypov 	KUNIT_EXPECT_PTR_EQ(test, NULL, NULL);
93*7b339105SDaniel Latypov 	KUNIT_EXPECT_PTR_NE(test, test, NULL);
94*7b339105SDaniel Latypov 
95*7b339105SDaniel Latypov 	/* String assertions */
96*7b339105SDaniel Latypov 	KUNIT_EXPECT_STREQ(test, "hi", "hi");
97*7b339105SDaniel Latypov 	KUNIT_EXPECT_STRNEQ(test, "hi", "bye");
98*7b339105SDaniel Latypov 
99*7b339105SDaniel Latypov 	/*
100*7b339105SDaniel Latypov 	 * There are also ASSERT variants of all of the above that abort test
101*7b339105SDaniel Latypov 	 * execution if they fail. Useful for memory allocations, etc.
102*7b339105SDaniel Latypov 	 */
103*7b339105SDaniel Latypov 	KUNIT_ASSERT_GT(test, sizeof(char), 0);
104*7b339105SDaniel Latypov 
105*7b339105SDaniel Latypov 	/*
106*7b339105SDaniel Latypov 	 * There are also _MSG variants of all of the above that let you include
107*7b339105SDaniel Latypov 	 * additional text on failure.
108*7b339105SDaniel Latypov 	 */
109*7b339105SDaniel Latypov 	KUNIT_EXPECT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!");
110*7b339105SDaniel Latypov 	KUNIT_ASSERT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!");
111*7b339105SDaniel Latypov }
112*7b339105SDaniel Latypov 
113d99ea675SDavid Gow /*
114c475c77dSAlan Maguire  * Here we make a list of all the test cases we want to add to the test suite
115c475c77dSAlan Maguire  * below.
116c475c77dSAlan Maguire  */
117c475c77dSAlan Maguire static struct kunit_case example_test_cases[] = {
118c475c77dSAlan Maguire 	/*
119c475c77dSAlan Maguire 	 * This is a helper to create a test case object from a test case
120c475c77dSAlan Maguire 	 * function; its exact function is not important to understand how to
121c475c77dSAlan Maguire 	 * use KUnit, just know that this is how you associate test cases with a
122c475c77dSAlan Maguire 	 * test suite.
123c475c77dSAlan Maguire 	 */
124c475c77dSAlan Maguire 	KUNIT_CASE(example_simple_test),
125d99ea675SDavid Gow 	KUNIT_CASE(example_skip_test),
126d99ea675SDavid Gow 	KUNIT_CASE(example_mark_skipped_test),
127*7b339105SDaniel Latypov 	KUNIT_CASE(example_all_expect_macros_test),
128c475c77dSAlan Maguire 	{}
129c475c77dSAlan Maguire };
130c475c77dSAlan Maguire 
131c475c77dSAlan Maguire /*
132c475c77dSAlan Maguire  * This defines a suite or grouping of tests.
133c475c77dSAlan Maguire  *
134c475c77dSAlan Maguire  * Test cases are defined as belonging to the suite by adding them to
135c475c77dSAlan Maguire  * `kunit_cases`.
136c475c77dSAlan Maguire  *
137c475c77dSAlan Maguire  * Often it is desirable to run some function which will set up things which
138c475c77dSAlan Maguire  * will be used by every test; this is accomplished with an `init` function
139c475c77dSAlan Maguire  * which runs before each test case is invoked. Similarly, an `exit` function
140c475c77dSAlan Maguire  * may be specified which runs after every test case and can be used to for
141c475c77dSAlan Maguire  * cleanup. For clarity, running tests in a test suite would behave as follows:
142c475c77dSAlan Maguire  *
143c475c77dSAlan Maguire  * suite.init(test);
144c475c77dSAlan Maguire  * suite.test_case[0](test);
145c475c77dSAlan Maguire  * suite.exit(test);
146c475c77dSAlan Maguire  * suite.init(test);
147c475c77dSAlan Maguire  * suite.test_case[1](test);
148c475c77dSAlan Maguire  * suite.exit(test);
149c475c77dSAlan Maguire  * ...;
150c475c77dSAlan Maguire  */
151c475c77dSAlan Maguire static struct kunit_suite example_test_suite = {
152c475c77dSAlan Maguire 	.name = "example",
153c475c77dSAlan Maguire 	.init = example_test_init,
154c475c77dSAlan Maguire 	.test_cases = example_test_cases,
155c475c77dSAlan Maguire };
156c475c77dSAlan Maguire 
157c475c77dSAlan Maguire /*
158c475c77dSAlan Maguire  * This registers the above test suite telling KUnit that this is a suite of
159c475c77dSAlan Maguire  * tests that need to be run.
160c475c77dSAlan Maguire  */
161c475c77dSAlan Maguire kunit_test_suites(&example_test_suite);
162c475c77dSAlan Maguire 
163c475c77dSAlan Maguire MODULE_LICENSE("GPL v2");
164