Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Documentation: KUnit: Restyle Test Style and Nomenclature page

Rewrite page to enhance content consistency.

Signed-off-by: Harinder Singh <sharinder@google.com>
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
Link: https://lore.kernel.org/r/20211217044911.798817-7-sharinder@google.com
Signed-off-by: Jonathan Corbet <corbet@lwn.net>

authored by

Harinder Singh and committed by
Jonathan Corbet
39150e80 95357439

+51 -54
+51 -54
Documentation/dev-tools/kunit/style.rst
··· 4 4 Test Style and Nomenclature 5 5 =========================== 6 6 7 - To make finding, writing, and using KUnit tests as simple as possible, it's 7 + To make finding, writing, and using KUnit tests as simple as possible, it is 8 8 strongly encouraged that they are named and written according to the guidelines 9 - below. While it's possible to write KUnit tests which do not follow these rules, 9 + below. While it is possible to write KUnit tests which do not follow these rules, 10 10 they may break some tooling, may conflict with other tests, and may not be run 11 11 automatically by testing systems. 12 12 13 - It's recommended that you only deviate from these guidelines when: 13 + It is recommended that you only deviate from these guidelines when: 14 14 15 - 1. Porting tests to KUnit which are already known with an existing name, or 16 - 2. Writing tests which would cause serious problems if automatically run (e.g., 17 - non-deterministically producing false positives or negatives, or taking an 18 - extremely long time to run). 15 + 1. Porting tests to KUnit which are already known with an existing name. 16 + 2. Writing tests which would cause serious problems if automatically run. For 17 + example, non-deterministically producing false positives or negatives, or 18 + taking a long time to run. 19 19 20 20 Subsystems, Suites, and Tests 21 21 ============================= 22 22 23 - In order to make tests as easy to find as possible, they're grouped into suites 24 - and subsystems. A test suite is a group of tests which test a related area of 25 - the kernel, and a subsystem is a set of test suites which test different parts 26 - of the same kernel subsystem or driver. 23 + To make tests easy to find, they are grouped into suites and subsystems. A test 24 + suite is a group of tests which test a related area of the kernel. A subsystem 25 + is a set of test suites which test different parts of a kernel subsystem 26 + or a driver. 27 27 28 28 Subsystems 29 29 ---------- 30 30 31 31 Every test suite must belong to a subsystem. A subsystem is a collection of one 32 32 or more KUnit test suites which test the same driver or part of the kernel. A 33 - rule of thumb is that a test subsystem should match a single kernel module. If 34 - the code being tested can't be compiled as a module, in many cases the subsystem 35 - should correspond to a directory in the source tree or an entry in the 36 - MAINTAINERS file. If unsure, follow the conventions set by tests in similar 37 - areas. 33 + test subsystem should match a single kernel module. If the code being tested 34 + cannot be compiled as a module, in many cases the subsystem should correspond to 35 + a directory in the source tree or an entry in the ``MAINTAINERS`` file. If 36 + unsure, follow the conventions set by tests in similar areas. 38 37 39 38 Test subsystems should be named after the code being tested, either after the 40 39 module (wherever possible), or after the directory or files being tested. Test ··· 41 42 42 43 If a test subsystem name has multiple components, they should be separated by 43 44 underscores. *Do not* include "test" or "kunit" directly in the subsystem name 44 - unless you are actually testing other tests or the kunit framework itself. 45 - 46 - Example subsystems could be: 45 + unless we are actually testing other tests or the kunit framework itself. For 46 + example, subsystems could be called: 47 47 48 48 ``ext4`` 49 49 Matches the module and filesystem name. ··· 54 56 Has several components (``snd``, ``hda``, ``codec``, ``hdmi``) separated by 55 57 underscores. Matches the module name. 56 58 57 - Avoid names like these: 59 + Avoid names as shown in examples below: 58 60 59 61 ``linear-ranges`` 60 62 Names should use underscores, not dashes, to separate words. Prefer 61 63 ``linear_ranges``. 62 64 ``qos-kunit-test`` 63 - As well as using underscores, this name should not have "kunit-test" as a 64 - suffix, and ``qos`` is ambiguous as a subsystem name. ``power_qos`` would be a 65 - better name. 65 + This name should use underscores, and not have "kunit-test" as a 66 + suffix. ``qos`` is also ambiguous as a subsystem name, because several parts 67 + of the kernel have a ``qos`` subsystem. ``power_qos`` would be a better name. 66 68 ``pc_parallel_port`` 67 69 The corresponding module name is ``parport_pc``, so this subsystem should also 68 70 be named ``parport_pc``. 69 71 70 72 .. note:: 71 - The KUnit API and tools do not explicitly know about subsystems. They're 72 - simply a way of categorising test suites and naming modules which 73 - provides a simple, consistent way for humans to find and run tests. This 74 - may change in the future, though. 73 + The KUnit API and tools do not explicitly know about subsystems. They are 74 + a way of categorizing test suites and naming modules which provides a 75 + simple, consistent way for humans to find and run tests. This may change 76 + in the future. 75 77 76 78 Suites 77 79 ------ 78 80 79 81 KUnit tests are grouped into test suites, which cover a specific area of 80 - functionality being tested. Test suites can have shared initialisation and 81 - shutdown code which is run for all tests in the suite. 82 - Not all subsystems will need to be split into multiple test suites (e.g. simple drivers). 82 + functionality being tested. Test suites can have shared initialization and 83 + shutdown code which is run for all tests in the suite. Not all subsystems need 84 + to be split into multiple test suites (for example, simple drivers). 83 85 84 86 Test suites are named after the subsystem they are part of. If a subsystem 85 87 contains several suites, the specific area under test should be appended to the 86 88 subsystem name, separated by an underscore. 87 89 88 90 In the event that there are multiple types of test using KUnit within a 89 - subsystem (e.g., both unit tests and integration tests), they should be put into 90 - separate suites, with the type of test as the last element in the suite name. 91 - Unless these tests are actually present, avoid using ``_test``, ``_unittest`` or 92 - similar in the suite name. 91 + subsystem (for example, both unit tests and integration tests), they should be 92 + put into separate suites, with the type of test as the last element in the suite 93 + name. Unless these tests are actually present, avoid using ``_test``, ``_unittest`` 94 + or similar in the suite name. 93 95 94 96 The full test suite name (including the subsystem name) should be specified as 95 97 the ``.name`` member of the ``kunit_suite`` struct, and forms the base for the 96 - module name (see below). 97 - 98 - Example test suites could include: 98 + module name. For example, test suites could include: 99 99 100 100 ``ext4_inode`` 101 101 Part of the ``ext4`` subsystem, testing the ``inode`` area. ··· 105 109 The ``kasan`` subsystem has only one suite, so the suite name is the same as 106 110 the subsystem name. 107 111 108 - Avoid names like: 112 + Avoid names, for example: 109 113 110 114 ``ext4_ext4_inode`` 111 - There's no reason to state the subsystem twice. 115 + There is no reason to state the subsystem twice. 112 116 ``property_entry`` 113 117 The suite name is ambiguous without the subsystem name. 114 118 ``kasan_integration_test`` 115 119 Because there is only one suite in the ``kasan`` subsystem, the suite should 116 - just be called ``kasan``. There's no need to redundantly add 117 - ``integration_test``. Should a separate test suite with, for example, unit 118 - tests be added, then that suite could be named ``kasan_unittest`` or similar. 120 + just be called as ``kasan``. Do not redundantly add 121 + ``integration_test``. It should be a separate test suite. For example, if the 122 + unit tests are added, then that suite could be named as ``kasan_unittest`` or 123 + similar. 119 124 120 125 Test Cases 121 126 ---------- 122 127 123 128 Individual tests consist of a single function which tests a constrained 124 - codepath, property, or function. In the test output, individual tests' results 125 - will show up as subtests of the suite's results. 129 + codepath, property, or function. In the test output, an individual test's 130 + results will show up as subtests of the suite's results. 126 131 127 - Tests should be named after what they're testing. This is often the name of the 132 + Tests should be named after what they are testing. This is often the name of the 128 133 function being tested, with a description of the input or codepath being tested. 129 134 As tests are C functions, they should be named and written in accordance with 130 135 the kernel coding style. ··· 133 136 .. note:: 134 137 As tests are themselves functions, their names cannot conflict with 135 138 other C identifiers in the kernel. This may require some creative 136 - naming. It's a good idea to make your test functions `static` to avoid 139 + naming. It is a good idea to make your test functions `static` to avoid 137 140 polluting the global namespace. 138 141 139 142 Example test names include: ··· 159 162 * be named ``CONFIG_<name>_KUNIT_TEST``: where <name> is the name of the test 160 163 suite. 161 164 * be listed either alongside the config entries for the driver/subsystem being 162 - tested, or be under [Kernel Hacking]→[Kernel Testing and Coverage] 163 - * depend on ``CONFIG_KUNIT`` 165 + tested, or be under [Kernel Hacking]->[Kernel Testing and Coverage] 166 + * depend on ``CONFIG_KUNIT``. 164 167 * be visible only if ``CONFIG_KUNIT_ALL_TESTS`` is not enabled. 165 168 * have a default value of ``CONFIG_KUNIT_ALL_TESTS``. 166 - * have a brief description of KUnit in the help text 169 + * have a brief description of KUnit in the help text. 167 170 168 - Unless there's a specific reason not to (e.g. the test is unable to be built as 169 - a module), Kconfig entries for tests should be tristate. 171 + If we are not able to meet above conditions (for example, the test is unable to 172 + be built as a module), Kconfig entries for tests should be tristate. 170 173 171 - An example Kconfig entry: 174 + For example, a Kconfig entry might look like: 172 175 173 176 .. code-block:: none 174 177 ··· 179 182 help 180 183 This builds unit tests for foo. 181 184 182 - For more information on KUnit and unit tests in general, please refer 183 - to the KUnit documentation in Documentation/dev-tools/kunit/. 185 + For more information on KUnit and unit tests in general, 186 + please refer to the KUnit documentation in Documentation/dev-tools/kunit/. 184 187 185 188 If unsure, say N. 186 189