요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.
1. 요약·해설
원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.
2. 영어 원문 전체
번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.
원문 전체 펼치기
.. SPDX-License-Identifier: GPL-2.0
=================================
KUnit - Linux Kernel Unit Testing
=================================
.. toctree::
:maxdepth: 2
:caption: Contents:
start
architecture
run_wrapper
run_manual
usage
api/index
style
faq
running_tips
This section details the kernel unit testing framework.
Introduction
============
KUnit (Kernel unit testing framework) provides a common framework for
unit tests within the Linux kernel. Using KUnit, you can define groups
of test cases called test suites. The tests either run on kernel boot
if built-in, or load as a module. KUnit automatically flags and reports
failed test cases in the kernel log. The test results appear in
:doc:`KTAP (Kernel - Test Anything Protocol) format</dev-tools/ktap>`.
It is inspired by JUnit, Python’s unittest.mock, and GoogleTest/GoogleMock
(C++ unit testing framework).
KUnit tests are part of the kernel, written in the C (programming)
language, and test parts of the Kernel implementation (example: a C
language function). Excluding build time, from invocation to
completion, KUnit can run around 100 tests in less than 10 seconds.
KUnit can test any kernel component, for example: file system, system
calls, memory management, device drivers and so on.
KUnit follows the white-box testing approach. The test has access to
internal system functionality. KUnit runs in kernel space and is not
restricted to things exposed to user-space.
In addition, KUnit has kunit_tool, a script (``tools/testing/kunit/kunit.py``)
that configures the Linux kernel, runs KUnit tests under QEMU or UML
(:doc:`User Mode Linux </virt/uml/user_mode_linux_howto_v2>`),
parses the test results and
displays them in a user friendly manner.
Features
--------
- Provides a framework for writing unit tests.
- Runs tests on any kernel architecture.
- Runs a test in milliseconds.
Prerequisites
-------------
- Any Linux kernel compatible hardware.
- For Kernel under test, Linux kernel version 5.5 or greater.
Unit Testing
============
A unit test tests a single unit of code in isolation. A unit test is the finest
granularity of testing and allows all possible code paths to be tested in the
code under test. This is possible if the code under test is small and does not
have any external dependencies outside of the test's control like hardware.
Write Unit Tests
----------------
To write good unit tests, there is a simple but powerful pattern:
Arrange-Act-Assert. This is a great way to structure test cases and
defines an order of operations.
- Arrange inputs and targets: At the start of the test, arrange the data
that allows a function to work. Example: initialize a statement or
object.
- Act on the target behavior: Call your function/code under test.
- Assert expected outcome: Verify that the result (or resulting state) is as
expected.
Unit Testing Advantages
-----------------------
- Increases testing speed and development in the long run.
- Detects bugs at initial stage and therefore decreases bug fix cost
compared to acceptance testing.
- Improves code quality.
- Encourages writing testable code.
Read also :ref:`kinds-of-tests`.
How do I use it?
================
You can find a step-by-step guide to writing and running KUnit tests in
Documentation/dev-tools/kunit/start.rst
Alternatively, feel free to look through the rest of the KUnit documentation,
or to experiment with tools/testing/kunit/kunit.py and the example test under
lib/kunit/kunit-example-test.c
Happy testing!
3. 한국어 전문 번역
영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.
KUnit 문서 구성
1-22SPDX 라이선스 식별자: 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 이상이어야 합니다.
Framework의 실행 위치, 형식과 기본 요구 사항을 정리했습니다.
Unit test 작성 원칙과 장점
65-98Unit 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가 예상과 같은지 확인합니다.
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를 직접 실험해도 됩니다.
즐겁게 테스트하십시오.
요약과 해설
index.rst:1-109KUnit은 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 설계를 촉진합니다.