18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci==========================
48c2ecf20Sopenharmony_ciFrequently Asked Questions
58c2ecf20Sopenharmony_ci==========================
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ciHow is this different from Autotest, kselftest, etc?
88c2ecf20Sopenharmony_ci====================================================
98c2ecf20Sopenharmony_ciKUnit is a unit testing framework. Autotest, kselftest (and some others) are
108c2ecf20Sopenharmony_cinot.
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ciA `unit test <https://martinfowler.com/bliki/UnitTest.html>`_ is supposed to
138c2ecf20Sopenharmony_citest a single unit of code in isolation, hence the name. A unit test should be
148c2ecf20Sopenharmony_cithe finest granularity of testing and as such should allow all possible code
158c2ecf20Sopenharmony_cipaths to be tested in the code under test; this is only possible if the code
168c2ecf20Sopenharmony_ciunder test is very small and does not have any external dependencies outside of
178c2ecf20Sopenharmony_cithe test's control like hardware.
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ciThere are no testing frameworks currently available for the kernel that do not
208c2ecf20Sopenharmony_cirequire installing the kernel on a test machine or in a VM and all require
218c2ecf20Sopenharmony_citests to be written in userspace and run on the kernel under test; this is true
228c2ecf20Sopenharmony_cifor Autotest, kselftest, and some others, disqualifying any of them from being
238c2ecf20Sopenharmony_ciconsidered unit testing frameworks.
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ciDoes KUnit support running on architectures other than UML?
268c2ecf20Sopenharmony_ci===========================================================
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ciYes, well, mostly.
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ciFor the most part, the KUnit core framework (what you use to write the tests)
318c2ecf20Sopenharmony_cican compile to any architecture; it compiles like just another part of the
328c2ecf20Sopenharmony_cikernel and runs when the kernel boots, or when built as a module, when the
338c2ecf20Sopenharmony_cimodule is loaded.  However, there is some infrastructure,
348c2ecf20Sopenharmony_cilike the KUnit Wrapper (``tools/testing/kunit/kunit.py``) that does not support
358c2ecf20Sopenharmony_ciother architectures.
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ciIn short, this means that, yes, you can run KUnit on other architectures, but
388c2ecf20Sopenharmony_ciit might require more work than using KUnit on UML.
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ciFor more information, see :ref:`kunit-on-non-uml`.
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ciWhat is the difference between a unit test and these other kinds of tests?
438c2ecf20Sopenharmony_ci==========================================================================
448c2ecf20Sopenharmony_ciMost existing tests for the Linux kernel would be categorized as an integration
458c2ecf20Sopenharmony_citest, or an end-to-end test.
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci- A unit test is supposed to test a single unit of code in isolation, hence the
488c2ecf20Sopenharmony_ci  name. A unit test should be the finest granularity of testing and as such
498c2ecf20Sopenharmony_ci  should allow all possible code paths to be tested in the code under test; this
508c2ecf20Sopenharmony_ci  is only possible if the code under test is very small and does not have any
518c2ecf20Sopenharmony_ci  external dependencies outside of the test's control like hardware.
528c2ecf20Sopenharmony_ci- An integration test tests the interaction between a minimal set of components,
538c2ecf20Sopenharmony_ci  usually just two or three. For example, someone might write an integration
548c2ecf20Sopenharmony_ci  test to test the interaction between a driver and a piece of hardware, or to
558c2ecf20Sopenharmony_ci  test the interaction between the userspace libraries the kernel provides and
568c2ecf20Sopenharmony_ci  the kernel itself; however, one of these tests would probably not test the
578c2ecf20Sopenharmony_ci  entire kernel along with hardware interactions and interactions with the
588c2ecf20Sopenharmony_ci  userspace.
598c2ecf20Sopenharmony_ci- An end-to-end test usually tests the entire system from the perspective of the
608c2ecf20Sopenharmony_ci  code under test. For example, someone might write an end-to-end test for the
618c2ecf20Sopenharmony_ci  kernel by installing a production configuration of the kernel on production
628c2ecf20Sopenharmony_ci  hardware with a production userspace and then trying to exercise some behavior
638c2ecf20Sopenharmony_ci  that depends on interactions between the hardware, the kernel, and userspace.
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ciKUnit isn't working, what should I do?
668c2ecf20Sopenharmony_ci======================================
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ciUnfortunately, there are a number of things which can break, but here are some
698c2ecf20Sopenharmony_cithings to try.
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci1. Try running ``./tools/testing/kunit/kunit.py run`` with the ``--raw_output``
728c2ecf20Sopenharmony_ci   parameter. This might show details or error messages hidden by the kunit_tool
738c2ecf20Sopenharmony_ci   parser.
748c2ecf20Sopenharmony_ci2. Instead of running ``kunit.py run``, try running ``kunit.py config``,
758c2ecf20Sopenharmony_ci   ``kunit.py build``, and ``kunit.py exec`` independently. This can help track
768c2ecf20Sopenharmony_ci   down where an issue is occurring. (If you think the parser is at fault, you
778c2ecf20Sopenharmony_ci   can run it manually against stdin or a file with ``kunit.py parse``.)
788c2ecf20Sopenharmony_ci3. Running the UML kernel directly can often reveal issues or error messages
798c2ecf20Sopenharmony_ci   kunit_tool ignores. This should be as simple as running ``./vmlinux`` after
808c2ecf20Sopenharmony_ci   building the UML kernel (e.g., by using ``kunit.py build``). Note that UML
818c2ecf20Sopenharmony_ci   has some unusual requirements (such as the host having a tmpfs filesystem
828c2ecf20Sopenharmony_ci   mounted), and has had issues in the past when built statically and the host
838c2ecf20Sopenharmony_ci   has KASLR enabled. (On older host kernels, you may need to run ``setarch
848c2ecf20Sopenharmony_ci   `uname -m` -R ./vmlinux`` to disable KASLR.)
858c2ecf20Sopenharmony_ci4. Make sure the kernel .config has ``CONFIG_KUNIT=y`` and at least one test
868c2ecf20Sopenharmony_ci   (e.g. ``CONFIG_KUNIT_EXAMPLE_TEST=y``). kunit_tool will keep its .config
878c2ecf20Sopenharmony_ci   around, so you can see what config was used after running ``kunit.py run``.
888c2ecf20Sopenharmony_ci   It also preserves any config changes you might make, so you can
898c2ecf20Sopenharmony_ci   enable/disable things with ``make ARCH=um menuconfig`` or similar, and then
908c2ecf20Sopenharmony_ci   re-run kunit_tool.
918c2ecf20Sopenharmony_ci5. Try to run ``make ARCH=um defconfig`` before running ``kunit.py run``. This
928c2ecf20Sopenharmony_ci   may help clean up any residual config items which could be causing problems.
938c2ecf20Sopenharmony_ci6. Finally, try running KUnit outside UML. KUnit and KUnit tests can be
948c2ecf20Sopenharmony_ci   built into any kernel, or can be built as a module and loaded at runtime.
958c2ecf20Sopenharmony_ci   Doing so should allow you to determine if UML is causing the issue you're
968c2ecf20Sopenharmony_ci   seeing. When tests are built-in, they will execute when the kernel boots, and
978c2ecf20Sopenharmony_ci   modules will automatically execute associated tests when loaded. Test results
988c2ecf20Sopenharmony_ci   can be collected from ``/sys/kernel/debug/kunit/<test suite>/results``, and
998c2ecf20Sopenharmony_ci   can be parsed with ``kunit.py parse``. For more details, see "KUnit on
1008c2ecf20Sopenharmony_ci   non-UML architectures" in :doc:`usage`.
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ciIf none of the above tricks help, you are always welcome to email any issues to
1038c2ecf20Sopenharmony_cikunit-dev@googlegroups.com.
104