index.rst 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =================================
  3. KUnit - Linux Kernel Unit Testing
  4. =================================
  5. .. toctree::
  6. :maxdepth: 2
  7. :caption: Contents:
  8. start
  9. architecture
  10. run_wrapper
  11. run_manual
  12. usage
  13. api/index
  14. style
  15. faq
  16. tips
  17. running_tips
  18. .. warning::
  19. AOSP only supports running tests loaded with modules. Built-in
  20. test execution support has been disabled. In addition, in order
  21. to fully enable running module loaded tests both CONFIG_KUNIT
  22. needs to be enabled and kernel command line argument
  23. `kunit.enable` needs to be set to 1.
  24. The remaining KUnit documentation has been left as-is.
  25. This section details the kernel unit testing framework.
  26. Introduction
  27. ============
  28. KUnit (Kernel unit testing framework) provides a common framework for
  29. unit tests within the Linux kernel. Using KUnit, you can define groups
  30. of test cases called test suites. The tests either run on kernel boot
  31. if built-in, or load as a module. KUnit automatically flags and reports
  32. failed test cases in the kernel log. The test results appear in
  33. :doc:`KTAP (Kernel - Test Anything Protocol) format</dev-tools/ktap>`.
  34. It is inspired by JUnit, Python’s unittest.mock, and GoogleTest/GoogleMock
  35. (C++ unit testing framework).
  36. KUnit tests are part of the kernel, written in the C (programming)
  37. language, and test parts of the Kernel implementation (example: a C
  38. language function). Excluding build time, from invocation to
  39. completion, KUnit can run around 100 tests in less than 10 seconds.
  40. KUnit can test any kernel component, for example: file system, system
  41. calls, memory management, device drivers and so on.
  42. KUnit follows the white-box testing approach. The test has access to
  43. internal system functionality. KUnit runs in kernel space and is not
  44. restricted to things exposed to user-space.
  45. In addition, KUnit has kunit_tool, a script (``tools/testing/kunit/kunit.py``)
  46. that configures the Linux kernel, runs KUnit tests under QEMU or UML
  47. (:doc:`User Mode Linux </virt/uml/user_mode_linux_howto_v2>`),
  48. parses the test results and
  49. displays them in a user friendly manner.
  50. Features
  51. --------
  52. - Provides a framework for writing unit tests.
  53. - Runs tests on any kernel architecture.
  54. - Runs a test in milliseconds.
  55. Prerequisites
  56. -------------
  57. - Any Linux kernel compatible hardware.
  58. - For Kernel under test, Linux kernel version 5.5 or greater.
  59. Unit Testing
  60. ============
  61. A unit test tests a single unit of code in isolation. A unit test is the finest
  62. granularity of testing and allows all possible code paths to be tested in the
  63. code under test. This is possible if the code under test is small and does not
  64. have any external dependencies outside of the test's control like hardware.
  65. Write Unit Tests
  66. ----------------
  67. To write good unit tests, there is a simple but powerful pattern:
  68. Arrange-Act-Assert. This is a great way to structure test cases and
  69. defines an order of operations.
  70. - Arrange inputs and targets: At the start of the test, arrange the data
  71. that allows a function to work. Example: initialize a statement or
  72. object.
  73. - Act on the target behavior: Call your function/code under test.
  74. - Assert expected outcome: Verify that the result (or resulting state) is as
  75. expected.
  76. Unit Testing Advantages
  77. -----------------------
  78. - Increases testing speed and development in the long run.
  79. - Detects bugs at initial stage and therefore decreases bug fix cost
  80. compared to acceptance testing.
  81. - Improves code quality.
  82. - Encourages writing testable code.
  83. Read also :ref:`kinds-of-tests`.
  84. How do I use it?
  85. ================
  86. * Documentation/dev-tools/kunit/start.rst - for KUnit new users.
  87. * Documentation/dev-tools/kunit/architecture.rst - KUnit architecture.
  88. * Documentation/dev-tools/kunit/run_wrapper.rst - run kunit_tool.
  89. * Documentation/dev-tools/kunit/run_manual.rst - run tests without kunit_tool.
  90. * Documentation/dev-tools/kunit/usage.rst - write tests.
  91. * Documentation/dev-tools/kunit/tips.rst - best practices with
  92. examples.
  93. * Documentation/dev-tools/kunit/api/index.rst - KUnit APIs
  94. used for testing.
  95. * Documentation/dev-tools/kunit/faq.rst - KUnit common questions and
  96. answers.