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 /* 44d99ea675SDavid Gow * This test should always be skipped. 45d99ea675SDavid Gow */ 46d99ea675SDavid Gow static void example_skip_test(struct kunit *test) 47d99ea675SDavid Gow { 48d99ea675SDavid Gow /* This line should run */ 49d99ea675SDavid Gow kunit_info(test, "You should not see a line below."); 50d99ea675SDavid Gow 51d99ea675SDavid Gow /* Skip (and abort) the test */ 52d99ea675SDavid Gow kunit_skip(test, "this test should be skipped"); 53d99ea675SDavid Gow 54d99ea675SDavid Gow /* This line should not execute */ 55d99ea675SDavid Gow KUNIT_FAIL(test, "You should not see this line."); 56d99ea675SDavid Gow } 57d99ea675SDavid Gow 58d99ea675SDavid Gow /* 59d99ea675SDavid Gow * This test should always be marked skipped. 60d99ea675SDavid Gow */ 61d99ea675SDavid Gow static void example_mark_skipped_test(struct kunit *test) 62d99ea675SDavid Gow { 63d99ea675SDavid Gow /* This line should run */ 64d99ea675SDavid Gow kunit_info(test, "You should see a line below."); 65d99ea675SDavid Gow 66d99ea675SDavid Gow /* Skip (but do not abort) the test */ 67d99ea675SDavid Gow kunit_mark_skipped(test, "this test should be skipped"); 68d99ea675SDavid Gow 69d99ea675SDavid Gow /* This line should run */ 70d99ea675SDavid Gow kunit_info(test, "You should see this line."); 71d99ea675SDavid Gow } 727b339105SDaniel Latypov 737b339105SDaniel Latypov /* 747b339105SDaniel Latypov * This test shows off all the types of KUNIT_EXPECT macros. 757b339105SDaniel Latypov */ 767b339105SDaniel Latypov static void example_all_expect_macros_test(struct kunit *test) 777b339105SDaniel Latypov { 787b339105SDaniel Latypov /* Boolean assertions */ 797b339105SDaniel Latypov KUNIT_EXPECT_TRUE(test, true); 807b339105SDaniel Latypov KUNIT_EXPECT_FALSE(test, false); 817b339105SDaniel Latypov 827b339105SDaniel Latypov /* Integer assertions */ 837b339105SDaniel Latypov KUNIT_EXPECT_EQ(test, 1, 1); /* check == */ 847b339105SDaniel Latypov KUNIT_EXPECT_GE(test, 1, 1); /* check >= */ 857b339105SDaniel Latypov KUNIT_EXPECT_LE(test, 1, 1); /* check <= */ 867b339105SDaniel Latypov KUNIT_EXPECT_NE(test, 1, 0); /* check != */ 877b339105SDaniel Latypov KUNIT_EXPECT_GT(test, 1, 0); /* check > */ 887b339105SDaniel Latypov KUNIT_EXPECT_LT(test, 0, 1); /* check < */ 897b339105SDaniel Latypov 907b339105SDaniel Latypov /* Pointer assertions */ 917b339105SDaniel Latypov KUNIT_EXPECT_NOT_ERR_OR_NULL(test, test); 927b339105SDaniel Latypov KUNIT_EXPECT_PTR_EQ(test, NULL, NULL); 937b339105SDaniel Latypov KUNIT_EXPECT_PTR_NE(test, test, NULL); 94*de82c15dSRicardo Ribalda KUNIT_EXPECT_NULL(test, NULL); 95*de82c15dSRicardo Ribalda KUNIT_EXPECT_NOT_NULL(test, test); 967b339105SDaniel Latypov 977b339105SDaniel Latypov /* String assertions */ 987b339105SDaniel Latypov KUNIT_EXPECT_STREQ(test, "hi", "hi"); 997b339105SDaniel Latypov KUNIT_EXPECT_STRNEQ(test, "hi", "bye"); 1007b339105SDaniel Latypov 1017b339105SDaniel Latypov /* 1027b339105SDaniel Latypov * There are also ASSERT variants of all of the above that abort test 1037b339105SDaniel Latypov * execution if they fail. Useful for memory allocations, etc. 1047b339105SDaniel Latypov */ 1057b339105SDaniel Latypov KUNIT_ASSERT_GT(test, sizeof(char), 0); 1067b339105SDaniel Latypov 1077b339105SDaniel Latypov /* 1087b339105SDaniel Latypov * There are also _MSG variants of all of the above that let you include 1097b339105SDaniel Latypov * additional text on failure. 1107b339105SDaniel Latypov */ 1117b339105SDaniel Latypov KUNIT_EXPECT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!"); 1127b339105SDaniel Latypov KUNIT_ASSERT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!"); 1137b339105SDaniel Latypov } 1147b339105SDaniel Latypov 115d99ea675SDavid Gow /* 116c475c77dSAlan Maguire * Here we make a list of all the test cases we want to add to the test suite 117c475c77dSAlan Maguire * below. 118c475c77dSAlan Maguire */ 119c475c77dSAlan Maguire static struct kunit_case example_test_cases[] = { 120c475c77dSAlan Maguire /* 121c475c77dSAlan Maguire * This is a helper to create a test case object from a test case 122c475c77dSAlan Maguire * function; its exact function is not important to understand how to 123c475c77dSAlan Maguire * use KUnit, just know that this is how you associate test cases with a 124c475c77dSAlan Maguire * test suite. 125c475c77dSAlan Maguire */ 126c475c77dSAlan Maguire KUNIT_CASE(example_simple_test), 127d99ea675SDavid Gow KUNIT_CASE(example_skip_test), 128d99ea675SDavid Gow KUNIT_CASE(example_mark_skipped_test), 1297b339105SDaniel Latypov KUNIT_CASE(example_all_expect_macros_test), 130c475c77dSAlan Maguire {} 131c475c77dSAlan Maguire }; 132c475c77dSAlan Maguire 133c475c77dSAlan Maguire /* 134c475c77dSAlan Maguire * This defines a suite or grouping of tests. 135c475c77dSAlan Maguire * 136c475c77dSAlan Maguire * Test cases are defined as belonging to the suite by adding them to 137c475c77dSAlan Maguire * `kunit_cases`. 138c475c77dSAlan Maguire * 139c475c77dSAlan Maguire * Often it is desirable to run some function which will set up things which 140c475c77dSAlan Maguire * will be used by every test; this is accomplished with an `init` function 141c475c77dSAlan Maguire * which runs before each test case is invoked. Similarly, an `exit` function 142c475c77dSAlan Maguire * may be specified which runs after every test case and can be used to for 143c475c77dSAlan Maguire * cleanup. For clarity, running tests in a test suite would behave as follows: 144c475c77dSAlan Maguire * 145c475c77dSAlan Maguire * suite.init(test); 146c475c77dSAlan Maguire * suite.test_case[0](test); 147c475c77dSAlan Maguire * suite.exit(test); 148c475c77dSAlan Maguire * suite.init(test); 149c475c77dSAlan Maguire * suite.test_case[1](test); 150c475c77dSAlan Maguire * suite.exit(test); 151c475c77dSAlan Maguire * ...; 152c475c77dSAlan Maguire */ 153c475c77dSAlan Maguire static struct kunit_suite example_test_suite = { 154c475c77dSAlan Maguire .name = "example", 155c475c77dSAlan Maguire .init = example_test_init, 156c475c77dSAlan Maguire .test_cases = example_test_cases, 157c475c77dSAlan Maguire }; 158c475c77dSAlan Maguire 159c475c77dSAlan Maguire /* 160c475c77dSAlan Maguire * This registers the above test suite telling KUnit that this is a suite of 161c475c77dSAlan Maguire * tests that need to be run. 162c475c77dSAlan Maguire */ 163c475c77dSAlan Maguire kunit_test_suites(&example_test_suite); 164c475c77dSAlan Maguire 165c475c77dSAlan Maguire MODULE_LICENSE("GPL v2"); 166