xref: /openbmc/linux/lib/kunit/kunit-example-test.c (revision e047c5eaa76324575e1f95664be4c74ce0e2571b)
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>
10*e047c5eaSDavid Gow #include <kunit/static_stub.h>
11c475c77dSAlan Maguire 
12c475c77dSAlan Maguire /*
13c475c77dSAlan Maguire  * This is the most fundamental element of KUnit, the test case. A test case
14c475c77dSAlan Maguire  * makes a set EXPECTATIONs and ASSERTIONs about the behavior of some code; if
15c475c77dSAlan Maguire  * any expectations or assertions are not met, the test fails; otherwise, the
16c475c77dSAlan Maguire  * test passes.
17c475c77dSAlan Maguire  *
18c475c77dSAlan Maguire  * In KUnit, a test case is just a function with the signature
19c475c77dSAlan Maguire  * `void (*)(struct kunit *)`. `struct kunit` is a context object that stores
20c475c77dSAlan Maguire  * information about the current test.
21c475c77dSAlan Maguire  */
22c475c77dSAlan Maguire static void example_simple_test(struct kunit *test)
23c475c77dSAlan Maguire {
24c475c77dSAlan Maguire 	/*
25c475c77dSAlan Maguire 	 * This is an EXPECTATION; it is how KUnit tests things. When you want
26c475c77dSAlan Maguire 	 * to test a piece of code, you set some expectations about what the
27c475c77dSAlan Maguire 	 * code should do. KUnit then runs the test and verifies that the code's
28c475c77dSAlan Maguire 	 * behavior matched what was expected.
29c475c77dSAlan Maguire 	 */
30c475c77dSAlan Maguire 	KUNIT_EXPECT_EQ(test, 1 + 1, 2);
31c475c77dSAlan Maguire }
32c475c77dSAlan Maguire 
33c475c77dSAlan Maguire /*
34c475c77dSAlan Maguire  * This is run once before each test case, see the comment on
35c475c77dSAlan Maguire  * example_test_suite for more information.
36c475c77dSAlan Maguire  */
37c475c77dSAlan Maguire static int example_test_init(struct kunit *test)
38c475c77dSAlan Maguire {
39c475c77dSAlan Maguire 	kunit_info(test, "initializing\n");
40c475c77dSAlan Maguire 
41c475c77dSAlan Maguire 	return 0;
42c475c77dSAlan Maguire }
43c475c77dSAlan Maguire 
44c475c77dSAlan Maguire /*
451cdba21dSDaniel Latypov  * This is run once before all test cases in the suite.
461cdba21dSDaniel Latypov  * See the comment on example_test_suite for more information.
471cdba21dSDaniel Latypov  */
481cdba21dSDaniel Latypov static int example_test_init_suite(struct kunit_suite *suite)
491cdba21dSDaniel Latypov {
501cdba21dSDaniel Latypov 	kunit_info(suite, "initializing suite\n");
511cdba21dSDaniel Latypov 
521cdba21dSDaniel Latypov 	return 0;
531cdba21dSDaniel Latypov }
541cdba21dSDaniel Latypov 
551cdba21dSDaniel Latypov /*
56d99ea675SDavid Gow  * This test should always be skipped.
57d99ea675SDavid Gow  */
58d99ea675SDavid Gow static void example_skip_test(struct kunit *test)
59d99ea675SDavid Gow {
60d99ea675SDavid Gow 	/* This line should run */
61d99ea675SDavid Gow 	kunit_info(test, "You should not see a line below.");
62d99ea675SDavid Gow 
63d99ea675SDavid Gow 	/* Skip (and abort) the test */
64d99ea675SDavid Gow 	kunit_skip(test, "this test should be skipped");
65d99ea675SDavid Gow 
66d99ea675SDavid Gow 	/* This line should not execute */
67d99ea675SDavid Gow 	KUNIT_FAIL(test, "You should not see this line.");
68d99ea675SDavid Gow }
69d99ea675SDavid Gow 
70d99ea675SDavid Gow /*
71d99ea675SDavid Gow  * This test should always be marked skipped.
72d99ea675SDavid Gow  */
73d99ea675SDavid Gow static void example_mark_skipped_test(struct kunit *test)
74d99ea675SDavid Gow {
75d99ea675SDavid Gow 	/* This line should run */
76d99ea675SDavid Gow 	kunit_info(test, "You should see a line below.");
77d99ea675SDavid Gow 
78d99ea675SDavid Gow 	/* Skip (but do not abort) the test */
79d99ea675SDavid Gow 	kunit_mark_skipped(test, "this test should be skipped");
80d99ea675SDavid Gow 
81d99ea675SDavid Gow 	/* This line should run */
82d99ea675SDavid Gow 	kunit_info(test, "You should see this line.");
83d99ea675SDavid Gow }
847b339105SDaniel Latypov 
857b339105SDaniel Latypov /*
867b339105SDaniel Latypov  * This test shows off all the types of KUNIT_EXPECT macros.
877b339105SDaniel Latypov  */
887b339105SDaniel Latypov static void example_all_expect_macros_test(struct kunit *test)
897b339105SDaniel Latypov {
903b30fb62SMaíra Canal 	const u32 array1[] = { 0x0F, 0xFF };
913b30fb62SMaíra Canal 	const u32 array2[] = { 0x1F, 0xFF };
923b30fb62SMaíra Canal 
937b339105SDaniel Latypov 	/* Boolean assertions */
947b339105SDaniel Latypov 	KUNIT_EXPECT_TRUE(test, true);
957b339105SDaniel Latypov 	KUNIT_EXPECT_FALSE(test, false);
967b339105SDaniel Latypov 
977b339105SDaniel Latypov 	/* Integer assertions */
987b339105SDaniel Latypov 	KUNIT_EXPECT_EQ(test, 1, 1); /* check == */
997b339105SDaniel Latypov 	KUNIT_EXPECT_GE(test, 1, 1); /* check >= */
1007b339105SDaniel Latypov 	KUNIT_EXPECT_LE(test, 1, 1); /* check <= */
1017b339105SDaniel Latypov 	KUNIT_EXPECT_NE(test, 1, 0); /* check != */
1027b339105SDaniel Latypov 	KUNIT_EXPECT_GT(test, 1, 0); /* check >  */
1037b339105SDaniel Latypov 	KUNIT_EXPECT_LT(test, 0, 1); /* check <  */
1047b339105SDaniel Latypov 
1057b339105SDaniel Latypov 	/* Pointer assertions */
1067b339105SDaniel Latypov 	KUNIT_EXPECT_NOT_ERR_OR_NULL(test, test);
1077b339105SDaniel Latypov 	KUNIT_EXPECT_PTR_EQ(test, NULL, NULL);
1087b339105SDaniel Latypov 	KUNIT_EXPECT_PTR_NE(test, test, NULL);
109de82c15dSRicardo Ribalda 	KUNIT_EXPECT_NULL(test, NULL);
110de82c15dSRicardo Ribalda 	KUNIT_EXPECT_NOT_NULL(test, test);
1117b339105SDaniel Latypov 
1127b339105SDaniel Latypov 	/* String assertions */
1137b339105SDaniel Latypov 	KUNIT_EXPECT_STREQ(test, "hi", "hi");
1147b339105SDaniel Latypov 	KUNIT_EXPECT_STRNEQ(test, "hi", "bye");
1157b339105SDaniel Latypov 
1163b30fb62SMaíra Canal 	/* Memory block assertions */
1173b30fb62SMaíra Canal 	KUNIT_EXPECT_MEMEQ(test, array1, array1, sizeof(array1));
1183b30fb62SMaíra Canal 	KUNIT_EXPECT_MEMNEQ(test, array1, array2, sizeof(array1));
1193b30fb62SMaíra Canal 
1207b339105SDaniel Latypov 	/*
1217b339105SDaniel Latypov 	 * There are also ASSERT variants of all of the above that abort test
1227b339105SDaniel Latypov 	 * execution if they fail. Useful for memory allocations, etc.
1237b339105SDaniel Latypov 	 */
1247b339105SDaniel Latypov 	KUNIT_ASSERT_GT(test, sizeof(char), 0);
1257b339105SDaniel Latypov 
1267b339105SDaniel Latypov 	/*
1277b339105SDaniel Latypov 	 * There are also _MSG variants of all of the above that let you include
1287b339105SDaniel Latypov 	 * additional text on failure.
1297b339105SDaniel Latypov 	 */
1307b339105SDaniel Latypov 	KUNIT_EXPECT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!");
1317b339105SDaniel Latypov 	KUNIT_ASSERT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!");
1327b339105SDaniel Latypov }
1337b339105SDaniel Latypov 
134*e047c5eaSDavid Gow /* This is a function we'll replace with static stubs. */
135*e047c5eaSDavid Gow static int add_one(int i)
136*e047c5eaSDavid Gow {
137*e047c5eaSDavid Gow 	/* This will trigger the stub if active. */
138*e047c5eaSDavid Gow 	KUNIT_STATIC_STUB_REDIRECT(add_one, i);
139*e047c5eaSDavid Gow 
140*e047c5eaSDavid Gow 	return i + 1;
141*e047c5eaSDavid Gow }
142*e047c5eaSDavid Gow 
143*e047c5eaSDavid Gow /* This is used as a replacement for the above function. */
144*e047c5eaSDavid Gow static int subtract_one(int i)
145*e047c5eaSDavid Gow {
146*e047c5eaSDavid Gow 	/* We don't need to trigger the stub from the replacement. */
147*e047c5eaSDavid Gow 
148*e047c5eaSDavid Gow 	return i - 1;
149*e047c5eaSDavid Gow }
150*e047c5eaSDavid Gow 
151*e047c5eaSDavid Gow /*
152*e047c5eaSDavid Gow  * This test shows the use of static stubs.
153*e047c5eaSDavid Gow  */
154*e047c5eaSDavid Gow static void example_static_stub_test(struct kunit *test)
155*e047c5eaSDavid Gow {
156*e047c5eaSDavid Gow 	/* By default, function is not stubbed. */
157*e047c5eaSDavid Gow 	KUNIT_EXPECT_EQ(test, add_one(1), 2);
158*e047c5eaSDavid Gow 
159*e047c5eaSDavid Gow 	/* Replace add_one() with subtract_one(). */
160*e047c5eaSDavid Gow 	kunit_activate_static_stub(test, add_one, subtract_one);
161*e047c5eaSDavid Gow 
162*e047c5eaSDavid Gow 	/* add_one() is now replaced. */
163*e047c5eaSDavid Gow 	KUNIT_EXPECT_EQ(test, add_one(1), 0);
164*e047c5eaSDavid Gow 
165*e047c5eaSDavid Gow 	/* Return add_one() to normal. */
166*e047c5eaSDavid Gow 	kunit_deactivate_static_stub(test, add_one);
167*e047c5eaSDavid Gow 	KUNIT_EXPECT_EQ(test, add_one(1), 2);
168*e047c5eaSDavid Gow }
169*e047c5eaSDavid Gow 
170d99ea675SDavid Gow /*
171c475c77dSAlan Maguire  * Here we make a list of all the test cases we want to add to the test suite
172c475c77dSAlan Maguire  * below.
173c475c77dSAlan Maguire  */
174c475c77dSAlan Maguire static struct kunit_case example_test_cases[] = {
175c475c77dSAlan Maguire 	/*
176c475c77dSAlan Maguire 	 * This is a helper to create a test case object from a test case
177c475c77dSAlan Maguire 	 * function; its exact function is not important to understand how to
178c475c77dSAlan Maguire 	 * use KUnit, just know that this is how you associate test cases with a
179c475c77dSAlan Maguire 	 * test suite.
180c475c77dSAlan Maguire 	 */
181c475c77dSAlan Maguire 	KUNIT_CASE(example_simple_test),
182d99ea675SDavid Gow 	KUNIT_CASE(example_skip_test),
183d99ea675SDavid Gow 	KUNIT_CASE(example_mark_skipped_test),
1847b339105SDaniel Latypov 	KUNIT_CASE(example_all_expect_macros_test),
185*e047c5eaSDavid Gow 	KUNIT_CASE(example_static_stub_test),
186c475c77dSAlan Maguire 	{}
187c475c77dSAlan Maguire };
188c475c77dSAlan Maguire 
189c475c77dSAlan Maguire /*
190c475c77dSAlan Maguire  * This defines a suite or grouping of tests.
191c475c77dSAlan Maguire  *
192c475c77dSAlan Maguire  * Test cases are defined as belonging to the suite by adding them to
193c475c77dSAlan Maguire  * `kunit_cases`.
194c475c77dSAlan Maguire  *
195c475c77dSAlan Maguire  * Often it is desirable to run some function which will set up things which
196c475c77dSAlan Maguire  * will be used by every test; this is accomplished with an `init` function
197c475c77dSAlan Maguire  * which runs before each test case is invoked. Similarly, an `exit` function
198c475c77dSAlan Maguire  * may be specified which runs after every test case and can be used to for
199c475c77dSAlan Maguire  * cleanup. For clarity, running tests in a test suite would behave as follows:
200c475c77dSAlan Maguire  *
2011cdba21dSDaniel Latypov  * suite.suite_init(suite);
202c475c77dSAlan Maguire  * suite.init(test);
203c475c77dSAlan Maguire  * suite.test_case[0](test);
204c475c77dSAlan Maguire  * suite.exit(test);
205c475c77dSAlan Maguire  * suite.init(test);
206c475c77dSAlan Maguire  * suite.test_case[1](test);
207c475c77dSAlan Maguire  * suite.exit(test);
2081cdba21dSDaniel Latypov  * suite.suite_exit(suite);
209c475c77dSAlan Maguire  * ...;
210c475c77dSAlan Maguire  */
211c475c77dSAlan Maguire static struct kunit_suite example_test_suite = {
212c475c77dSAlan Maguire 	.name = "example",
213c475c77dSAlan Maguire 	.init = example_test_init,
2141cdba21dSDaniel Latypov 	.suite_init = example_test_init_suite,
215c475c77dSAlan Maguire 	.test_cases = example_test_cases,
216c475c77dSAlan Maguire };
217c475c77dSAlan Maguire 
218c475c77dSAlan Maguire /*
219c475c77dSAlan Maguire  * This registers the above test suite telling KUnit that this is a suite of
220c475c77dSAlan Maguire  * tests that need to be run.
221c475c77dSAlan Maguire  */
222c475c77dSAlan Maguire kunit_test_suites(&example_test_suite);
223c475c77dSAlan Maguire 
224c475c77dSAlan Maguire MODULE_LICENSE("GPL v2");
225