xref: /openbmc/linux/lib/kunit/kunit-example-test.c (revision c475c77d5b56398303e726969e81208196b3aab3)
1*c475c77dSAlan Maguire // SPDX-License-Identifier: GPL-2.0
2*c475c77dSAlan Maguire /*
3*c475c77dSAlan Maguire  * Example KUnit test to show how to use KUnit.
4*c475c77dSAlan Maguire  *
5*c475c77dSAlan Maguire  * Copyright (C) 2019, Google LLC.
6*c475c77dSAlan Maguire  * Author: Brendan Higgins <brendanhiggins@google.com>
7*c475c77dSAlan Maguire  */
8*c475c77dSAlan Maguire 
9*c475c77dSAlan Maguire #include <kunit/test.h>
10*c475c77dSAlan Maguire 
11*c475c77dSAlan Maguire /*
12*c475c77dSAlan Maguire  * This is the most fundamental element of KUnit, the test case. A test case
13*c475c77dSAlan Maguire  * makes a set EXPECTATIONs and ASSERTIONs about the behavior of some code; if
14*c475c77dSAlan Maguire  * any expectations or assertions are not met, the test fails; otherwise, the
15*c475c77dSAlan Maguire  * test passes.
16*c475c77dSAlan Maguire  *
17*c475c77dSAlan Maguire  * In KUnit, a test case is just a function with the signature
18*c475c77dSAlan Maguire  * `void (*)(struct kunit *)`. `struct kunit` is a context object that stores
19*c475c77dSAlan Maguire  * information about the current test.
20*c475c77dSAlan Maguire  */
21*c475c77dSAlan Maguire static void example_simple_test(struct kunit *test)
22*c475c77dSAlan Maguire {
23*c475c77dSAlan Maguire 	/*
24*c475c77dSAlan Maguire 	 * This is an EXPECTATION; it is how KUnit tests things. When you want
25*c475c77dSAlan Maguire 	 * to test a piece of code, you set some expectations about what the
26*c475c77dSAlan Maguire 	 * code should do. KUnit then runs the test and verifies that the code's
27*c475c77dSAlan Maguire 	 * behavior matched what was expected.
28*c475c77dSAlan Maguire 	 */
29*c475c77dSAlan Maguire 	KUNIT_EXPECT_EQ(test, 1 + 1, 2);
30*c475c77dSAlan Maguire }
31*c475c77dSAlan Maguire 
32*c475c77dSAlan Maguire /*
33*c475c77dSAlan Maguire  * This is run once before each test case, see the comment on
34*c475c77dSAlan Maguire  * example_test_suite for more information.
35*c475c77dSAlan Maguire  */
36*c475c77dSAlan Maguire static int example_test_init(struct kunit *test)
37*c475c77dSAlan Maguire {
38*c475c77dSAlan Maguire 	kunit_info(test, "initializing\n");
39*c475c77dSAlan Maguire 
40*c475c77dSAlan Maguire 	return 0;
41*c475c77dSAlan Maguire }
42*c475c77dSAlan Maguire 
43*c475c77dSAlan Maguire /*
44*c475c77dSAlan Maguire  * Here we make a list of all the test cases we want to add to the test suite
45*c475c77dSAlan Maguire  * below.
46*c475c77dSAlan Maguire  */
47*c475c77dSAlan Maguire static struct kunit_case example_test_cases[] = {
48*c475c77dSAlan Maguire 	/*
49*c475c77dSAlan Maguire 	 * This is a helper to create a test case object from a test case
50*c475c77dSAlan Maguire 	 * function; its exact function is not important to understand how to
51*c475c77dSAlan Maguire 	 * use KUnit, just know that this is how you associate test cases with a
52*c475c77dSAlan Maguire 	 * test suite.
53*c475c77dSAlan Maguire 	 */
54*c475c77dSAlan Maguire 	KUNIT_CASE(example_simple_test),
55*c475c77dSAlan Maguire 	{}
56*c475c77dSAlan Maguire };
57*c475c77dSAlan Maguire 
58*c475c77dSAlan Maguire /*
59*c475c77dSAlan Maguire  * This defines a suite or grouping of tests.
60*c475c77dSAlan Maguire  *
61*c475c77dSAlan Maguire  * Test cases are defined as belonging to the suite by adding them to
62*c475c77dSAlan Maguire  * `kunit_cases`.
63*c475c77dSAlan Maguire  *
64*c475c77dSAlan Maguire  * Often it is desirable to run some function which will set up things which
65*c475c77dSAlan Maguire  * will be used by every test; this is accomplished with an `init` function
66*c475c77dSAlan Maguire  * which runs before each test case is invoked. Similarly, an `exit` function
67*c475c77dSAlan Maguire  * may be specified which runs after every test case and can be used to for
68*c475c77dSAlan Maguire  * cleanup. For clarity, running tests in a test suite would behave as follows:
69*c475c77dSAlan Maguire  *
70*c475c77dSAlan Maguire  * suite.init(test);
71*c475c77dSAlan Maguire  * suite.test_case[0](test);
72*c475c77dSAlan Maguire  * suite.exit(test);
73*c475c77dSAlan Maguire  * suite.init(test);
74*c475c77dSAlan Maguire  * suite.test_case[1](test);
75*c475c77dSAlan Maguire  * suite.exit(test);
76*c475c77dSAlan Maguire  * ...;
77*c475c77dSAlan Maguire  */
78*c475c77dSAlan Maguire static struct kunit_suite example_test_suite = {
79*c475c77dSAlan Maguire 	.name = "example",
80*c475c77dSAlan Maguire 	.init = example_test_init,
81*c475c77dSAlan Maguire 	.test_cases = example_test_cases,
82*c475c77dSAlan Maguire };
83*c475c77dSAlan Maguire 
84*c475c77dSAlan Maguire /*
85*c475c77dSAlan Maguire  * This registers the above test suite telling KUnit that this is a suite of
86*c475c77dSAlan Maguire  * tests that need to be run.
87*c475c77dSAlan Maguire  */
88*c475c77dSAlan Maguire kunit_test_suites(&example_test_suite);
89*c475c77dSAlan Maguire 
90*c475c77dSAlan Maguire MODULE_LICENSE("GPL v2");
91