18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Example KUnit test to show how to use KUnit. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2019, Google LLC. 68c2ecf20Sopenharmony_ci * Author: Brendan Higgins <brendanhiggins@google.com> 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <kunit/test.h> 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci/* 128c2ecf20Sopenharmony_ci * This is the most fundamental element of KUnit, the test case. A test case 138c2ecf20Sopenharmony_ci * makes a set EXPECTATIONs and ASSERTIONs about the behavior of some code; if 148c2ecf20Sopenharmony_ci * any expectations or assertions are not met, the test fails; otherwise, the 158c2ecf20Sopenharmony_ci * test passes. 168c2ecf20Sopenharmony_ci * 178c2ecf20Sopenharmony_ci * In KUnit, a test case is just a function with the signature 188c2ecf20Sopenharmony_ci * `void (*)(struct kunit *)`. `struct kunit` is a context object that stores 198c2ecf20Sopenharmony_ci * information about the current test. 208c2ecf20Sopenharmony_ci */ 218c2ecf20Sopenharmony_cistatic void example_simple_test(struct kunit *test) 228c2ecf20Sopenharmony_ci{ 238c2ecf20Sopenharmony_ci /* 248c2ecf20Sopenharmony_ci * This is an EXPECTATION; it is how KUnit tests things. When you want 258c2ecf20Sopenharmony_ci * to test a piece of code, you set some expectations about what the 268c2ecf20Sopenharmony_ci * code should do. KUnit then runs the test and verifies that the code's 278c2ecf20Sopenharmony_ci * behavior matched what was expected. 288c2ecf20Sopenharmony_ci */ 298c2ecf20Sopenharmony_ci KUNIT_EXPECT_EQ(test, 1 + 1, 2); 308c2ecf20Sopenharmony_ci} 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci/* 338c2ecf20Sopenharmony_ci * This is run once before each test case, see the comment on 348c2ecf20Sopenharmony_ci * example_test_suite for more information. 358c2ecf20Sopenharmony_ci */ 368c2ecf20Sopenharmony_cistatic int example_test_init(struct kunit *test) 378c2ecf20Sopenharmony_ci{ 388c2ecf20Sopenharmony_ci kunit_info(test, "initializing\n"); 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci return 0; 418c2ecf20Sopenharmony_ci} 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci/* 448c2ecf20Sopenharmony_ci * Here we make a list of all the test cases we want to add to the test suite 458c2ecf20Sopenharmony_ci * below. 468c2ecf20Sopenharmony_ci */ 478c2ecf20Sopenharmony_cistatic struct kunit_case example_test_cases[] = { 488c2ecf20Sopenharmony_ci /* 498c2ecf20Sopenharmony_ci * This is a helper to create a test case object from a test case 508c2ecf20Sopenharmony_ci * function; its exact function is not important to understand how to 518c2ecf20Sopenharmony_ci * use KUnit, just know that this is how you associate test cases with a 528c2ecf20Sopenharmony_ci * test suite. 538c2ecf20Sopenharmony_ci */ 548c2ecf20Sopenharmony_ci KUNIT_CASE(example_simple_test), 558c2ecf20Sopenharmony_ci {} 568c2ecf20Sopenharmony_ci}; 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci/* 598c2ecf20Sopenharmony_ci * This defines a suite or grouping of tests. 608c2ecf20Sopenharmony_ci * 618c2ecf20Sopenharmony_ci * Test cases are defined as belonging to the suite by adding them to 628c2ecf20Sopenharmony_ci * `kunit_cases`. 638c2ecf20Sopenharmony_ci * 648c2ecf20Sopenharmony_ci * Often it is desirable to run some function which will set up things which 658c2ecf20Sopenharmony_ci * will be used by every test; this is accomplished with an `init` function 668c2ecf20Sopenharmony_ci * which runs before each test case is invoked. Similarly, an `exit` function 678c2ecf20Sopenharmony_ci * may be specified which runs after every test case and can be used to for 688c2ecf20Sopenharmony_ci * cleanup. For clarity, running tests in a test suite would behave as follows: 698c2ecf20Sopenharmony_ci * 708c2ecf20Sopenharmony_ci * suite.init(test); 718c2ecf20Sopenharmony_ci * suite.test_case[0](test); 728c2ecf20Sopenharmony_ci * suite.exit(test); 738c2ecf20Sopenharmony_ci * suite.init(test); 748c2ecf20Sopenharmony_ci * suite.test_case[1](test); 758c2ecf20Sopenharmony_ci * suite.exit(test); 768c2ecf20Sopenharmony_ci * ...; 778c2ecf20Sopenharmony_ci */ 788c2ecf20Sopenharmony_cistatic struct kunit_suite example_test_suite = { 798c2ecf20Sopenharmony_ci .name = "example", 808c2ecf20Sopenharmony_ci .init = example_test_init, 818c2ecf20Sopenharmony_ci .test_cases = example_test_cases, 828c2ecf20Sopenharmony_ci}; 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci/* 858c2ecf20Sopenharmony_ci * This registers the above test suite telling KUnit that this is a suite of 868c2ecf20Sopenharmony_ci * tests that need to be run. 878c2ecf20Sopenharmony_ci */ 888c2ecf20Sopenharmony_cikunit_test_suites(&example_test_suite); 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 91