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

Linux 6.18.37 · Dev Tools

Test Style and Nomenclature

KUnit subsystem, suite, test case, Kconfig entry, module과 source file의 일관된 명명 규칙을 설명합니다.

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

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

1. 요약·해설

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

요약과 해설

style.rst:1-213

KUnit 명명 규칙은 사람이 테스트를 쉽게 찾도록 할 뿐 아니라 tooling과 자동 testing system이 suite를 안정적으로 발견하고 실행하게 합니다. Subsystem은 module이나 kernel 영역, suite는 그 안의 세부 기능, case는 구체적인 codepath나 input을 나타냅니다.

Kconfig는 `CONFIG_<name>_KUNIT_TEST`, module은 `<suite>_kunit`, source는 `tests/<suite>_kunit.c` 형식을 기본으로 합니다. 이름의 중복과 모호함을 피하고 underscore를 일관되게 사용하는 것이 핵심입니다.

2. 영어 원문 전체

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

원문 전체 펼치기
1 .. SPDX-License-Identifier: GPL-2.0
2
3 ===========================
4 Test Style and Nomenclature
5 ===========================
6
7 To make finding, writing, and using KUnit tests as simple as possible, it is
8 strongly encouraged that they are named and written according to the guidelines
9 below. While it is possible to write KUnit tests which do not follow these rules,
10 they may break some tooling, may conflict with other tests, and may not be run
11 automatically by testing systems.
12
13 It is recommended that you only deviate from these guidelines when:
14
15 1. Porting tests to KUnit which are already known with an existing name.
16 2. Writing tests which would cause serious problems if automatically run. For
17 example, non-deterministically producing false positives or negatives, or
18 taking a long time to run.
19
20 Subsystems, Suites, and Tests
21 =============================
22
23 To make tests easy to find, they are grouped into suites and subsystems. A test
24 suite is a group of tests which test a related area of the kernel. A subsystem
25 is a set of test suites which test different parts of a kernel subsystem
26 or a driver.
27
28 Subsystems
29 ----------
30
31 Every test suite must belong to a subsystem. A subsystem is a collection of one
32 or more KUnit test suites which test the same driver or part of the kernel. A
33 test subsystem should match a single kernel module. If the code being tested
34 cannot be compiled as a module, in many cases the subsystem should correspond to
35 a directory in the source tree or an entry in the ``MAINTAINERS`` file. If
36 unsure, follow the conventions set by tests in similar areas.
37
38 Test subsystems should be named after the code being tested, either after the
39 module (wherever possible), or after the directory or files being tested. Test
40 subsystems should be named to avoid ambiguity where necessary.
41
42 If a test subsystem name has multiple components, they should be separated by
43 underscores. *Do not* include "test" or "kunit" directly in the subsystem name
44 unless we are actually testing other tests or the kunit framework itself. For
45 example, subsystems could be called:
46
47 ``ext4``
48 Matches the module and filesystem name.
49 ``apparmor``
50 Matches the module name and LSM name.
51 ``kasan``
52 Common name for the tool, prominent part of the path ``mm/kasan``
53 ``snd_hda_codec_hdmi``
54 Has several components (``snd``, ``hda``, ``codec``, ``hdmi``) separated by
55 underscores. Matches the module name.
56
57 Avoid names as shown in examples below:
58
59 ``linear-ranges``
60 Names should use underscores, not dashes, to separate words. Prefer
61 ``linear_ranges``.
62 ``qos-kunit-test``
63 This name should use underscores, and not have "kunit-test" as a
64 suffix. ``qos`` is also ambiguous as a subsystem name, because several parts
65 of the kernel have a ``qos`` subsystem. ``power_qos`` would be a better name.
66 ``pc_parallel_port``
67 The corresponding module name is ``parport_pc``, so this subsystem should also
68 be named ``parport_pc``.
69
70 .. note::
71 The KUnit API and tools do not explicitly know about subsystems. They are
72 a way of categorizing test suites and naming modules which provides a
73 simple, consistent way for humans to find and run tests. This may change
74 in the future.
75
76 Suites
77 ------
78
79 KUnit tests are grouped into test suites, which cover a specific area of
80 functionality being tested. Test suites can have shared initialization and
81 shutdown code which is run for all tests in the suite. Not all subsystems need
82 to be split into multiple test suites (for example, simple drivers).
83
84 Test suites are named after the subsystem they are part of. If a subsystem
85 contains several suites, the specific area under test should be appended to the
86 subsystem name, separated by an underscore.
87
88 In the event that there are multiple types of test using KUnit within a
89 subsystem (for example, both unit tests and integration tests), they should be
90 put into separate suites, with the type of test as the last element in the suite
91 name. Unless these tests are actually present, avoid using ``_test``, ``_unittest``
92 or similar in the suite name.
93
94 The full test suite name (including the subsystem name) should be specified as
95 the ``.name`` member of the ``kunit_suite`` struct, and forms the base for the
96 module name. For example, test suites could include:
97
98 ``ext4_inode``
99 Part of the ``ext4`` subsystem, testing the ``inode`` area.
100 ``kunit_try_catch``
101 Part of the ``kunit`` implementation itself, testing the ``try_catch`` area.
102 ``apparmor_property_entry``
103 Part of the ``apparmor`` subsystem, testing the ``property_entry`` area.
104 ``kasan``
105 The ``kasan`` subsystem has only one suite, so the suite name is the same as
106 the subsystem name.
107
108 Avoid names, for example:
109
110 ``ext4_ext4_inode``
111 There is no reason to state the subsystem twice.
112 ``property_entry``
113 The suite name is ambiguous without the subsystem name.
114 ``kasan_integration_test``
115 Because there is only one suite in the ``kasan`` subsystem, the suite should
116 just be called as ``kasan``. Do not redundantly add
117 ``integration_test``. It should be a separate test suite. For example, if the
118 unit tests are added, then that suite could be named as ``kasan_unittest`` or
119 similar.
120
121 Test Cases
122 ----------
123
124 Individual tests consist of a single function which tests a constrained
125 codepath, property, or function. In the test output, an individual test's
126 results will show up as subtests of the suite's results.
127
128 Tests should be named after what they are testing. This is often the name of the
129 function being tested, with a description of the input or codepath being tested.
130 As tests are C functions, they should be named and written in accordance with
131 the kernel coding style.
132
133 .. note::
134 As tests are themselves functions, their names cannot conflict with
135 other C identifiers in the kernel. This may require some creative
136 naming. It is a good idea to make your test functions `static` to avoid
137 polluting the global namespace.
138
139 Example test names include:
140
141 ``unpack_u32_with_null_name``
142 Tests the ``unpack_u32`` function when a NULL name is passed in.
143 ``test_list_splice``
144 Tests the ``list_splice`` macro. It has the prefix ``test_`` to avoid a
145 name conflict with the macro itself.
146
147
148 Should it be necessary to refer to a test outside the context of its test suite,
149 the *fully-qualified* name of a test should be the suite name followed by the
150 test name, separated by a colon (i.e. ``suite:test``).
151
152 Test Kconfig Entries
153 ====================
154
155 Every test suite should be tied to a Kconfig entry.
156
157 This Kconfig entry must:
158
159 * be named ``CONFIG_<name>_KUNIT_TEST``: where <name> is the name of the test
160 suite.
161 * be listed either alongside the config entries for the driver/subsystem being
162 tested, or be under [Kernel Hacking]->[Kernel Testing and Coverage]
163 * depend on ``CONFIG_KUNIT``.
164 * be visible only if ``CONFIG_KUNIT_ALL_TESTS`` is not enabled.
165 * have a default value of ``CONFIG_KUNIT_ALL_TESTS``.
166 * have a brief description of KUnit in the help text.
167
168 If we are not able to meet above conditions (for example, the test is unable to
169 be built as a module), Kconfig entries for tests should be tristate.
170
171 For example, a Kconfig entry might look like:
172
173 .. code-block:: none
174
175 config FOO_KUNIT_TEST
176 tristate "KUnit test for foo" if !KUNIT_ALL_TESTS
177 depends on KUNIT
178 default KUNIT_ALL_TESTS
179 help
180 This builds unit tests for foo.
181
182 For more information on KUnit and unit tests in general,
183 please refer to the KUnit documentation in Documentation/dev-tools/kunit/.
184
185 If unsure, say N.
186
187
188 Test File and Module Names
189 ==========================
190
191 KUnit tests are often compiled as a separate module. To avoid conflicting
192 with regular modules, KUnit modules should be named after the test suite,
193 followed by ``_kunit`` (e.g. if "foobar" is the core module, then
194 "foobar_kunit" is the KUnit test module).
195
196 Test source files, whether compiled as a separate module or an
197 ``#include`` in another source file, are best kept in a ``tests/``
198 subdirectory to not conflict with other source files (e.g. for
199 tab-completion).
200
201 Note that the ``_test`` suffix has also been used in some existing
202 tests. The ``_kunit`` suffix is preferred, as it makes the distinction
203 between KUnit and non-KUnit tests clearer.
204
205 So for the common case, name the file containing the test suite
206 ``tests/<suite>_kunit.c``. The ``tests`` directory should be placed at
207 the same level as the code under test. For example, tests for
208 ``lib/string.c`` live in ``lib/tests/string_kunit.c``.
209
210 If the suite name contains some or all of the name of the test's parent
211 directory, it may make sense to modify the source filename to reduce
212 redundancy. For example, a ``foo_firmware`` suite could be in the
213 ``foo/tests/firmware_kunit.c`` file.
214

3. 한국어 전문 번역

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

Test style과 명명 지침

1-19

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

Test style과 nomenclature

KUnit test를 최대한 쉽게 찾고 작성하고 사용할 수 있도록 아래 지침에 따라 이름을 짓고 작성할 것을 강하게 권장합니다. 이 규칙을 따르지 않는 KUnit test도 작성할 수는 있지만 일부 tooling이 깨지거나 다른 테스트와 충돌하거나 testing system이 자동 실행하지 못할 수 있습니다.

다음 경우에만 이 지침에서 벗어나는 것이 좋습니다.

1. 이미 기존 이름으로 알려진 테스트를 KUnit으로 port하는 경우입니다.

2. 자동 실행했을 때 심각한 문제를 일으킬 테스트를 작성하는 경우입니다. 예를 들어 비결정적으로 false positive 또는 false negative를 만들거나 실행 시간이 매우 긴 테스트입니다.

Subsystem 정의와 이름

20-75

Subsystem, suite와 test

테스트를 쉽게 찾을 수 있도록 suite와 subsystem으로 묶습니다. Test suite는 kernel의 서로 관련된 영역을 검사하는 test group입니다. Subsystem은 kernel subsystem 또는 driver의 서로 다른 부분을 검사하는 test suite 집합입니다.

Subsystem

모든 test suite는 subsystem에 속해야 합니다. Subsystem은 같은 driver 또는 kernel 부분을 검사하는 하나 이상의 KUnit test suite 모음입니다.

Test subsystem은 하나의 kernel module과 대응해야 합니다. 검사할 code를 module로 compile할 수 없다면 많은 경우 source tree directory 또는 `MAINTAINERS` file의 entry와 대응해야 합니다. 확실하지 않으면 비슷한 영역의 test가 세운 convention을 따릅니다.

Test subsystem은 가능한 경우 module 이름을 따르고, 그렇지 않으면 검사할 directory 또는 file 이름을 따라야 합니다. 필요한 경우 모호하지 않도록 이름을 정합니다.

Subsystem 이름에 여러 component가 있으면 underscore로 구분합니다. 다른 test 자체나 KUnit framework를 검사하는 경우가 아니라면 subsystem 이름에 `test` 또는 `kunit`을 직접 포함하지 마십시오.

`ext4`는 module과 filesystem 이름에 대응합니다. `apparmor`는 module과 LSM 이름에 대응합니다. `kasan`은 tool의 일반적인 이름이며 `mm/kasan` path의 핵심 부분입니다.

`snd_hda_codec_hdmi`는 `snd`, `hda`, `codec`, `hdmi` component를 underscore로 구분하며 module 이름과 일치합니다.

다음과 같은 이름은 피해야 합니다.

`linear-ranges`는 단어 구분에 dash를 사용하므로 잘못되었습니다. `linear_ranges`가 좋습니다.

`qos-kunit-test`는 underscore를 사용해야 하고 `kunit-test` suffix를 붙이지 않아야 합니다. 또한 kernel 여러 부분에 `qos` subsystem이 있어 이름도 모호합니다. `power_qos`가 더 나은 이름입니다.

`pc_parallel_port`에 대응하는 module 이름은 `parport_pc`이므로 subsystem도 `parport_pc`로 이름 지어야 합니다.

참고: KUnit API와 tool은 subsystem을 명시적으로 알지 못합니다. Subsystem은 test suite를 분류하고 module 이름을 정해 사람이 테스트를 찾고 실행할 수 있게 하는 단순하고 일관된 방법입니다. 이는 향후 바뀔 수 있습니다.

KUnit test 계층
계층범위이름 기준
Subsystem같은 module, driver 또는 kernel 영역의 suite 집합Module, directory 또는 MAINTAINERS entry
Suite관련 기능을 검사하는 test groupSubsystem 이름과 세부 영역
Test case제한된 codepath, property 또는 function검사 대상과 input 또는 codepath

Subsystem, suite와 case가 담당하는 범위와 이름 기준을 정리했습니다.

Subsystem 이름 예
종류이름이유
권장ext4Module과 filesystem 이름에 일치
권장snd_hda_codec_hdmi여러 component를 underscore로 구분
피함linear-rangesDash 대신 underscore 필요
피함qos-kunit-test불필요한 suffix와 모호한 subsystem
피함pc_parallel_port실제 module parport_pc와 불일치

권장 이름과 피해야 할 이름의 차이를 보여 줍니다.

Test suite 구성과 이름

76-120

Suite

KUnit test는 검사할 특정 기능 영역을 다루는 test suite로 묶입니다. Suite에는 그 안의 모든 테스트에 대해 실행되는 공통 initialization과 shutdown code를 둘 수 있습니다. 단순한 driver처럼 모든 subsystem을 여러 suite로 나눌 필요는 없습니다.

Test suite 이름은 자신이 속한 subsystem 이름을 따릅니다. Subsystem에 여러 suite가 있다면 검사할 세부 영역을 underscore로 구분해 subsystem 이름 뒤에 붙입니다.

한 subsystem 안에서 KUnit을 사용하는 test type이 여러 개라면, 예를 들어 unit test와 integration test가 모두 있다면 서로 다른 suite에 넣고 test type을 suite 이름의 마지막 component로 둡니다. 실제로 여러 type이 존재하지 않는다면 suite 이름에 `_test`, `_unittest` 같은 suffix를 사용하지 마십시오.

Subsystem 이름을 포함한 전체 test suite 이름은 `kunit_suite` struct의 `.name` member로 지정하며 module 이름의 base가 됩니다.

`ext4_inode`는 `ext4` subsystem의 `inode` 영역을 검사합니다. `kunit_try_catch`는 KUnit 구현 자체의 `try_catch` 영역을 검사합니다. `apparmor_property_entry`는 `apparmor` subsystem의 `property_entry` 영역을 검사합니다.

`kasan` subsystem에는 suite가 하나뿐이므로 subsystem과 suite 이름이 같습니다.

다음 이름은 피해야 합니다.

`ext4_ext4_inode`는 subsystem 이름을 불필요하게 두 번 반복합니다.

`property_entry`는 subsystem 이름이 없어 어느 영역의 suite인지 모호합니다.

`kasan_integration_test`는 `kasan` subsystem에 suite가 하나뿐인 상황에서 `integration_test`를 불필요하게 덧붙였습니다. 이때 suite 이름은 단순히 `kasan`이어야 합니다.

나중에 unit test가 추가되어 별도의 test suite가 실제로 필요해지면 그 suite는 `kasan_unittest` 또는 비슷한 이름으로 정할 수 있습니다.

개별 test case 이름

121-151

Test case

개별 테스트는 제한된 codepath, property 또는 function을 검사하는 하나의 function으로 구성됩니다. Test output에서 개별 test 결과는 suite 결과의 subtest로 나타납니다.

Test 이름은 검사 대상을 따라야 합니다. 흔히 검사할 function 이름에 검사할 input 또는 codepath 설명을 붙입니다. Test는 C function이므로 kernel coding style에 맞게 이름을 짓고 작성해야 합니다.

참고: Test 자체가 function이므로 이름이 kernel의 다른 C identifier와 충돌할 수 없습니다. 이를 피하려면 이름을 창의적으로 정해야 할 수 있습니다. Global namespace를 오염시키지 않도록 test function을 `static`으로 만드는 것이 좋습니다.

`unpack_u32_with_null_name`은 NULL name을 전달했을 때의 `unpack_u32` function을 검사합니다.

`test_list_splice`는 `list_splice` macro를 검사합니다. Macro 자체와 이름이 충돌하지 않도록 `test_` prefix를 붙였습니다.

Test suite context 밖에서 test를 지칭해야 한다면 fully-qualified name은 suite 이름과 test 이름을 colon으로 구분한 `suite:test` 형식이어야 합니다.

Test Kconfig entry

152-187

Test Kconfig entry

모든 test suite는 Kconfig entry와 연결되어야 합니다.

Kconfig entry는 다음 조건을 충족해야 합니다.

이름은 `CONFIG_<name>_KUNIT_TEST`여야 하며 `<name>`은 test suite 이름입니다.

검사할 driver 또는 subsystem의 config entry와 나란히 두거나 `[Kernel Hacking] -> [Kernel Testing and Coverage]` 아래에 둡니다.

`CONFIG_KUNIT`에 dependency를 둡니다.

`CONFIG_KUNIT_ALL_TESTS`가 활성화되지 않았을 때만 사용자에게 보이도록 합니다.

Default value를 `CONFIG_KUNIT_ALL_TESTS`로 설정합니다.

Help text에 KUnit에 대한 짧은 설명을 넣습니다.

위 조건을 충족할 수 없는 경우, 예를 들어 테스트를 module로 build할 수 없는 경우에도 원문 지침은 test Kconfig entry가 tristate여야 한다고 설명합니다.

Kconfig entry 예는 다음과 같습니다.

config FOO_KUNIT_TEST
        tristate "KUnit test for foo" if !KUNIT_ALL_TESTS
        depends on KUNIT
        default KUNIT_ALL_TESTS
        help
          This builds unit tests for foo.

          For more information on KUnit and unit tests in general,
          please refer to the KUnit documentation in Documentation/dev-tools/kunit/.

          If unsure, say N.

Test file과 module 이름

188-213

Test file과 module 이름

KUnit test는 흔히 별도 module로 compile됩니다. 일반 module과 충돌하지 않도록 KUnit module은 test suite 이름 뒤에 `_kunit`을 붙여야 합니다. 예를 들어 core module이 `foobar`라면 KUnit test module은 `foobar_kunit`입니다.

Test source file은 별도 module로 compile하든 다른 source file에서 `#include`하든, 다른 source file과 충돌하지 않도록 `tests/` subdirectory에 두는 것이 좋습니다. Tab completion에서 이름이 섞이는 문제도 줄어듭니다.

기존 test 일부는 `_test` suffix도 사용해 왔습니다. KUnit test와 non-KUnit test를 더 명확히 구분하는 `_kunit` suffix를 권장합니다.

일반적인 경우 test suite를 포함하는 file 이름은 `tests/<suite>_kunit.c`로 정합니다. `tests` directory는 검사할 code와 같은 level에 둡니다. 예를 들어 `lib/string.c` 테스트는 `lib/tests/string_kunit.c`에 둡니다.

Suite 이름에 parent directory 이름 일부 또는 전부가 들어 있다면 중복을 줄이도록 source filename을 조정할 수 있습니다. 예를 들어 `foo_firmware` suite는 `foo/tests/firmware_kunit.c` file에 둘 수 있습니다.

KUnit artifact 이름
Artifact권장 형식
KconfigCONFIG_<name>_KUNIT_TESTCONFIG_FOO_KUNIT_TEST
Module<suite>_kunitfoobar_kunit
일반 sourcetests/<suite>_kunit.clib/tests/string_kunit.c
중복 축약 sourcetests/<detail>_kunit.cfoo/tests/firmware_kunit.c

Suite 이름에서 Kconfig, module과 source file 이름을 만드는 convention을 정리했습니다.