Commit b3606442 authored by Harinder Singh's avatar Harinder Singh Committed by Jonathan Corbet

Documentation: KUnit: Restyled Frequently Asked Questions

Reword to align with other chapters.
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-8-sharinder@google.comSigned-off-by: default avatarJonathan Corbet <corbet@lwn.net>
parent 39150e80
...@@ -4,56 +4,55 @@ ...@@ -4,56 +4,55 @@
Frequently Asked Questions Frequently Asked Questions
========================== ==========================
How is this different from Autotest, kselftest, etc? How is this different from Autotest, kselftest, and so on?
==================================================== ==========================================================
KUnit is a unit testing framework. Autotest, kselftest (and some others) are KUnit is a unit testing framework. Autotest, kselftest (and some others) are
not. not.
A `unit test <https://martinfowler.com/bliki/UnitTest.html>`_ is supposed to A `unit test <https://martinfowler.com/bliki/UnitTest.html>`_ is supposed to
test a single unit of code in isolation, hence the name. A unit test should be test a single unit of code in isolation and hence the name *unit test*. A unit
the finest granularity of testing and as such should allow all possible code test should be the finest granularity of testing and should allow all possible
paths to be tested in the code under test; this is only possible if the code code paths to be tested in the code under test. This is only possible if the
under test is very small and does not have any external dependencies outside of code under test is small and does not have any external dependencies outside of
the test's control like hardware. the test's control like hardware.
There are no testing frameworks currently available for the kernel that do not There are no testing frameworks currently available for the kernel that do not
require installing the kernel on a test machine or in a VM and all require require installing the kernel on a test machine or in a virtual machine. All
tests to be written in userspace and run on the kernel under test; this is true testing frameworks require tests to be written in userspace and run on the
for Autotest, kselftest, and some others, disqualifying any of them from being kernel under test. This is true for Autotest, kselftest, and some others,
considered unit testing frameworks. disqualifying any of them from being considered unit testing frameworks.
Does KUnit support running on architectures other than UML? Does KUnit support running on architectures other than UML?
=========================================================== ===========================================================
Yes, well, mostly. Yes, mostly.
For the most part, the KUnit core framework (what you use to write the tests) For the most part, the KUnit core framework (what we use to write the tests)
can compile to any architecture; it compiles like just another part of the can compile to any architecture. It compiles like just another part of the
kernel and runs when the kernel boots, or when built as a module, when the kernel and runs when the kernel boots, or when built as a module, when the
module is loaded. However, there is some infrastructure, module is loaded. However, there is infrastructure, like the KUnit Wrapper
like the KUnit Wrapper (``tools/testing/kunit/kunit.py``) that does not support (``tools/testing/kunit/kunit.py``) that does not support other architectures.
other architectures.
In short, this means that, yes, you can run KUnit on other architectures, but In short, yes, you can run KUnit on other architectures, but it might require
it might require more work than using KUnit on UML. more work than using KUnit on UML.
For more information, see :ref:`kunit-on-non-uml`. For more information, see :ref:`kunit-on-non-uml`.
What is the difference between a unit test and these other kinds of tests? What is the difference between a unit test and other kinds of tests?
========================================================================== ====================================================================
Most existing tests for the Linux kernel would be categorized as an integration Most existing tests for the Linux kernel would be categorized as an integration
test, or an end-to-end test. test, or an end-to-end test.
- A unit test is supposed to test a single unit of code in isolation, hence the - A unit test is supposed to test a single unit of code in isolation. A unit
name. A unit test should be the finest granularity of testing and as such test should be the finest granularity of testing and, as such, allows all
should allow all possible code paths to be tested in the code under test; this possible code paths to be tested in the code under test. This is only possible
is only possible if the code under test is very small and does not have any if the code under test is small and does not have any external dependencies
external dependencies outside of the test's control like hardware. outside of the test's control like hardware.
- An integration test tests the interaction between a minimal set of components, - An integration test tests the interaction between a minimal set of components,
usually just two or three. For example, someone might write an integration usually just two or three. For example, someone might write an integration
test to test the interaction between a driver and a piece of hardware, or to test to test the interaction between a driver and a piece of hardware, or to
test the interaction between the userspace libraries the kernel provides and test the interaction between the userspace libraries the kernel provides and
the kernel itself; however, one of these tests would probably not test the the kernel itself. However, one of these tests would probably not test the
entire kernel along with hardware interactions and interactions with the entire kernel along with hardware interactions and interactions with the
userspace. userspace.
- An end-to-end test usually tests the entire system from the perspective of the - An end-to-end test usually tests the entire system from the perspective of the
...@@ -62,26 +61,26 @@ test, or an end-to-end test. ...@@ -62,26 +61,26 @@ test, or an end-to-end test.
hardware with a production userspace and then trying to exercise some behavior hardware with a production userspace and then trying to exercise some behavior
that depends on interactions between the hardware, the kernel, and userspace. that depends on interactions between the hardware, the kernel, and userspace.
KUnit isn't working, what should I do? KUnit is not working, what should I do?
====================================== =======================================
Unfortunately, there are a number of things which can break, but here are some Unfortunately, there are a number of things which can break, but here are some
things to try. things to try.
1. Try running ``./tools/testing/kunit/kunit.py run`` with the ``--raw_output`` 1. Run ``./tools/testing/kunit/kunit.py run`` with the ``--raw_output``
parameter. This might show details or error messages hidden by the kunit_tool parameter. This might show details or error messages hidden by the kunit_tool
parser. parser.
2. Instead of running ``kunit.py run``, try running ``kunit.py config``, 2. Instead of running ``kunit.py run``, try running ``kunit.py config``,
``kunit.py build``, and ``kunit.py exec`` independently. This can help track ``kunit.py build``, and ``kunit.py exec`` independently. This can help track
down where an issue is occurring. (If you think the parser is at fault, you down where an issue is occurring. (If you think the parser is at fault, you
can run it manually against stdin or a file with ``kunit.py parse``.) can run it manually against ``stdin`` or a file with ``kunit.py parse``.)
3. Running the UML kernel directly can often reveal issues or error messages 3. Running the UML kernel directly can often reveal issues or error messages,
kunit_tool ignores. This should be as simple as running ``./vmlinux`` after ``kunit_tool`` ignores. This should be as simple as running ``./vmlinux``
building the UML kernel (e.g., by using ``kunit.py build``). Note that UML after building the UML kernel (for example, by using ``kunit.py build``).
has some unusual requirements (such as the host having a tmpfs filesystem Note that UML has some unusual requirements (such as the host having a tmpfs
mounted), and has had issues in the past when built statically and the host filesystem mounted), and has had issues in the past when built statically and
has KASLR enabled. (On older host kernels, you may need to run ``setarch the host has KASLR enabled. (On older host kernels, you may need to run
`uname -m` -R ./vmlinux`` to disable KASLR.) ``setarch `uname -m` -R ./vmlinux`` to disable KASLR.)
4. Make sure the kernel .config has ``CONFIG_KUNIT=y`` and at least one test 4. Make sure the kernel .config has ``CONFIG_KUNIT=y`` and at least one test
(e.g. ``CONFIG_KUNIT_EXAMPLE_TEST=y``). kunit_tool will keep its .config (e.g. ``CONFIG_KUNIT_EXAMPLE_TEST=y``). kunit_tool will keep its .config
around, so you can see what config was used after running ``kunit.py run``. around, so you can see what config was used after running ``kunit.py run``.
......
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