Linux Audio

Check our new training course

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