xref: /OK3568_Linux_fs/kernel/Documentation/dev-tools/kunit/faq.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun==========================
4*4882a593SmuzhiyunFrequently Asked Questions
5*4882a593Smuzhiyun==========================
6*4882a593Smuzhiyun
7*4882a593SmuzhiyunHow is this different from Autotest, kselftest, etc?
8*4882a593Smuzhiyun====================================================
9*4882a593SmuzhiyunKUnit is a unit testing framework. Autotest, kselftest (and some others) are
10*4882a593Smuzhiyunnot.
11*4882a593Smuzhiyun
12*4882a593SmuzhiyunA `unit test <https://martinfowler.com/bliki/UnitTest.html>`_ is supposed to
13*4882a593Smuzhiyuntest a single unit of code in isolation, hence the name. A unit test should be
14*4882a593Smuzhiyunthe finest granularity of testing and as such should allow all possible code
15*4882a593Smuzhiyunpaths to be tested in the code under test; this is only possible if the code
16*4882a593Smuzhiyununder test is very small and does not have any external dependencies outside of
17*4882a593Smuzhiyunthe test's control like hardware.
18*4882a593Smuzhiyun
19*4882a593SmuzhiyunThere are no testing frameworks currently available for the kernel that do not
20*4882a593Smuzhiyunrequire installing the kernel on a test machine or in a VM and all require
21*4882a593Smuzhiyuntests to be written in userspace and run on the kernel under test; this is true
22*4882a593Smuzhiyunfor Autotest, kselftest, and some others, disqualifying any of them from being
23*4882a593Smuzhiyunconsidered unit testing frameworks.
24*4882a593Smuzhiyun
25*4882a593SmuzhiyunDoes KUnit support running on architectures other than UML?
26*4882a593Smuzhiyun===========================================================
27*4882a593Smuzhiyun
28*4882a593SmuzhiyunYes, well, mostly.
29*4882a593Smuzhiyun
30*4882a593SmuzhiyunFor the most part, the KUnit core framework (what you use to write the tests)
31*4882a593Smuzhiyuncan compile to any architecture; it compiles like just another part of the
32*4882a593Smuzhiyunkernel and runs when the kernel boots, or when built as a module, when the
33*4882a593Smuzhiyunmodule is loaded.  However, there is some infrastructure,
34*4882a593Smuzhiyunlike the KUnit Wrapper (``tools/testing/kunit/kunit.py``) that does not support
35*4882a593Smuzhiyunother architectures.
36*4882a593Smuzhiyun
37*4882a593SmuzhiyunIn short, this means that, yes, you can run KUnit on other architectures, but
38*4882a593Smuzhiyunit might require more work than using KUnit on UML.
39*4882a593Smuzhiyun
40*4882a593SmuzhiyunFor more information, see :ref:`kunit-on-non-uml`.
41*4882a593Smuzhiyun
42*4882a593SmuzhiyunWhat is the difference between a unit test and these other kinds of tests?
43*4882a593Smuzhiyun==========================================================================
44*4882a593SmuzhiyunMost existing tests for the Linux kernel would be categorized as an integration
45*4882a593Smuzhiyuntest, or an end-to-end test.
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun- A unit test is supposed to test a single unit of code in isolation, hence the
48*4882a593Smuzhiyun  name. A unit test should be the finest granularity of testing and as such
49*4882a593Smuzhiyun  should allow all possible code paths to be tested in the code under test; this
50*4882a593Smuzhiyun  is only possible if the code under test is very small and does not have any
51*4882a593Smuzhiyun  external dependencies outside of the test's control like hardware.
52*4882a593Smuzhiyun- An integration test tests the interaction between a minimal set of components,
53*4882a593Smuzhiyun  usually just two or three. For example, someone might write an integration
54*4882a593Smuzhiyun  test to test the interaction between a driver and a piece of hardware, or to
55*4882a593Smuzhiyun  test the interaction between the userspace libraries the kernel provides and
56*4882a593Smuzhiyun  the kernel itself; however, one of these tests would probably not test the
57*4882a593Smuzhiyun  entire kernel along with hardware interactions and interactions with the
58*4882a593Smuzhiyun  userspace.
59*4882a593Smuzhiyun- An end-to-end test usually tests the entire system from the perspective of the
60*4882a593Smuzhiyun  code under test. For example, someone might write an end-to-end test for the
61*4882a593Smuzhiyun  kernel by installing a production configuration of the kernel on production
62*4882a593Smuzhiyun  hardware with a production userspace and then trying to exercise some behavior
63*4882a593Smuzhiyun  that depends on interactions between the hardware, the kernel, and userspace.
64*4882a593Smuzhiyun
65*4882a593SmuzhiyunKUnit isn't working, what should I do?
66*4882a593Smuzhiyun======================================
67*4882a593Smuzhiyun
68*4882a593SmuzhiyunUnfortunately, there are a number of things which can break, but here are some
69*4882a593Smuzhiyunthings to try.
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun1. Try running ``./tools/testing/kunit/kunit.py run`` with the ``--raw_output``
72*4882a593Smuzhiyun   parameter. This might show details or error messages hidden by the kunit_tool
73*4882a593Smuzhiyun   parser.
74*4882a593Smuzhiyun2. Instead of running ``kunit.py run``, try running ``kunit.py config``,
75*4882a593Smuzhiyun   ``kunit.py build``, and ``kunit.py exec`` independently. This can help track
76*4882a593Smuzhiyun   down where an issue is occurring. (If you think the parser is at fault, you
77*4882a593Smuzhiyun   can run it manually against stdin or a file with ``kunit.py parse``.)
78*4882a593Smuzhiyun3. Running the UML kernel directly can often reveal issues or error messages
79*4882a593Smuzhiyun   kunit_tool ignores. This should be as simple as running ``./vmlinux`` after
80*4882a593Smuzhiyun   building the UML kernel (e.g., by using ``kunit.py build``). Note that UML
81*4882a593Smuzhiyun   has some unusual requirements (such as the host having a tmpfs filesystem
82*4882a593Smuzhiyun   mounted), and has had issues in the past when built statically and the host
83*4882a593Smuzhiyun   has KASLR enabled. (On older host kernels, you may need to run ``setarch
84*4882a593Smuzhiyun   `uname -m` -R ./vmlinux`` to disable KASLR.)
85*4882a593Smuzhiyun4. Make sure the kernel .config has ``CONFIG_KUNIT=y`` and at least one test
86*4882a593Smuzhiyun   (e.g. ``CONFIG_KUNIT_EXAMPLE_TEST=y``). kunit_tool will keep its .config
87*4882a593Smuzhiyun   around, so you can see what config was used after running ``kunit.py run``.
88*4882a593Smuzhiyun   It also preserves any config changes you might make, so you can
89*4882a593Smuzhiyun   enable/disable things with ``make ARCH=um menuconfig`` or similar, and then
90*4882a593Smuzhiyun   re-run kunit_tool.
91*4882a593Smuzhiyun5. Try to run ``make ARCH=um defconfig`` before running ``kunit.py run``. This
92*4882a593Smuzhiyun   may help clean up any residual config items which could be causing problems.
93*4882a593Smuzhiyun6. Finally, try running KUnit outside UML. KUnit and KUnit tests can be
94*4882a593Smuzhiyun   built into any kernel, or can be built as a module and loaded at runtime.
95*4882a593Smuzhiyun   Doing so should allow you to determine if UML is causing the issue you're
96*4882a593Smuzhiyun   seeing. When tests are built-in, they will execute when the kernel boots, and
97*4882a593Smuzhiyun   modules will automatically execute associated tests when loaded. Test results
98*4882a593Smuzhiyun   can be collected from ``/sys/kernel/debug/kunit/<test suite>/results``, and
99*4882a593Smuzhiyun   can be parsed with ``kunit.py parse``. For more details, see "KUnit on
100*4882a593Smuzhiyun   non-UML architectures" in :doc:`usage`.
101*4882a593Smuzhiyun
102*4882a593SmuzhiyunIf none of the above tricks help, you are always welcome to email any issues to
103*4882a593Smuzhiyunkunit-dev@googlegroups.com.
104