Commit 6c6213f4 authored by Harinder Singh's avatar Harinder Singh Committed by Jonathan Corbet

Documentation: KUnit: Rewrite main page

Add a section on advantages of unit testing, how to write unit tests,
KUnit features and Prerequisites.
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-2-sharinder@google.comSigned-off-by: default avatarJonathan Corbet <corbet@lwn.net>
parent 422d98c1
.. SPDX-License-Identifier: GPL-2.0 .. SPDX-License-Identifier: GPL-2.0
========================================= =================================
KUnit - Unit Testing for the Linux Kernel KUnit - Linux Kernel Unit Testing
========================================= =================================
.. toctree:: .. toctree::
:maxdepth: 2 :maxdepth: 2
:caption: Contents:
start start
usage usage
...@@ -16,82 +17,91 @@ KUnit - Unit Testing for the Linux Kernel ...@@ -16,82 +17,91 @@ KUnit - Unit Testing for the Linux Kernel
tips tips
running_tips running_tips
What is KUnit? This section details the kernel unit testing framework.
==============
Introduction
KUnit is a lightweight unit testing and mocking framework for the Linux kernel. ============
KUnit is heavily inspired by JUnit, Python's unittest.mock, and KUnit (Kernel unit testing framework) provides a common framework for
Googletest/Googlemock for C++. KUnit provides facilities for defining unit test unit tests within the Linux kernel. Using KUnit, you can define groups
cases, grouping related test cases into test suites, providing common of test cases called test suites. The tests either run on kernel boot
infrastructure for running tests, and much more. if built-in, or load as a module. KUnit automatically flags and reports
failed test cases in the kernel log. The test results appear in `TAP
KUnit consists of a kernel component, which provides a set of macros for easily (Test Anything Protocol) format <https://testanything.org/>`_. It is inspired by
writing unit tests. Tests written against KUnit will run on kernel boot if JUnit, Python’s unittest.mock, and GoogleTest/GoogleMock (C++ unit testing
built-in, or when loaded if built as a module. These tests write out results to framework).
the kernel log in `TAP <https://testanything.org/>`_ format.
KUnit tests are part of the kernel, written in the C (programming)
To make running these tests (and reading the results) easier, KUnit offers language, and test parts of the Kernel implementation (example: a C
:doc:`kunit_tool <kunit-tool>`, which builds a `User Mode Linux language function). Excluding build time, from invocation to
<http://user-mode-linux.sourceforge.net>`_ kernel, runs it, and parses the test completion, KUnit can run around 100 tests in less than 10 seconds.
results. This provides a quick way of running KUnit tests during development, KUnit can test any kernel component, for example: file system, system
without requiring a virtual machine or separate hardware. calls, memory management, device drivers and so on.
Get started now: Documentation/dev-tools/kunit/start.rst KUnit follows the white-box testing approach. The test has access to
internal system functionality. KUnit runs in kernel space and is not
Why KUnit? restricted to things exposed to user-space.
==========
In addition, KUnit has kunit_tool, a script (``tools/testing/kunit/kunit.py``)
A unit test is supposed to test a single unit of code in isolation, hence the that configures the Linux kernel, runs KUnit tests under QEMU or UML (`User Mode
name. A unit test should be the finest granularity of testing and as such should Linux <http://user-mode-linux.sourceforge.net/>`_), parses the test results and
allow all possible code paths to be tested in the code under test; this is only displays them in a user friendly manner.
possible if the code under test is very small and does not have any external
dependencies outside of the test's control like hardware. Features
--------
KUnit provides a common framework for unit tests within the kernel.
- Provides a framework for writing unit tests.
KUnit tests can be run on most architectures, and most tests are architecture - Runs tests on any kernel architecture.
independent. All built-in KUnit tests run on kernel startup. Alternatively, - Runs a test in milliseconds.
KUnit and KUnit tests can be built as modules and tests will run when the test
module is loaded. Prerequisites
-------------
.. note::
- Any Linux kernel compatible hardware.
KUnit can also run tests without needing a virtual machine or actual - For Kernel under test, Linux kernel version 5.5 or greater.
hardware under User Mode Linux. User Mode Linux is a Linux architecture,
like ARM or x86, which compiles the kernel as a Linux executable. KUnit Unit Testing
can be used with UML either by building with ``ARCH=um`` (like any other ============
architecture), or by using :doc:`kunit_tool <kunit-tool>`.
A unit test tests a single unit of code in isolation. A unit test is the finest
KUnit is fast. Excluding build time, from invocation to completion KUnit can run granularity of testing and allows all possible code paths to be tested in the
several dozen tests in only 10 to 20 seconds; this might not sound like a big code under test. This is possible if the code under test is small and does not
deal to some people, but having such fast and easy to run tests fundamentally have any external dependencies outside of the test's control like hardware.
changes the way you go about testing and even writing code in the first place.
Linus himself said in his `git talk at Google
<https://gist.github.com/lorn/1272686/revisions#diff-53c65572127855f1b003db4064a94573R874>`_: Write Unit Tests
----------------
"... a lot of people seem to think that performance is about doing the
same thing, just doing it faster, and that is not true. That is not what To write good unit tests, there is a simple but powerful pattern:
performance is all about. If you can do something really fast, really Arrange-Act-Assert. This is a great way to structure test cases and
well, people will start using it differently." defines an order of operations.
In this context Linus was talking about branching and merging, - Arrange inputs and targets: At the start of the test, arrange the data
but this point also applies to testing. If your tests are slow, unreliable, are that allows a function to work. Example: initialize a statement or
difficult to write, and require a special setup or special hardware to run, object.
then you wait a lot longer to write tests, and you wait a lot longer to run - Act on the target behavior: Call your function/code under test.
tests; this means that tests are likely to break, unlikely to test a lot of - Assert expected outcome: Verify that the result (or resulting state) is as
things, and are unlikely to be rerun once they pass. If your tests are really expected.
fast, you run them all the time, every time you make a change, and every time
someone sends you some code. Why trust that someone ran all their tests Unit Testing Advantages
correctly on every change when you can just run them yourself in less time than -----------------------
it takes to read their test log?
- Increases testing speed and development in the long run.
- Detects bugs at initial stage and therefore decreases bug fix cost
compared to acceptance testing.
- Improves code quality.
- Encourages writing testable code.
How do I use it? How do I use it?
================ ================
* Documentation/dev-tools/kunit/start.rst - for new users of KUnit * Documentation/dev-tools/kunit/start.rst - for KUnit new users.
* Documentation/dev-tools/kunit/tips.rst - for short examples of best practices * Documentation/dev-tools/kunit/usage.rst - KUnit features.
* Documentation/dev-tools/kunit/usage.rst - for a more detailed explanation of KUnit features * Documentation/dev-tools/kunit/tips.rst - best practices with
* Documentation/dev-tools/kunit/api/index.rst - for the list of KUnit APIs used for testing examples.
* Documentation/dev-tools/kunit/kunit-tool.rst - for more information on the kunit_tool helper script * Documentation/dev-tools/kunit/api/index.rst - KUnit APIs
* Documentation/dev-tools/kunit/faq.rst - for answers to some common questions about KUnit used for testing.
* Documentation/dev-tools/kunit/kunit-tool.rst - kunit_tool helper
script.
* Documentation/dev-tools/kunit/faq.rst - KUnit common questions and
answers.
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