xref: /openbmc/linux/lib/kunit/kunit-example-test.c (revision d99ea675141934a1ea5cd1b2adff34eafcb779bc)
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 /*
44*d99ea675SDavid Gow  * This test should always be skipped.
45*d99ea675SDavid Gow  */
46*d99ea675SDavid Gow static void example_skip_test(struct kunit *test)
47*d99ea675SDavid Gow {
48*d99ea675SDavid Gow 	/* This line should run */
49*d99ea675SDavid Gow 	kunit_info(test, "You should not see a line below.");
50*d99ea675SDavid Gow 
51*d99ea675SDavid Gow 	/* Skip (and abort) the test */
52*d99ea675SDavid Gow 	kunit_skip(test, "this test should be skipped");
53*d99ea675SDavid Gow 
54*d99ea675SDavid Gow 	/* This line should not execute */
55*d99ea675SDavid Gow 	KUNIT_FAIL(test, "You should not see this line.");
56*d99ea675SDavid Gow }
57*d99ea675SDavid Gow 
58*d99ea675SDavid Gow /*
59*d99ea675SDavid Gow  * This test should always be marked skipped.
60*d99ea675SDavid Gow  */
61*d99ea675SDavid Gow static void example_mark_skipped_test(struct kunit *test)
62*d99ea675SDavid Gow {
63*d99ea675SDavid Gow 	/* This line should run */
64*d99ea675SDavid Gow 	kunit_info(test, "You should see a line below.");
65*d99ea675SDavid Gow 
66*d99ea675SDavid Gow 	/* Skip (but do not abort) the test */
67*d99ea675SDavid Gow 	kunit_mark_skipped(test, "this test should be skipped");
68*d99ea675SDavid Gow 
69*d99ea675SDavid Gow 	/* This line should run */
70*d99ea675SDavid Gow 	kunit_info(test, "You should see this line.");
71*d99ea675SDavid Gow }
72*d99ea675SDavid Gow /*
73c475c77dSAlan Maguire  * Here we make a list of all the test cases we want to add to the test suite
74c475c77dSAlan Maguire  * below.
75c475c77dSAlan Maguire  */
76c475c77dSAlan Maguire static struct kunit_case example_test_cases[] = {
77c475c77dSAlan Maguire 	/*
78c475c77dSAlan Maguire 	 * This is a helper to create a test case object from a test case
79c475c77dSAlan Maguire 	 * function; its exact function is not important to understand how to
80c475c77dSAlan Maguire 	 * use KUnit, just know that this is how you associate test cases with a
81c475c77dSAlan Maguire 	 * test suite.
82c475c77dSAlan Maguire 	 */
83c475c77dSAlan Maguire 	KUNIT_CASE(example_simple_test),
84*d99ea675SDavid Gow 	KUNIT_CASE(example_skip_test),
85*d99ea675SDavid Gow 	KUNIT_CASE(example_mark_skipped_test),
86c475c77dSAlan Maguire 	{}
87c475c77dSAlan Maguire };
88c475c77dSAlan Maguire 
89c475c77dSAlan Maguire /*
90c475c77dSAlan Maguire  * This defines a suite or grouping of tests.
91c475c77dSAlan Maguire  *
92c475c77dSAlan Maguire  * Test cases are defined as belonging to the suite by adding them to
93c475c77dSAlan Maguire  * `kunit_cases`.
94c475c77dSAlan Maguire  *
95c475c77dSAlan Maguire  * Often it is desirable to run some function which will set up things which
96c475c77dSAlan Maguire  * will be used by every test; this is accomplished with an `init` function
97c475c77dSAlan Maguire  * which runs before each test case is invoked. Similarly, an `exit` function
98c475c77dSAlan Maguire  * may be specified which runs after every test case and can be used to for
99c475c77dSAlan Maguire  * cleanup. For clarity, running tests in a test suite would behave as follows:
100c475c77dSAlan Maguire  *
101c475c77dSAlan Maguire  * suite.init(test);
102c475c77dSAlan Maguire  * suite.test_case[0](test);
103c475c77dSAlan Maguire  * suite.exit(test);
104c475c77dSAlan Maguire  * suite.init(test);
105c475c77dSAlan Maguire  * suite.test_case[1](test);
106c475c77dSAlan Maguire  * suite.exit(test);
107c475c77dSAlan Maguire  * ...;
108c475c77dSAlan Maguire  */
109c475c77dSAlan Maguire static struct kunit_suite example_test_suite = {
110c475c77dSAlan Maguire 	.name = "example",
111c475c77dSAlan Maguire 	.init = example_test_init,
112c475c77dSAlan Maguire 	.test_cases = example_test_cases,
113c475c77dSAlan Maguire };
114c475c77dSAlan Maguire 
115c475c77dSAlan Maguire /*
116c475c77dSAlan Maguire  * This registers the above test suite telling KUnit that this is a suite of
117c475c77dSAlan Maguire  * tests that need to be run.
118c475c77dSAlan Maguire  */
119c475c77dSAlan Maguire kunit_test_suites(&example_test_suite);
120c475c77dSAlan Maguire 
121c475c77dSAlan Maguire MODULE_LICENSE("GPL v2");
122