index.rst 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. running_tips
  17. This section details the kernel unit testing framework.
  18. Introduction
  19. ============
  20. KUnit (Kernel unit testing framework) provides a common framework for
  21. unit tests within the Linux kernel. Using KUnit, you can define groups
  22. of test cases called test suites. The tests either run on kernel boot
  23. if built-in, or load as a module. KUnit automatically flags and reports
  24. failed test cases in the kernel log. The test results appear in
  25. :doc:`KTAP (Kernel - Test Anything Protocol) format</dev-tools/ktap>`.
  26. It is inspired by JUnit, Python’s unittest.mock, and GoogleTest/GoogleMock
  27. (C++ unit testing framework).
  28. KUnit tests are part of the kernel, written in the C (programming)
  29. language, and test parts of the Kernel implementation (example: a C
  30. language function). Excluding build time, from invocation to
  31. completion, KUnit can run around 100 tests in less than 10 seconds.
  32. KUnit can test any kernel component, for example: file system, system
  33. calls, memory management, device drivers and so on.
  34. KUnit follows the white-box testing approach. The test has access to
  35. internal system functionality. KUnit runs in kernel space and is not
  36. restricted to things exposed to user-space.
  37. In addition, KUnit has kunit_tool, a script (``tools/testing/kunit/kunit.py``)
  38. that configures the Linux kernel, runs KUnit tests under QEMU or UML
  39. (:doc:`User Mode Linux </virt/uml/user_mode_linux_howto_v2>`),
  40. parses the test results and
  41. displays them in a user friendly manner.
  42. Features
  43. --------
  44. - Provides a framework for writing unit tests.
  45. - Runs tests on any kernel architecture.
  46. - Runs a test in milliseconds.
  47. Prerequisites
  48. -------------
  49. - Any Linux kernel compatible hardware.
  50. - For Kernel under test, Linux kernel version 5.5 or greater.
  51. Unit Testing
  52. ============
  53. A unit test tests a single unit of code in isolation. A unit test is the finest
  54. granularity of testing and allows all possible code paths to be tested in the
  55. code under test. This is possible if the code under test is small and does not
  56. have any external dependencies outside of the test's control like hardware.
  57. Write Unit Tests
  58. ----------------
  59. To write good unit tests, there is a simple but powerful pattern:
  60. Arrange-Act-Assert. This is a great way to structure test cases and
  61. defines an order of operations.
  62. - Arrange inputs and targets: At the start of the test, arrange the data
  63. that allows a function to work. Example: initialize a statement or
  64. object.
  65. - Act on the target behavior: Call your function/code under test.
  66. - Assert expected outcome: Verify that the result (or resulting state) is as
  67. expected.
  68. Unit Testing Advantages
  69. -----------------------
  70. - Increases testing speed and development in the long run.
  71. - Detects bugs at initial stage and therefore decreases bug fix cost
  72. compared to acceptance testing.
  73. - Improves code quality.
  74. - Encourages writing testable code.
  75. Read also :ref:`kinds-of-tests`.
  76. How do I use it?
  77. ================
  78. You can find a step-by-step guide to writing and running KUnit tests in
  79. Documentation/dev-tools/kunit/start.rst
  80. Alternatively, feel free to look through the rest of the KUnit documentation,
  81. or to experiment with tools/testing/kunit/kunit.py and the example test under
  82. lib/kunit/kunit-example-test.c
  83. Happy testing!