xref: /openbmc/linux/lib/kunit/kunit-example-test.c (revision 3b30fb62ec23b42be33d94ebf825d27daf508e8e)
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 /*
441cdba21dSDaniel Latypov  * This is run once before all test cases in the suite.
451cdba21dSDaniel Latypov  * See the comment on example_test_suite for more information.
461cdba21dSDaniel Latypov  */
471cdba21dSDaniel Latypov static int example_test_init_suite(struct kunit_suite *suite)
481cdba21dSDaniel Latypov {
491cdba21dSDaniel Latypov 	kunit_info(suite, "initializing suite\n");
501cdba21dSDaniel Latypov 
511cdba21dSDaniel Latypov 	return 0;
521cdba21dSDaniel Latypov }
531cdba21dSDaniel Latypov 
541cdba21dSDaniel Latypov /*
55d99ea675SDavid Gow  * This test should always be skipped.
56d99ea675SDavid Gow  */
57d99ea675SDavid Gow static void example_skip_test(struct kunit *test)
58d99ea675SDavid Gow {
59d99ea675SDavid Gow 	/* This line should run */
60d99ea675SDavid Gow 	kunit_info(test, "You should not see a line below.");
61d99ea675SDavid Gow 
62d99ea675SDavid Gow 	/* Skip (and abort) the test */
63d99ea675SDavid Gow 	kunit_skip(test, "this test should be skipped");
64d99ea675SDavid Gow 
65d99ea675SDavid Gow 	/* This line should not execute */
66d99ea675SDavid Gow 	KUNIT_FAIL(test, "You should not see this line.");
67d99ea675SDavid Gow }
68d99ea675SDavid Gow 
69d99ea675SDavid Gow /*
70d99ea675SDavid Gow  * This test should always be marked skipped.
71d99ea675SDavid Gow  */
72d99ea675SDavid Gow static void example_mark_skipped_test(struct kunit *test)
73d99ea675SDavid Gow {
74d99ea675SDavid Gow 	/* This line should run */
75d99ea675SDavid Gow 	kunit_info(test, "You should see a line below.");
76d99ea675SDavid Gow 
77d99ea675SDavid Gow 	/* Skip (but do not abort) the test */
78d99ea675SDavid Gow 	kunit_mark_skipped(test, "this test should be skipped");
79d99ea675SDavid Gow 
80d99ea675SDavid Gow 	/* This line should run */
81d99ea675SDavid Gow 	kunit_info(test, "You should see this line.");
82d99ea675SDavid Gow }
837b339105SDaniel Latypov 
847b339105SDaniel Latypov /*
857b339105SDaniel Latypov  * This test shows off all the types of KUNIT_EXPECT macros.
867b339105SDaniel Latypov  */
877b339105SDaniel Latypov static void example_all_expect_macros_test(struct kunit *test)
887b339105SDaniel Latypov {
89*3b30fb62SMaíra Canal 	const u32 array1[] = { 0x0F, 0xFF };
90*3b30fb62SMaíra Canal 	const u32 array2[] = { 0x1F, 0xFF };
91*3b30fb62SMaíra Canal 
927b339105SDaniel Latypov 	/* Boolean assertions */
937b339105SDaniel Latypov 	KUNIT_EXPECT_TRUE(test, true);
947b339105SDaniel Latypov 	KUNIT_EXPECT_FALSE(test, false);
957b339105SDaniel Latypov 
967b339105SDaniel Latypov 	/* Integer assertions */
977b339105SDaniel Latypov 	KUNIT_EXPECT_EQ(test, 1, 1); /* check == */
987b339105SDaniel Latypov 	KUNIT_EXPECT_GE(test, 1, 1); /* check >= */
997b339105SDaniel Latypov 	KUNIT_EXPECT_LE(test, 1, 1); /* check <= */
1007b339105SDaniel Latypov 	KUNIT_EXPECT_NE(test, 1, 0); /* check != */
1017b339105SDaniel Latypov 	KUNIT_EXPECT_GT(test, 1, 0); /* check >  */
1027b339105SDaniel Latypov 	KUNIT_EXPECT_LT(test, 0, 1); /* check <  */
1037b339105SDaniel Latypov 
1047b339105SDaniel Latypov 	/* Pointer assertions */
1057b339105SDaniel Latypov 	KUNIT_EXPECT_NOT_ERR_OR_NULL(test, test);
1067b339105SDaniel Latypov 	KUNIT_EXPECT_PTR_EQ(test, NULL, NULL);
1077b339105SDaniel Latypov 	KUNIT_EXPECT_PTR_NE(test, test, NULL);
108de82c15dSRicardo Ribalda 	KUNIT_EXPECT_NULL(test, NULL);
109de82c15dSRicardo Ribalda 	KUNIT_EXPECT_NOT_NULL(test, test);
1107b339105SDaniel Latypov 
1117b339105SDaniel Latypov 	/* String assertions */
1127b339105SDaniel Latypov 	KUNIT_EXPECT_STREQ(test, "hi", "hi");
1137b339105SDaniel Latypov 	KUNIT_EXPECT_STRNEQ(test, "hi", "bye");
1147b339105SDaniel Latypov 
115*3b30fb62SMaíra Canal 	/* Memory block assertions */
116*3b30fb62SMaíra Canal 	KUNIT_EXPECT_MEMEQ(test, array1, array1, sizeof(array1));
117*3b30fb62SMaíra Canal 	KUNIT_EXPECT_MEMNEQ(test, array1, array2, sizeof(array1));
118*3b30fb62SMaíra Canal 
1197b339105SDaniel Latypov 	/*
1207b339105SDaniel Latypov 	 * There are also ASSERT variants of all of the above that abort test
1217b339105SDaniel Latypov 	 * execution if they fail. Useful for memory allocations, etc.
1227b339105SDaniel Latypov 	 */
1237b339105SDaniel Latypov 	KUNIT_ASSERT_GT(test, sizeof(char), 0);
1247b339105SDaniel Latypov 
1257b339105SDaniel Latypov 	/*
1267b339105SDaniel Latypov 	 * There are also _MSG variants of all of the above that let you include
1277b339105SDaniel Latypov 	 * additional text on failure.
1287b339105SDaniel Latypov 	 */
1297b339105SDaniel Latypov 	KUNIT_EXPECT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!");
1307b339105SDaniel Latypov 	KUNIT_ASSERT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!");
1317b339105SDaniel Latypov }
1327b339105SDaniel Latypov 
133d99ea675SDavid Gow /*
134c475c77dSAlan Maguire  * Here we make a list of all the test cases we want to add to the test suite
135c475c77dSAlan Maguire  * below.
136c475c77dSAlan Maguire  */
137c475c77dSAlan Maguire static struct kunit_case example_test_cases[] = {
138c475c77dSAlan Maguire 	/*
139c475c77dSAlan Maguire 	 * This is a helper to create a test case object from a test case
140c475c77dSAlan Maguire 	 * function; its exact function is not important to understand how to
141c475c77dSAlan Maguire 	 * use KUnit, just know that this is how you associate test cases with a
142c475c77dSAlan Maguire 	 * test suite.
143c475c77dSAlan Maguire 	 */
144c475c77dSAlan Maguire 	KUNIT_CASE(example_simple_test),
145d99ea675SDavid Gow 	KUNIT_CASE(example_skip_test),
146d99ea675SDavid Gow 	KUNIT_CASE(example_mark_skipped_test),
1477b339105SDaniel Latypov 	KUNIT_CASE(example_all_expect_macros_test),
148c475c77dSAlan Maguire 	{}
149c475c77dSAlan Maguire };
150c475c77dSAlan Maguire 
151c475c77dSAlan Maguire /*
152c475c77dSAlan Maguire  * This defines a suite or grouping of tests.
153c475c77dSAlan Maguire  *
154c475c77dSAlan Maguire  * Test cases are defined as belonging to the suite by adding them to
155c475c77dSAlan Maguire  * `kunit_cases`.
156c475c77dSAlan Maguire  *
157c475c77dSAlan Maguire  * Often it is desirable to run some function which will set up things which
158c475c77dSAlan Maguire  * will be used by every test; this is accomplished with an `init` function
159c475c77dSAlan Maguire  * which runs before each test case is invoked. Similarly, an `exit` function
160c475c77dSAlan Maguire  * may be specified which runs after every test case and can be used to for
161c475c77dSAlan Maguire  * cleanup. For clarity, running tests in a test suite would behave as follows:
162c475c77dSAlan Maguire  *
1631cdba21dSDaniel Latypov  * suite.suite_init(suite);
164c475c77dSAlan Maguire  * suite.init(test);
165c475c77dSAlan Maguire  * suite.test_case[0](test);
166c475c77dSAlan Maguire  * suite.exit(test);
167c475c77dSAlan Maguire  * suite.init(test);
168c475c77dSAlan Maguire  * suite.test_case[1](test);
169c475c77dSAlan Maguire  * suite.exit(test);
1701cdba21dSDaniel Latypov  * suite.suite_exit(suite);
171c475c77dSAlan Maguire  * ...;
172c475c77dSAlan Maguire  */
173c475c77dSAlan Maguire static struct kunit_suite example_test_suite = {
174c475c77dSAlan Maguire 	.name = "example",
175c475c77dSAlan Maguire 	.init = example_test_init,
1761cdba21dSDaniel Latypov 	.suite_init = example_test_init_suite,
177c475c77dSAlan Maguire 	.test_cases = example_test_cases,
178c475c77dSAlan Maguire };
179c475c77dSAlan Maguire 
180c475c77dSAlan Maguire /*
181c475c77dSAlan Maguire  * This registers the above test suite telling KUnit that this is a suite of
182c475c77dSAlan Maguire  * tests that need to be run.
183c475c77dSAlan Maguire  */
184c475c77dSAlan Maguire kunit_test_suites(&example_test_suite);
185c475c77dSAlan Maguire 
186c475c77dSAlan Maguire MODULE_LICENSE("GPL v2");
187