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*1cdba21dSDaniel Latypov * This is run once before all test cases in the suite. 45*1cdba21dSDaniel Latypov * See the comment on example_test_suite for more information. 46*1cdba21dSDaniel Latypov */ 47*1cdba21dSDaniel Latypov static int example_test_init_suite(struct kunit_suite *suite) 48*1cdba21dSDaniel Latypov { 49*1cdba21dSDaniel Latypov kunit_info(suite, "initializing suite\n"); 50*1cdba21dSDaniel Latypov 51*1cdba21dSDaniel Latypov return 0; 52*1cdba21dSDaniel Latypov } 53*1cdba21dSDaniel Latypov 54*1cdba21dSDaniel Latypov /* 55d99ea675SDavid Gow * This test should always be skipped. 56d99ea675SDavid Gow */ 57d99ea675SDavid Gow static void example_skip_test(struct kunit *test) 58d99ea675SDavid Gow { 59d99ea675SDavid Gow /* This line should run */ 60d99ea675SDavid Gow kunit_info(test, "You should not see a line below."); 61d99ea675SDavid Gow 62d99ea675SDavid Gow /* Skip (and abort) the test */ 63d99ea675SDavid Gow kunit_skip(test, "this test should be skipped"); 64d99ea675SDavid Gow 65d99ea675SDavid Gow /* This line should not execute */ 66d99ea675SDavid Gow KUNIT_FAIL(test, "You should not see this line."); 67d99ea675SDavid Gow } 68d99ea675SDavid Gow 69d99ea675SDavid Gow /* 70d99ea675SDavid Gow * This test should always be marked skipped. 71d99ea675SDavid Gow */ 72d99ea675SDavid Gow static void example_mark_skipped_test(struct kunit *test) 73d99ea675SDavid Gow { 74d99ea675SDavid Gow /* This line should run */ 75d99ea675SDavid Gow kunit_info(test, "You should see a line below."); 76d99ea675SDavid Gow 77d99ea675SDavid Gow /* Skip (but do not abort) the test */ 78d99ea675SDavid Gow kunit_mark_skipped(test, "this test should be skipped"); 79d99ea675SDavid Gow 80d99ea675SDavid Gow /* This line should run */ 81d99ea675SDavid Gow kunit_info(test, "You should see this line."); 82d99ea675SDavid Gow } 837b339105SDaniel Latypov 847b339105SDaniel Latypov /* 857b339105SDaniel Latypov * This test shows off all the types of KUNIT_EXPECT macros. 867b339105SDaniel Latypov */ 877b339105SDaniel Latypov static void example_all_expect_macros_test(struct kunit *test) 887b339105SDaniel Latypov { 897b339105SDaniel Latypov /* Boolean assertions */ 907b339105SDaniel Latypov KUNIT_EXPECT_TRUE(test, true); 917b339105SDaniel Latypov KUNIT_EXPECT_FALSE(test, false); 927b339105SDaniel Latypov 937b339105SDaniel Latypov /* Integer assertions */ 947b339105SDaniel Latypov KUNIT_EXPECT_EQ(test, 1, 1); /* check == */ 957b339105SDaniel Latypov KUNIT_EXPECT_GE(test, 1, 1); /* check >= */ 967b339105SDaniel Latypov KUNIT_EXPECT_LE(test, 1, 1); /* check <= */ 977b339105SDaniel Latypov KUNIT_EXPECT_NE(test, 1, 0); /* check != */ 987b339105SDaniel Latypov KUNIT_EXPECT_GT(test, 1, 0); /* check > */ 997b339105SDaniel Latypov KUNIT_EXPECT_LT(test, 0, 1); /* check < */ 1007b339105SDaniel Latypov 1017b339105SDaniel Latypov /* Pointer assertions */ 1027b339105SDaniel Latypov KUNIT_EXPECT_NOT_ERR_OR_NULL(test, test); 1037b339105SDaniel Latypov KUNIT_EXPECT_PTR_EQ(test, NULL, NULL); 1047b339105SDaniel Latypov KUNIT_EXPECT_PTR_NE(test, test, NULL); 105de82c15dSRicardo Ribalda KUNIT_EXPECT_NULL(test, NULL); 106de82c15dSRicardo Ribalda KUNIT_EXPECT_NOT_NULL(test, test); 1077b339105SDaniel Latypov 1087b339105SDaniel Latypov /* String assertions */ 1097b339105SDaniel Latypov KUNIT_EXPECT_STREQ(test, "hi", "hi"); 1107b339105SDaniel Latypov KUNIT_EXPECT_STRNEQ(test, "hi", "bye"); 1117b339105SDaniel Latypov 1127b339105SDaniel Latypov /* 1137b339105SDaniel Latypov * There are also ASSERT variants of all of the above that abort test 1147b339105SDaniel Latypov * execution if they fail. Useful for memory allocations, etc. 1157b339105SDaniel Latypov */ 1167b339105SDaniel Latypov KUNIT_ASSERT_GT(test, sizeof(char), 0); 1177b339105SDaniel Latypov 1187b339105SDaniel Latypov /* 1197b339105SDaniel Latypov * There are also _MSG variants of all of the above that let you include 1207b339105SDaniel Latypov * additional text on failure. 1217b339105SDaniel Latypov */ 1227b339105SDaniel Latypov KUNIT_EXPECT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!"); 1237b339105SDaniel Latypov KUNIT_ASSERT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!"); 1247b339105SDaniel Latypov } 1257b339105SDaniel Latypov 126d99ea675SDavid Gow /* 127c475c77dSAlan Maguire * Here we make a list of all the test cases we want to add to the test suite 128c475c77dSAlan Maguire * below. 129c475c77dSAlan Maguire */ 130c475c77dSAlan Maguire static struct kunit_case example_test_cases[] = { 131c475c77dSAlan Maguire /* 132c475c77dSAlan Maguire * This is a helper to create a test case object from a test case 133c475c77dSAlan Maguire * function; its exact function is not important to understand how to 134c475c77dSAlan Maguire * use KUnit, just know that this is how you associate test cases with a 135c475c77dSAlan Maguire * test suite. 136c475c77dSAlan Maguire */ 137c475c77dSAlan Maguire KUNIT_CASE(example_simple_test), 138d99ea675SDavid Gow KUNIT_CASE(example_skip_test), 139d99ea675SDavid Gow KUNIT_CASE(example_mark_skipped_test), 1407b339105SDaniel Latypov KUNIT_CASE(example_all_expect_macros_test), 141c475c77dSAlan Maguire {} 142c475c77dSAlan Maguire }; 143c475c77dSAlan Maguire 144c475c77dSAlan Maguire /* 145c475c77dSAlan Maguire * This defines a suite or grouping of tests. 146c475c77dSAlan Maguire * 147c475c77dSAlan Maguire * Test cases are defined as belonging to the suite by adding them to 148c475c77dSAlan Maguire * `kunit_cases`. 149c475c77dSAlan Maguire * 150c475c77dSAlan Maguire * Often it is desirable to run some function which will set up things which 151c475c77dSAlan Maguire * will be used by every test; this is accomplished with an `init` function 152c475c77dSAlan Maguire * which runs before each test case is invoked. Similarly, an `exit` function 153c475c77dSAlan Maguire * may be specified which runs after every test case and can be used to for 154c475c77dSAlan Maguire * cleanup. For clarity, running tests in a test suite would behave as follows: 155c475c77dSAlan Maguire * 156*1cdba21dSDaniel Latypov * suite.suite_init(suite); 157c475c77dSAlan Maguire * suite.init(test); 158c475c77dSAlan Maguire * suite.test_case[0](test); 159c475c77dSAlan Maguire * suite.exit(test); 160c475c77dSAlan Maguire * suite.init(test); 161c475c77dSAlan Maguire * suite.test_case[1](test); 162c475c77dSAlan Maguire * suite.exit(test); 163*1cdba21dSDaniel Latypov * suite.suite_exit(suite); 164c475c77dSAlan Maguire * ...; 165c475c77dSAlan Maguire */ 166c475c77dSAlan Maguire static struct kunit_suite example_test_suite = { 167c475c77dSAlan Maguire .name = "example", 168c475c77dSAlan Maguire .init = example_test_init, 169*1cdba21dSDaniel Latypov .suite_init = example_test_init_suite, 170c475c77dSAlan Maguire .test_cases = example_test_cases, 171c475c77dSAlan Maguire }; 172c475c77dSAlan Maguire 173c475c77dSAlan Maguire /* 174c475c77dSAlan Maguire * This registers the above test suite telling KUnit that this is a suite of 175c475c77dSAlan Maguire * tests that need to be run. 176c475c77dSAlan Maguire */ 177c475c77dSAlan Maguire kunit_test_suites(&example_test_suite); 178c475c77dSAlan Maguire 179c475c77dSAlan Maguire MODULE_LICENSE("GPL v2"); 180