xref: /OK3568_Linux_fs/kernel/Documentation/dev-tools/kunit/style.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun===========================
4*4882a593SmuzhiyunTest Style and Nomenclature
5*4882a593Smuzhiyun===========================
6*4882a593Smuzhiyun
7*4882a593SmuzhiyunTo make finding, writing, and using KUnit tests as simple as possible, it's
8*4882a593Smuzhiyunstrongly encouraged that they are named and written according to the guidelines
9*4882a593Smuzhiyunbelow. While it's possible to write KUnit tests which do not follow these rules,
10*4882a593Smuzhiyunthey may break some tooling, may conflict with other tests, and may not be run
11*4882a593Smuzhiyunautomatically by testing systems.
12*4882a593Smuzhiyun
13*4882a593SmuzhiyunIt's recommended that you only deviate from these guidelines when:
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun1. Porting tests to KUnit which are already known with an existing name, or
16*4882a593Smuzhiyun2. Writing tests which would cause serious problems if automatically run (e.g.,
17*4882a593Smuzhiyun   non-deterministically producing false positives or negatives, or taking an
18*4882a593Smuzhiyun   extremely long time to run).
19*4882a593Smuzhiyun
20*4882a593SmuzhiyunSubsystems, Suites, and Tests
21*4882a593Smuzhiyun=============================
22*4882a593Smuzhiyun
23*4882a593SmuzhiyunIn order to make tests as easy to find as possible, they're grouped into suites
24*4882a593Smuzhiyunand subsystems. A test suite is a group of tests which test a related area of
25*4882a593Smuzhiyunthe kernel, and a subsystem is a set of test suites which test different parts
26*4882a593Smuzhiyunof the same kernel subsystem or driver.
27*4882a593Smuzhiyun
28*4882a593SmuzhiyunSubsystems
29*4882a593Smuzhiyun----------
30*4882a593Smuzhiyun
31*4882a593SmuzhiyunEvery test suite must belong to a subsystem. A subsystem is a collection of one
32*4882a593Smuzhiyunor more KUnit test suites which test the same driver or part of the kernel. A
33*4882a593Smuzhiyunrule of thumb is that a test subsystem should match a single kernel module. If
34*4882a593Smuzhiyunthe code being tested can't be compiled as a module, in many cases the subsystem
35*4882a593Smuzhiyunshould correspond to a directory in the source tree or an entry in the
36*4882a593SmuzhiyunMAINTAINERS file. If unsure, follow the conventions set by tests in similar
37*4882a593Smuzhiyunareas.
38*4882a593Smuzhiyun
39*4882a593SmuzhiyunTest subsystems should be named after the code being tested, either after the
40*4882a593Smuzhiyunmodule (wherever possible), or after the directory or files being tested. Test
41*4882a593Smuzhiyunsubsystems should be named to avoid ambiguity where necessary.
42*4882a593Smuzhiyun
43*4882a593SmuzhiyunIf a test subsystem name has multiple components, they should be separated by
44*4882a593Smuzhiyununderscores. *Do not* include "test" or "kunit" directly in the subsystem name
45*4882a593Smuzhiyununless you are actually testing other tests or the kunit framework itself.
46*4882a593Smuzhiyun
47*4882a593SmuzhiyunExample subsystems could be:
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun``ext4``
50*4882a593Smuzhiyun  Matches the module and filesystem name.
51*4882a593Smuzhiyun``apparmor``
52*4882a593Smuzhiyun  Matches the module name and LSM name.
53*4882a593Smuzhiyun``kasan``
54*4882a593Smuzhiyun  Common name for the tool, prominent part of the path ``mm/kasan``
55*4882a593Smuzhiyun``snd_hda_codec_hdmi``
56*4882a593Smuzhiyun  Has several components (``snd``, ``hda``, ``codec``, ``hdmi``) separated by
57*4882a593Smuzhiyun  underscores. Matches the module name.
58*4882a593Smuzhiyun
59*4882a593SmuzhiyunAvoid names like these:
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun``linear-ranges``
62*4882a593Smuzhiyun  Names should use underscores, not dashes, to separate words. Prefer
63*4882a593Smuzhiyun  ``linear_ranges``.
64*4882a593Smuzhiyun``qos-kunit-test``
65*4882a593Smuzhiyun  As well as using underscores, this name should not have "kunit-test" as a
66*4882a593Smuzhiyun  suffix, and ``qos`` is ambiguous as a subsystem name. ``power_qos`` would be a
67*4882a593Smuzhiyun  better name.
68*4882a593Smuzhiyun``pc_parallel_port``
69*4882a593Smuzhiyun  The corresponding module name is ``parport_pc``, so this subsystem should also
70*4882a593Smuzhiyun  be named ``parport_pc``.
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun.. note::
73*4882a593Smuzhiyun        The KUnit API and tools do not explicitly know about subsystems. They're
74*4882a593Smuzhiyun        simply a way of categorising test suites and naming modules which
75*4882a593Smuzhiyun        provides a simple, consistent way for humans to find and run tests. This
76*4882a593Smuzhiyun        may change in the future, though.
77*4882a593Smuzhiyun
78*4882a593SmuzhiyunSuites
79*4882a593Smuzhiyun------
80*4882a593Smuzhiyun
81*4882a593SmuzhiyunKUnit tests are grouped into test suites, which cover a specific area of
82*4882a593Smuzhiyunfunctionality being tested. Test suites can have shared initialisation and
83*4882a593Smuzhiyunshutdown code which is run for all tests in the suite.
84*4882a593SmuzhiyunNot all subsystems will need to be split into multiple test suites (e.g. simple drivers).
85*4882a593Smuzhiyun
86*4882a593SmuzhiyunTest suites are named after the subsystem they are part of. If a subsystem
87*4882a593Smuzhiyuncontains several suites, the specific area under test should be appended to the
88*4882a593Smuzhiyunsubsystem name, separated by an underscore.
89*4882a593Smuzhiyun
90*4882a593SmuzhiyunIn the event that there are multiple types of test using KUnit within a
91*4882a593Smuzhiyunsubsystem (e.g., both unit tests and integration tests), they should be put into
92*4882a593Smuzhiyunseparate suites, with the type of test as the last element in the suite name.
93*4882a593SmuzhiyunUnless these tests are actually present, avoid using ``_test``, ``_unittest`` or
94*4882a593Smuzhiyunsimilar in the suite name.
95*4882a593Smuzhiyun
96*4882a593SmuzhiyunThe full test suite name (including the subsystem name) should be specified as
97*4882a593Smuzhiyunthe ``.name`` member of the ``kunit_suite`` struct, and forms the base for the
98*4882a593Smuzhiyunmodule name (see below).
99*4882a593Smuzhiyun
100*4882a593SmuzhiyunExample test suites could include:
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun``ext4_inode``
103*4882a593Smuzhiyun  Part of the ``ext4`` subsystem, testing the ``inode`` area.
104*4882a593Smuzhiyun``kunit_try_catch``
105*4882a593Smuzhiyun  Part of the ``kunit`` implementation itself, testing the ``try_catch`` area.
106*4882a593Smuzhiyun``apparmor_property_entry``
107*4882a593Smuzhiyun  Part of the ``apparmor`` subsystem, testing the ``property_entry`` area.
108*4882a593Smuzhiyun``kasan``
109*4882a593Smuzhiyun  The ``kasan`` subsystem has only one suite, so the suite name is the same as
110*4882a593Smuzhiyun  the subsystem name.
111*4882a593Smuzhiyun
112*4882a593SmuzhiyunAvoid names like:
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun``ext4_ext4_inode``
115*4882a593Smuzhiyun  There's no reason to state the subsystem twice.
116*4882a593Smuzhiyun``property_entry``
117*4882a593Smuzhiyun  The suite name is ambiguous without the subsystem name.
118*4882a593Smuzhiyun``kasan_integration_test``
119*4882a593Smuzhiyun  Because there is only one suite in the ``kasan`` subsystem, the suite should
120*4882a593Smuzhiyun  just be called ``kasan``. There's no need to redundantly add
121*4882a593Smuzhiyun  ``integration_test``. Should a separate test suite with, for example, unit
122*4882a593Smuzhiyun  tests be added, then that suite could be named ``kasan_unittest`` or similar.
123*4882a593Smuzhiyun
124*4882a593SmuzhiyunTest Cases
125*4882a593Smuzhiyun----------
126*4882a593Smuzhiyun
127*4882a593SmuzhiyunIndividual tests consist of a single function which tests a constrained
128*4882a593Smuzhiyuncodepath, property, or function. In the test output, individual tests' results
129*4882a593Smuzhiyunwill show up as subtests of the suite's results.
130*4882a593Smuzhiyun
131*4882a593SmuzhiyunTests should be named after what they're testing. This is often the name of the
132*4882a593Smuzhiyunfunction being tested, with a description of the input or codepath being tested.
133*4882a593SmuzhiyunAs tests are C functions, they should be named and written in accordance with
134*4882a593Smuzhiyunthe kernel coding style.
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun.. note::
137*4882a593Smuzhiyun        As tests are themselves functions, their names cannot conflict with
138*4882a593Smuzhiyun        other C identifiers in the kernel. This may require some creative
139*4882a593Smuzhiyun        naming. It's a good idea to make your test functions `static` to avoid
140*4882a593Smuzhiyun        polluting the global namespace.
141*4882a593Smuzhiyun
142*4882a593SmuzhiyunExample test names include:
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun``unpack_u32_with_null_name``
145*4882a593Smuzhiyun  Tests the ``unpack_u32`` function when a NULL name is passed in.
146*4882a593Smuzhiyun``test_list_splice``
147*4882a593Smuzhiyun  Tests the ``list_splice`` macro. It has the prefix ``test_`` to avoid a
148*4882a593Smuzhiyun  name conflict with the macro itself.
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun
151*4882a593SmuzhiyunShould it be necessary to refer to a test outside the context of its test suite,
152*4882a593Smuzhiyunthe *fully-qualified* name of a test should be the suite name followed by the
153*4882a593Smuzhiyuntest name, separated by a colon (i.e. ``suite:test``).
154*4882a593Smuzhiyun
155*4882a593SmuzhiyunTest Kconfig Entries
156*4882a593Smuzhiyun====================
157*4882a593Smuzhiyun
158*4882a593SmuzhiyunEvery test suite should be tied to a Kconfig entry.
159*4882a593Smuzhiyun
160*4882a593SmuzhiyunThis Kconfig entry must:
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun* be named ``CONFIG_<name>_KUNIT_TEST``: where <name> is the name of the test
163*4882a593Smuzhiyun  suite.
164*4882a593Smuzhiyun* be listed either alongside the config entries for the driver/subsystem being
165*4882a593Smuzhiyun  tested, or be under [Kernel Hacking]→[Kernel Testing and Coverage]
166*4882a593Smuzhiyun* depend on ``CONFIG_KUNIT``
167*4882a593Smuzhiyun* be visible only if ``CONFIG_KUNIT_ALL_TESTS`` is not enabled.
168*4882a593Smuzhiyun* have a default value of ``CONFIG_KUNIT_ALL_TESTS``.
169*4882a593Smuzhiyun* have a brief description of KUnit in the help text
170*4882a593Smuzhiyun
171*4882a593SmuzhiyunUnless there's a specific reason not to (e.g. the test is unable to be built as
172*4882a593Smuzhiyuna module), Kconfig entries for tests should be tristate.
173*4882a593Smuzhiyun
174*4882a593SmuzhiyunAn example Kconfig entry:
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun.. code-block:: none
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun	config FOO_KUNIT_TEST
179*4882a593Smuzhiyun		tristate "KUnit test for foo" if !KUNIT_ALL_TESTS
180*4882a593Smuzhiyun		depends on KUNIT
181*4882a593Smuzhiyun		default KUNIT_ALL_TESTS
182*4882a593Smuzhiyun		help
183*4882a593Smuzhiyun		  This builds unit tests for foo.
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun		  For more information on KUnit and unit tests in general, please refer
186*4882a593Smuzhiyun		  to the KUnit documentation in Documentation/dev-tools/kunit/.
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun		  If unsure, say N.
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun
191*4882a593SmuzhiyunTest File and Module Names
192*4882a593Smuzhiyun==========================
193*4882a593Smuzhiyun
194*4882a593SmuzhiyunKUnit tests can often be compiled as a module. These modules should be named
195*4882a593Smuzhiyunafter the test suite, followed by ``_test``. If this is likely to conflict with
196*4882a593Smuzhiyunnon-KUnit tests, the suffix ``_kunit`` can also be used.
197*4882a593Smuzhiyun
198*4882a593SmuzhiyunThe easiest way of achieving this is to name the file containing the test suite
199*4882a593Smuzhiyun``<suite>_test.c`` (or, as above, ``<suite>_kunit.c``). This file should be
200*4882a593Smuzhiyunplaced next to the code under test.
201*4882a593Smuzhiyun
202*4882a593SmuzhiyunIf the suite name contains some or all of the name of the test's parent
203*4882a593Smuzhiyundirectory, it may make sense to modify the source filename to reduce redundancy.
204*4882a593SmuzhiyunFor example, a ``foo_firmware`` suite could be in the ``foo/firmware_test.c``
205*4882a593Smuzhiyunfile.
206