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