Commit 39150e80 authored by Harinder Singh's avatar Harinder Singh Committed by Jonathan Corbet

Documentation: KUnit: Restyle Test Style and Nomenclature page

Rewrite page to enhance content consistency.
Signed-off-by: default avatarHarinder Singh <sharinder@google.com>
Reviewed-by: default avatarBrendan Higgins <brendanhiggins@google.com>
Link: https://lore.kernel.org/r/20211217044911.798817-7-sharinder@google.comSigned-off-by: default avatarJonathan Corbet <corbet@lwn.net>
parent 95357439
...@@ -4,37 +4,36 @@ ...@@ -4,37 +4,36 @@
Test Style and Nomenclature Test Style and Nomenclature
=========================== ===========================
To make finding, writing, and using KUnit tests as simple as possible, it's To make finding, writing, and using KUnit tests as simple as possible, it is
strongly encouraged that they are named and written according to the guidelines strongly encouraged that they are named and written according to the guidelines
below. While it's possible to write KUnit tests which do not follow these rules, below. While it is possible to write KUnit tests which do not follow these rules,
they may break some tooling, may conflict with other tests, and may not be run they may break some tooling, may conflict with other tests, and may not be run
automatically by testing systems. automatically by testing systems.
It's recommended that you only deviate from these guidelines when: It is recommended that you only deviate from these guidelines when:
1. Porting tests to KUnit which are already known with an existing name, or 1. Porting tests to KUnit which are already known with an existing name.
2. Writing tests which would cause serious problems if automatically run (e.g., 2. Writing tests which would cause serious problems if automatically run. For
non-deterministically producing false positives or negatives, or taking an example, non-deterministically producing false positives or negatives, or
extremely long time to run). taking a long time to run.
Subsystems, Suites, and Tests Subsystems, Suites, and Tests
============================= =============================
In order to make tests as easy to find as possible, they're grouped into suites To make tests easy to find, they are grouped into suites and subsystems. A test
and subsystems. A test suite is a group of tests which test a related area of suite is a group of tests which test a related area of the kernel. A subsystem
the kernel, and a subsystem is a set of test suites which test different parts is a set of test suites which test different parts of a kernel subsystem
of the same kernel subsystem or driver. or a driver.
Subsystems Subsystems
---------- ----------
Every test suite must belong to a subsystem. A subsystem is a collection of one Every test suite must belong to a subsystem. A subsystem is a collection of one
or more KUnit test suites which test the same driver or part of the kernel. A or more KUnit test suites which test the same driver or part of the kernel. A
rule of thumb is that a test subsystem should match a single kernel module. If test subsystem should match a single kernel module. If the code being tested
the code being tested can't be compiled as a module, in many cases the subsystem cannot be compiled as a module, in many cases the subsystem should correspond to
should correspond to a directory in the source tree or an entry in the a directory in the source tree or an entry in the ``MAINTAINERS`` file. If
MAINTAINERS file. If unsure, follow the conventions set by tests in similar unsure, follow the conventions set by tests in similar areas.
areas.
Test subsystems should be named after the code being tested, either after the Test subsystems should be named after the code being tested, either after the
module (wherever possible), or after the directory or files being tested. Test module (wherever possible), or after the directory or files being tested. Test
...@@ -42,9 +41,8 @@ subsystems should be named to avoid ambiguity where necessary. ...@@ -42,9 +41,8 @@ subsystems should be named to avoid ambiguity where necessary.
If a test subsystem name has multiple components, they should be separated by If a test subsystem name has multiple components, they should be separated by
underscores. *Do not* include "test" or "kunit" directly in the subsystem name underscores. *Do not* include "test" or "kunit" directly in the subsystem name
unless you are actually testing other tests or the kunit framework itself. unless we are actually testing other tests or the kunit framework itself. For
example, subsystems could be called:
Example subsystems could be:
``ext4`` ``ext4``
Matches the module and filesystem name. Matches the module and filesystem name.
...@@ -56,48 +54,46 @@ Example subsystems could be: ...@@ -56,48 +54,46 @@ Example subsystems could be:
Has several components (``snd``, ``hda``, ``codec``, ``hdmi``) separated by Has several components (``snd``, ``hda``, ``codec``, ``hdmi``) separated by
underscores. Matches the module name. underscores. Matches the module name.
Avoid names like these: Avoid names as shown in examples below:
``linear-ranges`` ``linear-ranges``
Names should use underscores, not dashes, to separate words. Prefer Names should use underscores, not dashes, to separate words. Prefer
``linear_ranges``. ``linear_ranges``.
``qos-kunit-test`` ``qos-kunit-test``
As well as using underscores, this name should not have "kunit-test" as a This name should use underscores, and not have "kunit-test" as a
suffix, and ``qos`` is ambiguous as a subsystem name. ``power_qos`` would be a suffix. ``qos`` is also ambiguous as a subsystem name, because several parts
better name. of the kernel have a ``qos`` subsystem. ``power_qos`` would be a better name.
``pc_parallel_port`` ``pc_parallel_port``
The corresponding module name is ``parport_pc``, so this subsystem should also The corresponding module name is ``parport_pc``, so this subsystem should also
be named ``parport_pc``. be named ``parport_pc``.
.. note:: .. note::
The KUnit API and tools do not explicitly know about subsystems. They're The KUnit API and tools do not explicitly know about subsystems. They are
simply a way of categorising test suites and naming modules which a way of categorizing test suites and naming modules which provides a
provides a simple, consistent way for humans to find and run tests. This simple, consistent way for humans to find and run tests. This may change
may change in the future, though. in the future.
Suites Suites
------ ------
KUnit tests are grouped into test suites, which cover a specific area of KUnit tests are grouped into test suites, which cover a specific area of
functionality being tested. Test suites can have shared initialisation and functionality being tested. Test suites can have shared initialization and
shutdown code which is run for all tests in the suite. shutdown code which is run for all tests in the suite. Not all subsystems need
Not all subsystems will need to be split into multiple test suites (e.g. simple drivers). to be split into multiple test suites (for example, simple drivers).
Test suites are named after the subsystem they are part of. If a subsystem Test suites are named after the subsystem they are part of. If a subsystem
contains several suites, the specific area under test should be appended to the contains several suites, the specific area under test should be appended to the
subsystem name, separated by an underscore. subsystem name, separated by an underscore.
In the event that there are multiple types of test using KUnit within a In the event that there are multiple types of test using KUnit within a
subsystem (e.g., both unit tests and integration tests), they should be put into subsystem (for example, both unit tests and integration tests), they should be
separate suites, with the type of test as the last element in the suite name. put into separate suites, with the type of test as the last element in the suite
Unless these tests are actually present, avoid using ``_test``, ``_unittest`` or name. Unless these tests are actually present, avoid using ``_test``, ``_unittest``
similar in the suite name. or similar in the suite name.
The full test suite name (including the subsystem name) should be specified as The full test suite name (including the subsystem name) should be specified as
the ``.name`` member of the ``kunit_suite`` struct, and forms the base for the the ``.name`` member of the ``kunit_suite`` struct, and forms the base for the
module name (see below). module name. For example, test suites could include:
Example test suites could include:
``ext4_inode`` ``ext4_inode``
Part of the ``ext4`` subsystem, testing the ``inode`` area. Part of the ``ext4`` subsystem, testing the ``inode`` area.
...@@ -109,26 +105,27 @@ Example test suites could include: ...@@ -109,26 +105,27 @@ Example test suites could include:
The ``kasan`` subsystem has only one suite, so the suite name is the same as The ``kasan`` subsystem has only one suite, so the suite name is the same as
the subsystem name. the subsystem name.
Avoid names like: Avoid names, for example:
``ext4_ext4_inode`` ``ext4_ext4_inode``
There's no reason to state the subsystem twice. There is no reason to state the subsystem twice.
``property_entry`` ``property_entry``
The suite name is ambiguous without the subsystem name. The suite name is ambiguous without the subsystem name.
``kasan_integration_test`` ``kasan_integration_test``
Because there is only one suite in the ``kasan`` subsystem, the suite should Because there is only one suite in the ``kasan`` subsystem, the suite should
just be called ``kasan``. There's no need to redundantly add just be called as ``kasan``. Do not redundantly add
``integration_test``. Should a separate test suite with, for example, unit ``integration_test``. It should be a separate test suite. For example, if the
tests be added, then that suite could be named ``kasan_unittest`` or similar. unit tests are added, then that suite could be named as ``kasan_unittest`` or
similar.
Test Cases Test Cases
---------- ----------
Individual tests consist of a single function which tests a constrained Individual tests consist of a single function which tests a constrained
codepath, property, or function. In the test output, individual tests' results codepath, property, or function. In the test output, an individual test's
will show up as subtests of the suite's results. results will show up as subtests of the suite's results.
Tests should be named after what they're testing. This is often the name of the Tests should be named after what they are testing. This is often the name of the
function being tested, with a description of the input or codepath being tested. function being tested, with a description of the input or codepath being tested.
As tests are C functions, they should be named and written in accordance with As tests are C functions, they should be named and written in accordance with
the kernel coding style. the kernel coding style.
...@@ -136,7 +133,7 @@ the kernel coding style. ...@@ -136,7 +133,7 @@ the kernel coding style.
.. note:: .. note::
As tests are themselves functions, their names cannot conflict with As tests are themselves functions, their names cannot conflict with
other C identifiers in the kernel. This may require some creative other C identifiers in the kernel. This may require some creative
naming. It's a good idea to make your test functions `static` to avoid naming. It is a good idea to make your test functions `static` to avoid
polluting the global namespace. polluting the global namespace.
Example test names include: Example test names include:
...@@ -162,16 +159,16 @@ This Kconfig entry must: ...@@ -162,16 +159,16 @@ This Kconfig entry must:
* be named ``CONFIG_<name>_KUNIT_TEST``: where <name> is the name of the test * be named ``CONFIG_<name>_KUNIT_TEST``: where <name> is the name of the test
suite. suite.
* be listed either alongside the config entries for the driver/subsystem being * be listed either alongside the config entries for the driver/subsystem being
tested, or be under [Kernel Hacking][Kernel Testing and Coverage] tested, or be under [Kernel Hacking]->[Kernel Testing and Coverage]
* depend on ``CONFIG_KUNIT`` * depend on ``CONFIG_KUNIT``.
* be visible only if ``CONFIG_KUNIT_ALL_TESTS`` is not enabled. * be visible only if ``CONFIG_KUNIT_ALL_TESTS`` is not enabled.
* have a default value of ``CONFIG_KUNIT_ALL_TESTS``. * have a default value of ``CONFIG_KUNIT_ALL_TESTS``.
* have a brief description of KUnit in the help text * have a brief description of KUnit in the help text.
Unless there's a specific reason not to (e.g. the test is unable to be built as If we are not able to meet above conditions (for example, the test is unable to
a module), Kconfig entries for tests should be tristate. be built as a module), Kconfig entries for tests should be tristate.
An example Kconfig entry: For example, a Kconfig entry might look like:
.. code-block:: none .. code-block:: none
...@@ -182,8 +179,8 @@ An example Kconfig entry: ...@@ -182,8 +179,8 @@ An example Kconfig entry:
help help
This builds unit tests for foo. This builds unit tests for foo.
For more information on KUnit and unit tests in general, please refer For more information on KUnit and unit tests in general,
to the KUnit documentation in Documentation/dev-tools/kunit/. please refer to the KUnit documentation in Documentation/dev-tools/kunit/.
If unsure, say N. If unsure, say N.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment