← Documents Documentation/dev-tools/kunit/index.rst GitHub 원문 ↗

Linux 6.18.37 · Dev Tools

KUnit - Linux Kernel Unit Testing

KUnit의 kernel-space unit test 모델, 기능·요구 사항, Arrange-Act-Assert 작성 패턴과 시작 경로를 소개합니다.

Source pathDocumentation/dev-tools/kunit/index.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약과 해설

index.rst:1-109

KUnit은 C로 작성한 white-box test를 kernel 내부에서 실행하고 suite 단위로 구성해 KTAP 결과를 남깁니다. Built-in, module, native architecture, QEMU와 UML 실행을 모두 지원하며 kunit_tool이 구성과 결과 parsing을 자동화합니다.

좋은 test case는 외부 dependency를 통제하고 Arrange-Act-Assert 순서로 입력과 state를 준비한 뒤 동작을 실행하고 결과를 검증합니다. 세밀한 unit test는 빠른 회귀 탐지와 testable code 설계를 촉진합니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

원문 전체 펼치기
1 .. SPDX-License-Identifier: GPL-2.0
2
3 =================================
4 KUnit - Linux Kernel Unit Testing
5 =================================
6
7 .. toctree::
8 :maxdepth: 2
9 :caption: Contents:
10
11 start
12 architecture
13 run_wrapper
14 run_manual
15 usage
16 api/index
17 style
18 faq
19 running_tips
20
21 This section details the kernel unit testing framework.
22
23 Introduction
24 ============
25
26 KUnit (Kernel unit testing framework) provides a common framework for
27 unit tests within the Linux kernel. Using KUnit, you can define groups
28 of test cases called test suites. The tests either run on kernel boot
29 if built-in, or load as a module. KUnit automatically flags and reports
30 failed test cases in the kernel log. The test results appear in
31 :doc:`KTAP (Kernel - Test Anything Protocol) format</dev-tools/ktap>`.
32 It is inspired by JUnit, Python’s unittest.mock, and GoogleTest/GoogleMock
33 (C++ unit testing framework).
34
35 KUnit tests are part of the kernel, written in the C (programming)
36 language, and test parts of the Kernel implementation (example: a C
37 language function). Excluding build time, from invocation to
38 completion, KUnit can run around 100 tests in less than 10 seconds.
39 KUnit can test any kernel component, for example: file system, system
40 calls, memory management, device drivers and so on.
41
42 KUnit follows the white-box testing approach. The test has access to
43 internal system functionality. KUnit runs in kernel space and is not
44 restricted to things exposed to user-space.
45
46 In addition, KUnit has kunit_tool, a script (``tools/testing/kunit/kunit.py``)
47 that configures the Linux kernel, runs KUnit tests under QEMU or UML
48 (:doc:`User Mode Linux </virt/uml/user_mode_linux_howto_v2>`),
49 parses the test results and
50 displays them in a user friendly manner.
51
52 Features
53 --------
54
55 - Provides a framework for writing unit tests.
56 - Runs tests on any kernel architecture.
57 - Runs a test in milliseconds.
58
59 Prerequisites
60 -------------
61
62 - Any Linux kernel compatible hardware.
63 - For Kernel under test, Linux kernel version 5.5 or greater.
64
65 Unit Testing
66 ============
67
68 A unit test tests a single unit of code in isolation. A unit test is the finest
69 granularity of testing and allows all possible code paths to be tested in the
70 code under test. This is possible if the code under test is small and does not
71 have any external dependencies outside of the test's control like hardware.
72
73
74 Write Unit Tests
75 ----------------
76
77 To write good unit tests, there is a simple but powerful pattern:
78 Arrange-Act-Assert. This is a great way to structure test cases and
79 defines an order of operations.
80
81 - Arrange inputs and targets: At the start of the test, arrange the data
82 that allows a function to work. Example: initialize a statement or
83 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 as
86 expected.
87
88 Unit Testing Advantages
89 -----------------------
90
91 - Increases testing speed and development in the long run.
92 - Detects bugs at initial stage and therefore decreases bug fix cost
93 compared to acceptance testing.
94 - Improves code quality.
95 - Encourages writing testable code.
96
97 Read also :ref:`kinds-of-tests`.
98
99 How do I use it?
100 ================
101
102 You can find a step-by-step guide to writing and running KUnit tests in
103 Documentation/dev-tools/kunit/start.rst
104
105 Alternatively, feel free to look through the rest of the KUnit documentation,
106 or to experiment with tools/testing/kunit/kunit.py and the example test under
107 lib/kunit/kunit-example-test.c
108
109 Happy testing!
110

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

KUnit 문서 구성

1-22

SPDX 라이선스 식별자: GPL-2.0

KUnit - Linux Kernel Unit Testing

원문의 KUnit 문서 toctree는 다음과 같습니다.

.. toctree::
        :maxdepth: 2
        :caption: Contents:

        start
        architecture
        run_wrapper
        run_manual
        usage
        api/index
        style
        faq
        running_tips

이 절은 kernel unit testing framework를 자세히 설명합니다.

KUnit 소개, 기능과 요구 사항

23-64

소개

KUnit, 즉 Kernel unit testing framework는 Linux kernel 안의 unit test를 위한 공통 framework입니다. KUnit을 사용하면 test case를 test suite라는 그룹으로 정의할 수 있습니다. Built-in 테스트는 kernel boot 때 실행되고 module 테스트는 module을 load할 때 실행됩니다.

KUnit은 실패한 test case를 자동으로 표시하고 kernel log에 보고합니다. Test result는 `KTAP (Kernel - Test Anything Protocol) format</dev-tools/ktap>`으로 나타납니다. JUnit, Python의 unittest.mock, GoogleTest와 GoogleMock에서 영감을 받았습니다.

KUnit test는 kernel의 일부이며 C language로 작성되어 C function 같은 kernel 구현 요소를 검사합니다. Build 시간을 제외하면 호출부터 완료까지 10초 미만에 약 100개 테스트를 실행할 수 있습니다. File system, system call, memory management, device driver 등 어떤 kernel component도 검사할 수 있습니다.

KUnit은 white-box testing 접근법을 따릅니다. Test가 internal system 기능에 접근하며 kernel space에서 실행되므로 userspace에 노출된 기능으로 제한되지 않습니다.

또한 `tools/testing/kunit/kunit.py`의 kunit_tool script가 있습니다. Linux kernel을 구성하고 QEMU 또는 UML, 즉 `User Mode Linux </virt/uml/user_mode_linux_howto_v2>`에서 KUnit test를 실행하며 결과를 parsing해 사용자가 읽기 쉽게 표시합니다.

기능

Unit test 작성을 위한 framework를 제공합니다.

모든 kernel architecture에서 테스트를 실행합니다.

하나의 테스트를 millisecond 단위로 실행합니다.

사전 요구 사항

Linux kernel과 호환되는 hardware가 필요합니다.

검사 대상 kernel은 Linux kernel version 5.5 이상이어야 합니다.

KUnit 실행 특성
항목내용
실행 위치Kernel space, built-in boot 또는 module load
작성 언어C
결과 형식KTAP
실행 환경Native architecture, QEMU 또는 UML
지원 kernelVersion 5.5 이상

Framework의 실행 위치, 형식과 기본 요구 사항을 정리했습니다.

Unit test 작성 원칙과 장점

65-98

Unit Testing

Unit test는 단일 코드 단위를 격리해 검사합니다. 가장 세밀한 testing granularity로 검사 대상 코드의 모든 code path를 실행할 수 있습니다. 검사 대상 코드가 작고 hardware처럼 테스트 통제 밖의 external dependency가 없을 때 가능합니다.

Unit Test 작성

좋은 unit test를 작성하는 간단하고 강력한 패턴은 Arrange-Act-Assert입니다. Test case를 구성하고 작업 순서를 명확히 하는 방법입니다.

Arrange inputs and targets: 테스트 시작 시 function이 동작하는 데 필요한 data를 준비합니다. Statement나 object를 초기화하는 것이 예입니다.

Act on the target behavior: 검사할 function 또는 code를 호출합니다.

Assert expected outcome: 결과 또는 결과 state가 예상과 같은지 확인합니다.

Arrange-Act-Assert
ArrangeInput, object와 test state 준비
Act검사 대상 function 또는 code 실행
Assert결과와 resulting state 검증

KUnit test case를 준비, 실행, 검증의 세 단계로 나누는 기본 패턴입니다.

Unit Testing의 장점

장기적으로 testing과 development 속도를 높입니다.

Bug를 초기 단계에 발견하므로 acceptance testing에 비해 수정 비용을 낮춥니다.

Code 품질을 높입니다.

검사하기 쉬운 code 작성을 장려합니다.

다른 test 종류와의 차이는 `kinds-of-tests` 참조를 읽으십시오.

시작 방법

99-109

어떻게 사용하나요?

KUnit test를 작성하고 실행하는 단계별 guide는 `Documentation/dev-tools/kunit/start.rst`에서 볼 수 있습니다.

나머지 KUnit 문서를 살펴보거나 `tools/testing/kunit/kunit.py`와 `lib/kunit/kunit-example-test.c`의 example test를 직접 실험해도 됩니다.

즐겁게 테스트하십시오.