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> 10e047c5eaSDavid Gow #include <kunit/static_stub.h> 11c475c77dSAlan Maguire 12c475c77dSAlan Maguire /* 13c475c77dSAlan Maguire * This is the most fundamental element of KUnit, the test case. A test case 14c475c77dSAlan Maguire * makes a set EXPECTATIONs and ASSERTIONs about the behavior of some code; if 15c475c77dSAlan Maguire * any expectations or assertions are not met, the test fails; otherwise, the 16c475c77dSAlan Maguire * test passes. 17c475c77dSAlan Maguire * 18c475c77dSAlan Maguire * In KUnit, a test case is just a function with the signature 19c475c77dSAlan Maguire * `void (*)(struct kunit *)`. `struct kunit` is a context object that stores 20c475c77dSAlan Maguire * information about the current test. 21c475c77dSAlan Maguire */ 22c475c77dSAlan Maguire static void example_simple_test(struct kunit *test) 23c475c77dSAlan Maguire { 24c475c77dSAlan Maguire /* 25c475c77dSAlan Maguire * This is an EXPECTATION; it is how KUnit tests things. When you want 26c475c77dSAlan Maguire * to test a piece of code, you set some expectations about what the 27c475c77dSAlan Maguire * code should do. KUnit then runs the test and verifies that the code's 28c475c77dSAlan Maguire * behavior matched what was expected. 29c475c77dSAlan Maguire */ 30c475c77dSAlan Maguire KUNIT_EXPECT_EQ(test, 1 + 1, 2); 31c475c77dSAlan Maguire } 32c475c77dSAlan Maguire 33c475c77dSAlan Maguire /* 34c475c77dSAlan Maguire * This is run once before each test case, see the comment on 35c475c77dSAlan Maguire * example_test_suite for more information. 36c475c77dSAlan Maguire */ 37c475c77dSAlan Maguire static int example_test_init(struct kunit *test) 38c475c77dSAlan Maguire { 39c475c77dSAlan Maguire kunit_info(test, "initializing\n"); 40c475c77dSAlan Maguire 41c475c77dSAlan Maguire return 0; 42c475c77dSAlan Maguire } 43c475c77dSAlan Maguire 44c475c77dSAlan Maguire /* 45*a5ce66adSDavid Gow * This is run once after each test case, see the comment on 46*a5ce66adSDavid Gow * example_test_suite for more information. 47*a5ce66adSDavid Gow */ 48*a5ce66adSDavid Gow static void example_test_exit(struct kunit *test) 49*a5ce66adSDavid Gow { 50*a5ce66adSDavid Gow kunit_info(test, "cleaning up\n"); 51*a5ce66adSDavid Gow } 52*a5ce66adSDavid Gow 53*a5ce66adSDavid Gow 54*a5ce66adSDavid Gow /* 551cdba21dSDaniel Latypov * This is run once before all test cases in the suite. 561cdba21dSDaniel Latypov * See the comment on example_test_suite for more information. 571cdba21dSDaniel Latypov */ 581cdba21dSDaniel Latypov static int example_test_init_suite(struct kunit_suite *suite) 591cdba21dSDaniel Latypov { 601cdba21dSDaniel Latypov kunit_info(suite, "initializing suite\n"); 611cdba21dSDaniel Latypov 621cdba21dSDaniel Latypov return 0; 631cdba21dSDaniel Latypov } 641cdba21dSDaniel Latypov 651cdba21dSDaniel Latypov /* 66*a5ce66adSDavid Gow * This is run once after all test cases in the suite. 67*a5ce66adSDavid Gow * See the comment on example_test_suite for more information. 68*a5ce66adSDavid Gow */ 69*a5ce66adSDavid Gow static void example_test_exit_suite(struct kunit_suite *suite) 70*a5ce66adSDavid Gow { 71*a5ce66adSDavid Gow kunit_info(suite, "exiting suite\n"); 72*a5ce66adSDavid Gow } 73*a5ce66adSDavid Gow 74*a5ce66adSDavid Gow 75*a5ce66adSDavid Gow /* 76d99ea675SDavid Gow * This test should always be skipped. 77d99ea675SDavid Gow */ 78d99ea675SDavid Gow static void example_skip_test(struct kunit *test) 79d99ea675SDavid Gow { 80d99ea675SDavid Gow /* This line should run */ 81d99ea675SDavid Gow kunit_info(test, "You should not see a line below."); 82d99ea675SDavid Gow 83d99ea675SDavid Gow /* Skip (and abort) the test */ 84d99ea675SDavid Gow kunit_skip(test, "this test should be skipped"); 85d99ea675SDavid Gow 86d99ea675SDavid Gow /* This line should not execute */ 87d99ea675SDavid Gow KUNIT_FAIL(test, "You should not see this line."); 88d99ea675SDavid Gow } 89d99ea675SDavid Gow 90d99ea675SDavid Gow /* 91d99ea675SDavid Gow * This test should always be marked skipped. 92d99ea675SDavid Gow */ 93d99ea675SDavid Gow static void example_mark_skipped_test(struct kunit *test) 94d99ea675SDavid Gow { 95d99ea675SDavid Gow /* This line should run */ 96d99ea675SDavid Gow kunit_info(test, "You should see a line below."); 97d99ea675SDavid Gow 98d99ea675SDavid Gow /* Skip (but do not abort) the test */ 99d99ea675SDavid Gow kunit_mark_skipped(test, "this test should be skipped"); 100d99ea675SDavid Gow 101d99ea675SDavid Gow /* This line should run */ 102d99ea675SDavid Gow kunit_info(test, "You should see this line."); 103d99ea675SDavid Gow } 1047b339105SDaniel Latypov 1057b339105SDaniel Latypov /* 1067b339105SDaniel Latypov * This test shows off all the types of KUNIT_EXPECT macros. 1077b339105SDaniel Latypov */ 1087b339105SDaniel Latypov static void example_all_expect_macros_test(struct kunit *test) 1097b339105SDaniel Latypov { 1103b30fb62SMaíra Canal const u32 array1[] = { 0x0F, 0xFF }; 1113b30fb62SMaíra Canal const u32 array2[] = { 0x1F, 0xFF }; 1123b30fb62SMaíra Canal 1137b339105SDaniel Latypov /* Boolean assertions */ 1147b339105SDaniel Latypov KUNIT_EXPECT_TRUE(test, true); 1157b339105SDaniel Latypov KUNIT_EXPECT_FALSE(test, false); 1167b339105SDaniel Latypov 1177b339105SDaniel Latypov /* Integer assertions */ 1187b339105SDaniel Latypov KUNIT_EXPECT_EQ(test, 1, 1); /* check == */ 1197b339105SDaniel Latypov KUNIT_EXPECT_GE(test, 1, 1); /* check >= */ 1207b339105SDaniel Latypov KUNIT_EXPECT_LE(test, 1, 1); /* check <= */ 1217b339105SDaniel Latypov KUNIT_EXPECT_NE(test, 1, 0); /* check != */ 1227b339105SDaniel Latypov KUNIT_EXPECT_GT(test, 1, 0); /* check > */ 1237b339105SDaniel Latypov KUNIT_EXPECT_LT(test, 0, 1); /* check < */ 1247b339105SDaniel Latypov 1257b339105SDaniel Latypov /* Pointer assertions */ 1267b339105SDaniel Latypov KUNIT_EXPECT_NOT_ERR_OR_NULL(test, test); 1277b339105SDaniel Latypov KUNIT_EXPECT_PTR_EQ(test, NULL, NULL); 1287b339105SDaniel Latypov KUNIT_EXPECT_PTR_NE(test, test, NULL); 129de82c15dSRicardo Ribalda KUNIT_EXPECT_NULL(test, NULL); 130de82c15dSRicardo Ribalda KUNIT_EXPECT_NOT_NULL(test, test); 1317b339105SDaniel Latypov 1327b339105SDaniel Latypov /* String assertions */ 1337b339105SDaniel Latypov KUNIT_EXPECT_STREQ(test, "hi", "hi"); 1347b339105SDaniel Latypov KUNIT_EXPECT_STRNEQ(test, "hi", "bye"); 1357b339105SDaniel Latypov 1363b30fb62SMaíra Canal /* Memory block assertions */ 1373b30fb62SMaíra Canal KUNIT_EXPECT_MEMEQ(test, array1, array1, sizeof(array1)); 1383b30fb62SMaíra Canal KUNIT_EXPECT_MEMNEQ(test, array1, array2, sizeof(array1)); 1393b30fb62SMaíra Canal 1407b339105SDaniel Latypov /* 1417b339105SDaniel Latypov * There are also ASSERT variants of all of the above that abort test 1427b339105SDaniel Latypov * execution if they fail. Useful for memory allocations, etc. 1437b339105SDaniel Latypov */ 1447b339105SDaniel Latypov KUNIT_ASSERT_GT(test, sizeof(char), 0); 1457b339105SDaniel Latypov 1467b339105SDaniel Latypov /* 1477b339105SDaniel Latypov * There are also _MSG variants of all of the above that let you include 1487b339105SDaniel Latypov * additional text on failure. 1497b339105SDaniel Latypov */ 1507b339105SDaniel Latypov KUNIT_EXPECT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!"); 1517b339105SDaniel Latypov KUNIT_ASSERT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!"); 1527b339105SDaniel Latypov } 1537b339105SDaniel Latypov 154e047c5eaSDavid Gow /* This is a function we'll replace with static stubs. */ 155e047c5eaSDavid Gow static int add_one(int i) 156e047c5eaSDavid Gow { 157e047c5eaSDavid Gow /* This will trigger the stub if active. */ 158e047c5eaSDavid Gow KUNIT_STATIC_STUB_REDIRECT(add_one, i); 159e047c5eaSDavid Gow 160e047c5eaSDavid Gow return i + 1; 161e047c5eaSDavid Gow } 162e047c5eaSDavid Gow 163e047c5eaSDavid Gow /* This is used as a replacement for the above function. */ 164e047c5eaSDavid Gow static int subtract_one(int i) 165e047c5eaSDavid Gow { 166e047c5eaSDavid Gow /* We don't need to trigger the stub from the replacement. */ 167e047c5eaSDavid Gow 168e047c5eaSDavid Gow return i - 1; 169e047c5eaSDavid Gow } 170e047c5eaSDavid Gow 171e047c5eaSDavid Gow /* 172e047c5eaSDavid Gow * This test shows the use of static stubs. 173e047c5eaSDavid Gow */ 174e047c5eaSDavid Gow static void example_static_stub_test(struct kunit *test) 175e047c5eaSDavid Gow { 176e047c5eaSDavid Gow /* By default, function is not stubbed. */ 177e047c5eaSDavid Gow KUNIT_EXPECT_EQ(test, add_one(1), 2); 178e047c5eaSDavid Gow 179e047c5eaSDavid Gow /* Replace add_one() with subtract_one(). */ 180e047c5eaSDavid Gow kunit_activate_static_stub(test, add_one, subtract_one); 181e047c5eaSDavid Gow 182e047c5eaSDavid Gow /* add_one() is now replaced. */ 183e047c5eaSDavid Gow KUNIT_EXPECT_EQ(test, add_one(1), 0); 184e047c5eaSDavid Gow 185e047c5eaSDavid Gow /* Return add_one() to normal. */ 186e047c5eaSDavid Gow kunit_deactivate_static_stub(test, add_one); 187e047c5eaSDavid Gow KUNIT_EXPECT_EQ(test, add_one(1), 2); 188e047c5eaSDavid Gow } 189e047c5eaSDavid Gow 190d99ea675SDavid Gow /* 191c475c77dSAlan Maguire * Here we make a list of all the test cases we want to add to the test suite 192c475c77dSAlan Maguire * below. 193c475c77dSAlan Maguire */ 194c475c77dSAlan Maguire static struct kunit_case example_test_cases[] = { 195c475c77dSAlan Maguire /* 196c475c77dSAlan Maguire * This is a helper to create a test case object from a test case 197c475c77dSAlan Maguire * function; its exact function is not important to understand how to 198c475c77dSAlan Maguire * use KUnit, just know that this is how you associate test cases with a 199c475c77dSAlan Maguire * test suite. 200c475c77dSAlan Maguire */ 201c475c77dSAlan Maguire KUNIT_CASE(example_simple_test), 202d99ea675SDavid Gow KUNIT_CASE(example_skip_test), 203d99ea675SDavid Gow KUNIT_CASE(example_mark_skipped_test), 2047b339105SDaniel Latypov KUNIT_CASE(example_all_expect_macros_test), 205e047c5eaSDavid Gow KUNIT_CASE(example_static_stub_test), 206c475c77dSAlan Maguire {} 207c475c77dSAlan Maguire }; 208c475c77dSAlan Maguire 209c475c77dSAlan Maguire /* 210c475c77dSAlan Maguire * This defines a suite or grouping of tests. 211c475c77dSAlan Maguire * 212c475c77dSAlan Maguire * Test cases are defined as belonging to the suite by adding them to 213c475c77dSAlan Maguire * `kunit_cases`. 214c475c77dSAlan Maguire * 215c475c77dSAlan Maguire * Often it is desirable to run some function which will set up things which 216c475c77dSAlan Maguire * will be used by every test; this is accomplished with an `init` function 217c475c77dSAlan Maguire * which runs before each test case is invoked. Similarly, an `exit` function 218c475c77dSAlan Maguire * may be specified which runs after every test case and can be used to for 219c475c77dSAlan Maguire * cleanup. For clarity, running tests in a test suite would behave as follows: 220c475c77dSAlan Maguire * 2211cdba21dSDaniel Latypov * suite.suite_init(suite); 222c475c77dSAlan Maguire * suite.init(test); 223c475c77dSAlan Maguire * suite.test_case[0](test); 224c475c77dSAlan Maguire * suite.exit(test); 225c475c77dSAlan Maguire * suite.init(test); 226c475c77dSAlan Maguire * suite.test_case[1](test); 227c475c77dSAlan Maguire * suite.exit(test); 2281cdba21dSDaniel Latypov * suite.suite_exit(suite); 229c475c77dSAlan Maguire * ...; 230c475c77dSAlan Maguire */ 231c475c77dSAlan Maguire static struct kunit_suite example_test_suite = { 232c475c77dSAlan Maguire .name = "example", 233c475c77dSAlan Maguire .init = example_test_init, 234*a5ce66adSDavid Gow .exit = example_test_exit, 2351cdba21dSDaniel Latypov .suite_init = example_test_init_suite, 236*a5ce66adSDavid Gow .suite_exit = example_test_exit_suite, 237c475c77dSAlan Maguire .test_cases = example_test_cases, 238c475c77dSAlan Maguire }; 239c475c77dSAlan Maguire 240c475c77dSAlan Maguire /* 241c475c77dSAlan Maguire * This registers the above test suite telling KUnit that this is a suite of 242c475c77dSAlan Maguire * tests that need to be run. 243c475c77dSAlan Maguire */ 244c475c77dSAlan Maguire kunit_test_suites(&example_test_suite); 245c475c77dSAlan Maguire 246c475c77dSAlan Maguire MODULE_LICENSE("GPL v2"); 247