style.rst 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ===========================
  3. Test Style and Nomenclature
  4. ===========================
  5. To make finding, writing, and using KUnit tests as simple as possible, it is
  6. strongly encouraged that they are named and written according to the guidelines
  7. below. While it is possible to write KUnit tests which do not follow these rules,
  8. they may break some tooling, may conflict with other tests, and may not be run
  9. automatically by testing systems.
  10. It is recommended that you only deviate from these guidelines when:
  11. 1. Porting tests to KUnit which are already known with an existing name.
  12. 2. Writing tests which would cause serious problems if automatically run. For
  13. example, non-deterministically producing false positives or negatives, or
  14. taking a long time to run.
  15. Subsystems, Suites, and Tests
  16. =============================
  17. To make tests easy to find, they are grouped into suites and subsystems. A test
  18. suite is a group of tests which test a related area of the kernel. A subsystem
  19. is a set of test suites which test different parts of a kernel subsystem
  20. or a driver.
  21. Subsystems
  22. ----------
  23. Every test suite must belong to a subsystem. A subsystem is a collection of one
  24. or more KUnit test suites which test the same driver or part of the kernel. A
  25. test subsystem should match a single kernel module. If the code being tested
  26. cannot be compiled as a module, in many cases the subsystem should correspond to
  27. a directory in the source tree or an entry in the ``MAINTAINERS`` file. If
  28. unsure, follow the conventions set by tests in similar areas.
  29. Test subsystems should be named after the code being tested, either after the
  30. module (wherever possible), or after the directory or files being tested. Test
  31. subsystems should be named to avoid ambiguity where necessary.
  32. If a test subsystem name has multiple components, they should be separated by
  33. underscores. *Do not* include "test" or "kunit" directly in the subsystem name
  34. unless we are actually testing other tests or the kunit framework itself. For
  35. example, subsystems could be called:
  36. ``ext4``
  37. Matches the module and filesystem name.
  38. ``apparmor``
  39. Matches the module name and LSM name.
  40. ``kasan``
  41. Common name for the tool, prominent part of the path ``mm/kasan``
  42. ``snd_hda_codec_hdmi``
  43. Has several components (``snd``, ``hda``, ``codec``, ``hdmi``) separated by
  44. underscores. Matches the module name.
  45. Avoid names as shown in examples below:
  46. ``linear-ranges``
  47. Names should use underscores, not dashes, to separate words. Prefer
  48. ``linear_ranges``.
  49. ``qos-kunit-test``
  50. This name should use underscores, and not have "kunit-test" as a
  51. suffix. ``qos`` is also ambiguous as a subsystem name, because several parts
  52. of the kernel have a ``qos`` subsystem. ``power_qos`` would be a better name.
  53. ``pc_parallel_port``
  54. The corresponding module name is ``parport_pc``, so this subsystem should also
  55. be named ``parport_pc``.
  56. .. note::
  57. The KUnit API and tools do not explicitly know about subsystems. They are
  58. a way of categorizing test suites and naming modules which provides a
  59. simple, consistent way for humans to find and run tests. This may change
  60. in the future.
  61. Suites
  62. ------
  63. KUnit tests are grouped into test suites, which cover a specific area of
  64. functionality being tested. Test suites can have shared initialization and
  65. shutdown code which is run for all tests in the suite. Not all subsystems need
  66. to be split into multiple test suites (for example, simple drivers).
  67. Test suites are named after the subsystem they are part of. If a subsystem
  68. contains several suites, the specific area under test should be appended to the
  69. subsystem name, separated by an underscore.
  70. In the event that there are multiple types of test using KUnit within a
  71. subsystem (for example, both unit tests and integration tests), they should be
  72. put into separate suites, with the type of test as the last element in the suite
  73. name. Unless these tests are actually present, avoid using ``_test``, ``_unittest``
  74. or similar in the suite name.
  75. The full test suite name (including the subsystem name) should be specified as
  76. the ``.name`` member of the ``kunit_suite`` struct, and forms the base for the
  77. module name. For example, test suites could include:
  78. ``ext4_inode``
  79. Part of the ``ext4`` subsystem, testing the ``inode`` area.
  80. ``kunit_try_catch``
  81. Part of the ``kunit`` implementation itself, testing the ``try_catch`` area.
  82. ``apparmor_property_entry``
  83. Part of the ``apparmor`` subsystem, testing the ``property_entry`` area.
  84. ``kasan``
  85. The ``kasan`` subsystem has only one suite, so the suite name is the same as
  86. the subsystem name.
  87. Avoid names, for example:
  88. ``ext4_ext4_inode``
  89. There is no reason to state the subsystem twice.
  90. ``property_entry``
  91. The suite name is ambiguous without the subsystem name.
  92. ``kasan_integration_test``
  93. Because there is only one suite in the ``kasan`` subsystem, the suite should
  94. just be called as ``kasan``. Do not redundantly add
  95. ``integration_test``. It should be a separate test suite. For example, if the
  96. unit tests are added, then that suite could be named as ``kasan_unittest`` or
  97. similar.
  98. Test Cases
  99. ----------
  100. Individual tests consist of a single function which tests a constrained
  101. codepath, property, or function. In the test output, an individual test's
  102. results will show up as subtests of the suite's results.
  103. Tests should be named after what they are testing. This is often the name of the
  104. function being tested, with a description of the input or codepath being tested.
  105. As tests are C functions, they should be named and written in accordance with
  106. the kernel coding style.
  107. .. note::
  108. As tests are themselves functions, their names cannot conflict with
  109. other C identifiers in the kernel. This may require some creative
  110. naming. It is a good idea to make your test functions `static` to avoid
  111. polluting the global namespace.
  112. Example test names include:
  113. ``unpack_u32_with_null_name``
  114. Tests the ``unpack_u32`` function when a NULL name is passed in.
  115. ``test_list_splice``
  116. Tests the ``list_splice`` macro. It has the prefix ``test_`` to avoid a
  117. name conflict with the macro itself.
  118. Should it be necessary to refer to a test outside the context of its test suite,
  119. the *fully-qualified* name of a test should be the suite name followed by the
  120. test name, separated by a colon (i.e. ``suite:test``).
  121. Test Kconfig Entries
  122. ====================
  123. Every test suite should be tied to a Kconfig entry.
  124. This Kconfig entry must:
  125. * be named ``CONFIG_<name>_KUNIT_TEST``: where <name> is the name of the test
  126. suite.
  127. * be listed either alongside the config entries for the driver/subsystem being
  128. tested, or be under [Kernel Hacking]->[Kernel Testing and Coverage]
  129. * depend on ``CONFIG_KUNIT``.
  130. * be visible only if ``CONFIG_KUNIT_ALL_TESTS`` is not enabled.
  131. * have a default value of ``CONFIG_KUNIT_ALL_TESTS``.
  132. * have a brief description of KUnit in the help text.
  133. If we are not able to meet above conditions (for example, the test is unable to
  134. be built as a module), Kconfig entries for tests should be tristate.
  135. For example, a Kconfig entry might look like:
  136. .. code-block:: none
  137. config FOO_KUNIT_TEST
  138. tristate "KUnit test for foo" if !KUNIT_ALL_TESTS
  139. depends on KUNIT
  140. default KUNIT_ALL_TESTS
  141. help
  142. This builds unit tests for foo.
  143. For more information on KUnit and unit tests in general,
  144. please refer to the KUnit documentation in Documentation/dev-tools/kunit/.
  145. If unsure, say N.
  146. Test File and Module Names
  147. ==========================
  148. KUnit tests can often be compiled as a module. These modules should be named
  149. after the test suite, followed by ``_test``. If this is likely to conflict with
  150. non-KUnit tests, the suffix ``_kunit`` can also be used.
  151. The easiest way of achieving this is to name the file containing the test suite
  152. ``<suite>_test.c`` (or, as above, ``<suite>_kunit.c``). This file should be
  153. placed next to the code under test.
  154. If the suite name contains some or all of the name of the test's parent
  155. directory, it may make sense to modify the source filename to reduce redundancy.
  156. For example, a ``foo_firmware`` suite could be in the ``foo/firmware_test.c``
  157. file.