brintos

brintos / linux-shallow public Read only

0
0
Text · 3.4 KiB · b3593ae Raw
110 lines · plain
1.. SPDX-License-Identifier: GPL-2.02 3=================================4KUnit - Linux Kernel Unit Testing5=================================6 7.. toctree::8	:maxdepth: 29	:caption: Contents:10 11	start12	architecture13	run_wrapper14	run_manual15	usage16	api/index17	style18	faq19	running_tips20 21This section details the kernel unit testing framework.22 23Introduction24============25 26KUnit (Kernel unit testing framework) provides a common framework for27unit tests within the Linux kernel. Using KUnit, you can define groups28of test cases called test suites. The tests either run on kernel boot29if built-in, or load as a module. KUnit automatically flags and reports30failed test cases in the kernel log. The test results appear in31:doc:`KTAP (Kernel - Test Anything Protocol) format</dev-tools/ktap>`.32It is inspired by JUnit, Python’s unittest.mock, and GoogleTest/GoogleMock33(C++ unit testing framework).34 35KUnit tests are part of the kernel, written in the C (programming)36language, and test parts of the Kernel implementation (example: a C37language function). Excluding build time, from invocation to38completion, KUnit can run around 100 tests in less than 10 seconds.39KUnit can test any kernel component, for example: file system, system40calls, memory management, device drivers and so on.41 42KUnit follows the white-box testing approach. The test has access to43internal system functionality. KUnit runs in kernel space and is not44restricted to things exposed to user-space.45 46In addition, KUnit has kunit_tool, a script (``tools/testing/kunit/kunit.py``)47that configures the Linux kernel, runs KUnit tests under QEMU or UML48(:doc:`User Mode Linux </virt/uml/user_mode_linux_howto_v2>`),49parses the test results and50displays them in a user friendly manner.51 52Features53--------54 55- Provides a framework for writing unit tests.56- Runs tests on any kernel architecture.57- Runs a test in milliseconds.58 59Prerequisites60-------------61 62- Any Linux kernel compatible hardware.63- For Kernel under test, Linux kernel version 5.5 or greater.64 65Unit Testing66============67 68A unit test tests a single unit of code in isolation. A unit test is the finest69granularity of testing and allows all possible code paths to be tested in the70code under test. This is possible if the code under test is small and does not71have any external dependencies outside of the test's control like hardware.72 73 74Write Unit Tests75----------------76 77To write good unit tests, there is a simple but powerful pattern:78Arrange-Act-Assert. This is a great way to structure test cases and79defines an order of operations.80 81- Arrange inputs and targets: At the start of the test, arrange the data82  that allows a function to work. Example: initialize a statement or83  object.84- Act on the target behavior: Call your function/code under test.85- Assert expected outcome: Verify that the result (or resulting state) is as86  expected.87 88Unit Testing Advantages89-----------------------90 91- Increases testing speed and development in the long run.92- Detects bugs at initial stage and therefore decreases bug fix cost93  compared to acceptance testing.94- Improves code quality.95- Encourages writing testable code.96 97Read also :ref:`kinds-of-tests`.98 99How do I use it?100================101 102You can find a step-by-step guide to writing and running KUnit tests in103Documentation/dev-tools/kunit/start.rst104 105Alternatively, feel free to look through the rest of the KUnit documentation,106or to experiment with tools/testing/kunit/kunit.py and the example test under107lib/kunit/kunit-example-test.c108 109Happy testing!110