kunit-example-test.c 2.6 KB

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