Skip to content

Commit 39150e8

Browse files
Harinder SinghJonathan Corbet
authored andcommitted
Documentation: KUnit: Restyle Test Style and Nomenclature page
Rewrite page to enhance content consistency. Signed-off-by: Harinder Singh <[email protected]> Reviewed-by: Brendan Higgins <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jonathan Corbet <[email protected]>
1 parent 9535743 commit 39150e8

File tree

1 file changed

+51
-54
lines changed

1 file changed

+51
-54
lines changed

Documentation/dev-tools/kunit/style.rst

Lines changed: 51 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,45 @@
44
Test Style and Nomenclature
55
===========================
66

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
88
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,
1010
they may break some tooling, may conflict with other tests, and may not be run
1111
automatically by testing systems.
1212

13-
It's recommended that you only deviate from these guidelines when:
13+
It is recommended that you only deviate from these guidelines when:
1414

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.
1919

2020
Subsystems, Suites, and Tests
2121
=============================
2222

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.
2727

2828
Subsystems
2929
----------
3030

3131
Every test suite must belong to a subsystem. A subsystem is a collection of one
3232
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.
3837

3938
Test subsystems should be named after the code being tested, either after the
4039
module (wherever possible), or after the directory or files being tested. Test
4140
subsystems should be named to avoid ambiguity where necessary.
4241

4342
If a test subsystem name has multiple components, they should be separated by
4443
underscores. *Do not* include "test" or "kunit" directly in the subsystem name
45-
unless you are actually testing other tests or the kunit framework itself.
46-
47-
Example subsystems could be:
44+
unless we are actually testing other tests or the kunit framework itself. For
45+
example, subsystems could be called:
4846

4947
``ext4``
5048
Matches the module and filesystem name.
@@ -56,48 +54,46 @@ Example subsystems could be:
5654
Has several components (``snd``, ``hda``, ``codec``, ``hdmi``) separated by
5755
underscores. Matches the module name.
5856

59-
Avoid names like these:
57+
Avoid names as shown in examples below:
6058

6159
``linear-ranges``
6260
Names should use underscores, not dashes, to separate words. Prefer
6361
``linear_ranges``.
6462
``qos-kunit-test``
65-
As well as using underscores, this name should not have "kunit-test" as a
66-
suffix, and ``qos`` is ambiguous as a subsystem name. ``power_qos`` would be a
67-
better name.
63+
This name should use underscores, and not have "kunit-test" as a
64+
suffix. ``qos`` is also ambiguous as a subsystem name, because several parts
65+
of the kernel have a ``qos`` subsystem. ``power_qos`` would be a better name.
6866
``pc_parallel_port``
6967
The corresponding module name is ``parport_pc``, so this subsystem should also
7068
be named ``parport_pc``.
7169

7270
.. note::
73-
The KUnit API and tools do not explicitly know about subsystems. They're
74-
simply a way of categorising test suites and naming modules which
75-
provides a simple, consistent way for humans to find and run tests. This
76-
may change in the future, though.
71+
The KUnit API and tools do not explicitly know about subsystems. They are
72+
a way of categorizing test suites and naming modules which provides a
73+
simple, consistent way for humans to find and run tests. This may change
74+
in the future.
7775

7876
Suites
7977
------
8078

8179
KUnit tests are grouped into test suites, which cover a specific area of
82-
functionality being tested. Test suites can have shared initialisation and
83-
shutdown code which is run for all tests in the suite.
84-
Not all subsystems will need to be split into multiple test suites (e.g. simple drivers).
80+
functionality being tested. Test suites can have shared initialization and
81+
shutdown code which is run for all tests in the suite. Not all subsystems need
82+
to be split into multiple test suites (for example, simple drivers).
8583

8684
Test suites are named after the subsystem they are part of. If a subsystem
8785
contains several suites, the specific area under test should be appended to the
8886
subsystem name, separated by an underscore.
8987

9088
In the event that there are multiple types of test using KUnit within a
91-
subsystem (e.g., both unit tests and integration tests), they should be put into
92-
separate suites, with the type of test as the last element in the suite name.
93-
Unless these tests are actually present, avoid using ``_test``, ``_unittest`` or
94-
similar in the suite name.
89+
subsystem (for example, both unit tests and integration tests), they should be
90+
put into separate suites, with the type of test as the last element in the suite
91+
name. Unless these tests are actually present, avoid using ``_test``, ``_unittest``
92+
or similar in the suite name.
9593

9694
The full test suite name (including the subsystem name) should be specified as
9795
the ``.name`` member of the ``kunit_suite`` struct, and forms the base for the
98-
module name (see below).
99-
100-
Example test suites could include:
96+
module name. For example, test suites could include:
10197

10298
``ext4_inode``
10399
Part of the ``ext4`` subsystem, testing the ``inode`` area.
@@ -109,34 +105,35 @@ Example test suites could include:
109105
The ``kasan`` subsystem has only one suite, so the suite name is the same as
110106
the subsystem name.
111107

112-
Avoid names like:
108+
Avoid names, for example:
113109

114110
``ext4_ext4_inode``
115-
There's no reason to state the subsystem twice.
111+
There is no reason to state the subsystem twice.
116112
``property_entry``
117113
The suite name is ambiguous without the subsystem name.
118114
``kasan_integration_test``
119115
Because there is only one suite in the ``kasan`` subsystem, the suite should
120-
just be called ``kasan``. There's no need to redundantly add
121-
``integration_test``. Should a separate test suite with, for example, unit
122-
tests be added, then that suite could be named ``kasan_unittest`` or similar.
116+
just be called as ``kasan``. Do not redundantly add
117+
``integration_test``. It should be a separate test suite. For example, if the
118+
unit tests are added, then that suite could be named as ``kasan_unittest`` or
119+
similar.
123120

124121
Test Cases
125122
----------
126123

127124
Individual tests consist of a single function which tests a constrained
128-
codepath, property, or function. In the test output, individual tests' results
129-
will show up as subtests of the suite's results.
125+
codepath, property, or function. In the test output, an individual test's
126+
results will show up as subtests of the suite's results.
130127

131-
Tests should be named after what they're testing. This is often the name of the
128+
Tests should be named after what they are testing. This is often the name of the
132129
function being tested, with a description of the input or codepath being tested.
133130
As tests are C functions, they should be named and written in accordance with
134131
the kernel coding style.
135132

136133
.. note::
137134
As tests are themselves functions, their names cannot conflict with
138135
other C identifiers in the kernel. This may require some creative
139-
naming. It's a good idea to make your test functions `static` to avoid
136+
naming. It is a good idea to make your test functions `static` to avoid
140137
polluting the global namespace.
141138

142139
Example test names include:
@@ -162,16 +159,16 @@ This Kconfig entry must:
162159
* be named ``CONFIG_<name>_KUNIT_TEST``: where <name> is the name of the test
163160
suite.
164161
* be listed either alongside the config entries for the driver/subsystem being
165-
tested, or be under [Kernel Hacking][Kernel Testing and Coverage]
166-
* depend on ``CONFIG_KUNIT``
162+
tested, or be under [Kernel Hacking]->[Kernel Testing and Coverage]
163+
* depend on ``CONFIG_KUNIT``.
167164
* be visible only if ``CONFIG_KUNIT_ALL_TESTS`` is not enabled.
168165
* have a default value of ``CONFIG_KUNIT_ALL_TESTS``.
169-
* have a brief description of KUnit in the help text
166+
* have a brief description of KUnit in the help text.
170167

171-
Unless there's a specific reason not to (e.g. the test is unable to be built as
172-
a module), Kconfig entries for tests should be tristate.
168+
If we are not able to meet above conditions (for example, the test is unable to
169+
be built as a module), Kconfig entries for tests should be tristate.
173170

174-
An example Kconfig entry:
171+
For example, a Kconfig entry might look like:
175172

176173
.. code-block:: none
177174
@@ -182,8 +179,8 @@ An example Kconfig entry:
182179
help
183180
This builds unit tests for foo.
184181
185-
For more information on KUnit and unit tests in general, please refer
186-
to the KUnit documentation in Documentation/dev-tools/kunit/.
182+
For more information on KUnit and unit tests in general,
183+
please refer to the KUnit documentation in Documentation/dev-tools/kunit/.
187184
188185
If unsure, say N.
189186

0 commit comments

Comments
 (0)