Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame^] | 1 | .. SPDX-License-Identifier: GPL-2.0 |
| 2 | |
| 3 | ========================================= |
| 4 | KUnit - Unit Testing for the Linux Kernel |
| 5 | ========================================= |
| 6 | |
| 7 | .. toctree:: |
| 8 | :maxdepth: 2 |
| 9 | |
| 10 | start |
| 11 | usage |
| 12 | api/index |
| 13 | faq |
| 14 | |
| 15 | What is KUnit? |
| 16 | ============== |
| 17 | |
| 18 | KUnit is a lightweight unit testing and mocking framework for the Linux kernel. |
| 19 | These tests are able to be run locally on a developer's workstation without a VM |
| 20 | or special hardware. |
| 21 | |
| 22 | KUnit is heavily inspired by JUnit, Python's unittest.mock, and |
| 23 | Googletest/Googlemock for C++. KUnit provides facilities for defining unit test |
| 24 | cases, grouping related test cases into test suites, providing common |
| 25 | infrastructure for running tests, and much more. |
| 26 | |
| 27 | Get started now: :doc:`start` |
| 28 | |
| 29 | Why KUnit? |
| 30 | ========== |
| 31 | |
| 32 | A unit test is supposed to test a single unit of code in isolation, hence the |
| 33 | name. A unit test should be the finest granularity of testing and as such should |
| 34 | allow all possible code paths to be tested in the code under test; this is only |
| 35 | possible if the code under test is very small and does not have any external |
| 36 | dependencies outside of the test's control like hardware. |
| 37 | |
| 38 | Outside of KUnit, there are no testing frameworks currently |
| 39 | available for the kernel that do not require installing the kernel on a test |
| 40 | machine or in a VM and all require tests to be written in userspace running on |
| 41 | the kernel; this is true for Autotest, and kselftest, disqualifying |
| 42 | any of them from being considered unit testing frameworks. |
| 43 | |
| 44 | KUnit addresses the problem of being able to run tests without needing a virtual |
| 45 | machine or actual hardware with User Mode Linux. User Mode Linux is a Linux |
| 46 | architecture, like ARM or x86; however, unlike other architectures it compiles |
| 47 | to a standalone program that can be run like any other program directly inside |
| 48 | of a host operating system; to be clear, it does not require any virtualization |
| 49 | support; it is just a regular program. |
| 50 | |
| 51 | KUnit is fast. Excluding build time, from invocation to completion KUnit can run |
| 52 | several dozen tests in only 10 to 20 seconds; this might not sound like a big |
| 53 | deal to some people, but having such fast and easy to run tests fundamentally |
| 54 | changes the way you go about testing and even writing code in the first place. |
| 55 | Linus himself said in his `git talk at Google |
| 56 | <https://gist.github.com/lorn/1272686/revisions#diff-53c65572127855f1b003db4064a94573R874>`_: |
| 57 | |
| 58 | "... a lot of people seem to think that performance is about doing the |
| 59 | same thing, just doing it faster, and that is not true. That is not what |
| 60 | performance is all about. If you can do something really fast, really |
| 61 | well, people will start using it differently." |
| 62 | |
| 63 | In this context Linus was talking about branching and merging, |
| 64 | but this point also applies to testing. If your tests are slow, unreliable, are |
| 65 | difficult to write, and require a special setup or special hardware to run, |
| 66 | then you wait a lot longer to write tests, and you wait a lot longer to run |
| 67 | tests; this means that tests are likely to break, unlikely to test a lot of |
| 68 | things, and are unlikely to be rerun once they pass. If your tests are really |
| 69 | fast, you run them all the time, every time you make a change, and every time |
| 70 | someone sends you some code. Why trust that someone ran all their tests |
| 71 | correctly on every change when you can just run them yourself in less time than |
| 72 | it takes to read their test log? |
| 73 | |
| 74 | How do I use it? |
| 75 | ================ |
| 76 | |
| 77 | * :doc:`start` - for new users of KUnit |
| 78 | * :doc:`usage` - for a more detailed explanation of KUnit features |
| 79 | * :doc:`api/index` - for the list of KUnit APIs used for testing |