xref: /linux/Documentation/dev-tools/ktap.rst (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
1a32fa6b2SRae Moar.. SPDX-License-Identifier: GPL-2.0
2a32fa6b2SRae Moar
3a693396fSFrank Rowand===================================================
4a693396fSFrank RowandThe Kernel Test Anything Protocol (KTAP), version 1
5a693396fSFrank Rowand===================================================
6a32fa6b2SRae Moar
7a32fa6b2SRae MoarTAP, or the Test Anything Protocol is a format for specifying test results used
8a32fa6b2SRae Moarby a number of projects. It's website and specification are found at this `link
9a32fa6b2SRae Moar<https://testanything.org/>`_. The Linux Kernel largely uses TAP output for test
10a32fa6b2SRae Moarresults. However, Kernel testing frameworks have special needs for test results
11a32fa6b2SRae Moarwhich don't align with the original TAP specification. Thus, a "Kernel TAP"
12a32fa6b2SRae Moar(KTAP) format is specified to extend and alter TAP to support these use-cases.
13a32fa6b2SRae MoarThis specification describes the generally accepted format of KTAP as it is
14a32fa6b2SRae Moarcurrently used in the kernel.
15a32fa6b2SRae Moar
16a32fa6b2SRae MoarKTAP test results describe a series of tests (which may be nested: i.e., test
17a32fa6b2SRae Moarcan have subtests), each of which can contain both diagnostic data -- e.g., log
18a32fa6b2SRae Moarlines -- and a final result. The test structure and results are
19a32fa6b2SRae Moarmachine-readable, whereas the diagnostic data is unstructured and is there to
20a32fa6b2SRae Moaraid human debugging.
21a32fa6b2SRae Moar
22a32fa6b2SRae MoarKTAP output is built from four different types of lines:
23a32fa6b2SRae Moar- Version lines
24a32fa6b2SRae Moar- Plan lines
25a32fa6b2SRae Moar- Test case result lines
26a32fa6b2SRae Moar- Diagnostic lines
27a32fa6b2SRae Moar
28a32fa6b2SRae MoarIn general, valid KTAP output should also form valid TAP output, but some
29a32fa6b2SRae Moarinformation, in particular nested test results, may be lost. Also note that
30a32fa6b2SRae Moarthere is a stagnant draft specification for TAP14, KTAP diverges from this in
31a32fa6b2SRae Moara couple of places (notably the "Subtest" header), which are described where
32a32fa6b2SRae Moarrelevant later in this document.
33a32fa6b2SRae Moar
34a32fa6b2SRae MoarVersion lines
35a32fa6b2SRae Moar-------------
36a32fa6b2SRae Moar
37a32fa6b2SRae MoarAll KTAP-formatted results begin with a "version line" which specifies which
38a32fa6b2SRae Moarversion of the (K)TAP standard the result is compliant with.
39a32fa6b2SRae Moar
40a32fa6b2SRae MoarFor example:
41a32fa6b2SRae Moar- "KTAP version 1"
42a32fa6b2SRae Moar- "TAP version 13"
43a32fa6b2SRae Moar- "TAP version 14"
44a32fa6b2SRae Moar
45a32fa6b2SRae MoarNote that, in KTAP, subtests also begin with a version line, which denotes the
46a32fa6b2SRae Moarstart of the nested test results. This differs from TAP14, which uses a
47a32fa6b2SRae Moarseparate "Subtest" line.
48a32fa6b2SRae Moar
49a32fa6b2SRae MoarWhile, going forward, "KTAP version 1" should be used by compliant tests, it
50a32fa6b2SRae Moaris expected that most parsers and other tooling will accept the other versions
51a32fa6b2SRae Moarlisted here for compatibility with existing tests and frameworks.
52a32fa6b2SRae Moar
53a32fa6b2SRae MoarPlan lines
54a32fa6b2SRae Moar----------
55a32fa6b2SRae Moar
56a32fa6b2SRae MoarA test plan provides the number of tests (or subtests) in the KTAP output.
57a32fa6b2SRae Moar
58a32fa6b2SRae MoarPlan lines must follow the format of "1..N" where N is the number of tests or subtests.
59a32fa6b2SRae MoarPlan lines follow version lines to indicate the number of nested tests.
60a32fa6b2SRae Moar
61a32fa6b2SRae MoarWhile there are cases where the number of tests is not known in advance -- in
62a32fa6b2SRae Moarwhich case the test plan may be omitted -- it is strongly recommended one is
63a32fa6b2SRae Moarpresent where possible.
64a32fa6b2SRae Moar
65a32fa6b2SRae MoarTest case result lines
66a32fa6b2SRae Moar----------------------
67a32fa6b2SRae Moar
68a32fa6b2SRae MoarTest case result lines indicate the final status of a test.
69a32fa6b2SRae MoarThey are required and must have the format:
70a32fa6b2SRae Moar
71ff136876SRandy Dunlap.. code-block:: none
72a32fa6b2SRae Moar
73a32fa6b2SRae Moar	<result> <number> [<description>][ # [<directive>] [<diagnostic data>]]
74a32fa6b2SRae Moar
75a32fa6b2SRae MoarThe result can be either "ok", which indicates the test case passed,
76a32fa6b2SRae Moaror "not ok", which indicates that the test case failed.
77a32fa6b2SRae Moar
78a32fa6b2SRae Moar<number> represents the number of the test being performed. The first test must
79a32fa6b2SRae Moarhave the number 1 and the number then must increase by 1 for each additional
80a32fa6b2SRae Moarsubtest within the same test at the same nesting level.
81a32fa6b2SRae Moar
82a32fa6b2SRae MoarThe description is a description of the test, generally the name of
83*054be257SMark Brownthe test, and can be any string of characters other than # or a
84*054be257SMark Brownnewline.  The description is optional, but recommended.
85a32fa6b2SRae Moar
86a32fa6b2SRae MoarThe directive and any diagnostic data is optional. If either are present, they
87a32fa6b2SRae Moarmust follow a hash sign, "#".
88a32fa6b2SRae Moar
89a32fa6b2SRae MoarA directive is a keyword that indicates a different outcome for a test other
90a32fa6b2SRae Moarthan passed and failed. The directive is optional, and consists of a single
91a32fa6b2SRae Moarkeyword preceding the diagnostic data. In the event that a parser encounters
92a32fa6b2SRae Moara directive it doesn't support, it should fall back to the "ok" / "not ok"
93a32fa6b2SRae Moarresult.
94a32fa6b2SRae Moar
95a32fa6b2SRae MoarCurrently accepted directives are:
96a32fa6b2SRae Moar
97a32fa6b2SRae Moar- "SKIP", which indicates a test was skipped (note the result of the test case
98a32fa6b2SRae Moar  result line can be either "ok" or "not ok" if the SKIP directive is used)
99a32fa6b2SRae Moar- "TODO", which indicates that a test is not expected to pass at the moment,
100a32fa6b2SRae Moar  e.g. because the feature it is testing is known to be broken. While this
101a32fa6b2SRae Moar  directive is inherited from TAP, its use in the kernel is discouraged.
102a32fa6b2SRae Moar- "XFAIL", which indicates that a test is expected to fail. This is similar
103a32fa6b2SRae Moar  to "TODO", above, and is used by some kselftest tests.
104a32fa6b2SRae Moar- “TIMEOUT”, which indicates a test has timed out (note the result of the test
105a32fa6b2SRae Moar  case result line should be “not ok” if the TIMEOUT directive is used)
106a32fa6b2SRae Moar- “ERROR”, which indicates that the execution of a test has failed due to a
107a32fa6b2SRae Moar  specific error that is included in the diagnostic data. (note the result of
108a32fa6b2SRae Moar  the test case result line should be “not ok” if the ERROR directive is used)
109a32fa6b2SRae Moar
110a32fa6b2SRae MoarThe diagnostic data is a plain-text field which contains any additional details
111a32fa6b2SRae Moarabout why this result was produced. This is typically an error message for ERROR
112a32fa6b2SRae Moaror failed tests, or a description of missing dependencies for a SKIP result.
113a32fa6b2SRae Moar
114a32fa6b2SRae MoarThe diagnostic data field is optional, and results which have neither a
115a32fa6b2SRae Moardirective nor any diagnostic data do not need to include the "#" field
116a32fa6b2SRae Moarseparator.
117a32fa6b2SRae Moar
11862ce577bSFrank RowandExample result lines include::
119a32fa6b2SRae Moar
120a32fa6b2SRae Moar	ok 1 test_case_name
121a32fa6b2SRae Moar
122a32fa6b2SRae MoarThe test "test_case_name" passed.
123a32fa6b2SRae Moar
12462ce577bSFrank Rowand::
125a32fa6b2SRae Moar
126a32fa6b2SRae Moar	not ok 1 test_case_name
127a32fa6b2SRae Moar
128a32fa6b2SRae MoarThe test "test_case_name" failed.
129a32fa6b2SRae Moar
13062ce577bSFrank Rowand::
131a32fa6b2SRae Moar
132a32fa6b2SRae Moar	ok 1 test # SKIP necessary dependency unavailable
133a32fa6b2SRae Moar
134a32fa6b2SRae MoarThe test "test" was SKIPPED with the diagnostic message "necessary dependency
135a32fa6b2SRae Moarunavailable".
136a32fa6b2SRae Moar
13762ce577bSFrank Rowand::
138a32fa6b2SRae Moar
139a32fa6b2SRae Moar	not ok 1 test # TIMEOUT 30 seconds
140a32fa6b2SRae Moar
141a32fa6b2SRae MoarThe test "test" timed out, with diagnostic data "30 seconds".
142a32fa6b2SRae Moar
14362ce577bSFrank Rowand::
144a32fa6b2SRae Moar
145a32fa6b2SRae Moar	ok 5 check return code # rcode=0
146a32fa6b2SRae Moar
147a32fa6b2SRae MoarThe test "check return code" passed, with additional diagnostic data “rcode=0”
148a32fa6b2SRae Moar
149a32fa6b2SRae Moar
150a32fa6b2SRae MoarDiagnostic lines
151a32fa6b2SRae Moar----------------
152a32fa6b2SRae Moar
153a32fa6b2SRae MoarIf tests wish to output any further information, they should do so using
154a32fa6b2SRae Moar"diagnostic lines". Diagnostic lines are optional, freeform text, and are
155a32fa6b2SRae Moaroften used to describe what is being tested and any intermediate results in
156a32fa6b2SRae Moarmore detail than the final result and diagnostic data line provides.
157a32fa6b2SRae Moar
158a32fa6b2SRae MoarDiagnostic lines are formatted as "# <diagnostic_description>", where the
159a32fa6b2SRae Moardescription can be any string.  Diagnostic lines can be anywhere in the test
160a32fa6b2SRae Moaroutput. As a rule, diagnostic lines regarding a test are directly before the
161a32fa6b2SRae Moartest result line for that test.
162a32fa6b2SRae Moar
163a32fa6b2SRae MoarNote that most tools will treat unknown lines (see below) as diagnostic lines,
164a32fa6b2SRae Moareven if they do not start with a "#": this is to capture any other useful
165a32fa6b2SRae Moarkernel output which may help debug the test. It is nevertheless recommended
166a32fa6b2SRae Moarthat tests always prefix any diagnostic output they have with a "#" character.
167a32fa6b2SRae Moar
168a32fa6b2SRae MoarUnknown lines
169a32fa6b2SRae Moar-------------
170a32fa6b2SRae Moar
171a32fa6b2SRae MoarThere may be lines within KTAP output that do not follow the format of one of
172a32fa6b2SRae Moarthe four formats for lines described above. This is allowed, however, they will
173a32fa6b2SRae Moarnot influence the status of the tests.
174a32fa6b2SRae Moar
175a693396fSFrank RowandThis is an important difference from TAP.  Kernel tests may print messages
176a693396fSFrank Rowandto the system console or a log file.  Both of these destinations may contain
177a693396fSFrank Rowandmessages either from unrelated kernel or userspace activity, or kernel
178a693396fSFrank Rowandmessages from non-test code that is invoked by the test.  The kernel code
179a693396fSFrank Rowandinvoked by the test likely is not aware that a test is in progress and
180a693396fSFrank Rowandthus can not print the message as a diagnostic message.
181a693396fSFrank Rowand
182a32fa6b2SRae MoarNested tests
183a32fa6b2SRae Moar------------
184a32fa6b2SRae Moar
185a32fa6b2SRae MoarIn KTAP, tests can be nested. This is done by having a test include within its
186a32fa6b2SRae Moaroutput an entire set of KTAP-formatted results. This can be used to categorize
187a32fa6b2SRae Moarand group related tests, or to split out different results from the same test.
188a32fa6b2SRae Moar
189a32fa6b2SRae MoarThe "parent" test's result should consist of all of its subtests' results,
190a32fa6b2SRae Moarstarting with another KTAP version line and test plan, and end with the overall
191a32fa6b2SRae Moarresult. If one of the subtests fail, for example, the parent test should also
192a32fa6b2SRae Moarfail.
193a32fa6b2SRae Moar
194a693396fSFrank RowandAdditionally, all lines in a subtest should be indented. One level of
195a32fa6b2SRae Moarindentation is two spaces: "  ". The indentation should begin at the version
196a32fa6b2SRae Moarline and should end before the parent test's result line.
197a32fa6b2SRae Moar
198a693396fSFrank Rowand"Unknown lines" are not considered to be lines in a subtest and thus are
199a693396fSFrank Rowandallowed to be either indented or not indented.
200a693396fSFrank Rowand
201a32fa6b2SRae MoarAn example of a test with two nested subtests:
202a32fa6b2SRae Moar
20362ce577bSFrank Rowand::
204a32fa6b2SRae Moar
205a32fa6b2SRae Moar	KTAP version 1
206a32fa6b2SRae Moar	1..1
207a32fa6b2SRae Moar	  KTAP version 1
208a32fa6b2SRae Moar	  1..2
209a32fa6b2SRae Moar	  ok 1 test_1
210a32fa6b2SRae Moar	  not ok 2 test_2
211a32fa6b2SRae Moar	# example failed
212a32fa6b2SRae Moar	not ok 1 example
213a32fa6b2SRae Moar
214a32fa6b2SRae MoarAn example format with multiple levels of nested testing:
215a32fa6b2SRae Moar
21662ce577bSFrank Rowand::
217a32fa6b2SRae Moar
218a32fa6b2SRae Moar	KTAP version 1
219a32fa6b2SRae Moar	1..2
220a32fa6b2SRae Moar	  KTAP version 1
221a32fa6b2SRae Moar	  1..2
222a32fa6b2SRae Moar	    KTAP version 1
223a32fa6b2SRae Moar	    1..2
224a32fa6b2SRae Moar	    not ok 1 test_1
225a32fa6b2SRae Moar	    ok 2 test_2
226a32fa6b2SRae Moar	  not ok 1 test_3
227a32fa6b2SRae Moar	  ok 2 test_4 # SKIP
228a32fa6b2SRae Moar	not ok 1 example_test_1
229a32fa6b2SRae Moar	ok 2 example_test_2
230a32fa6b2SRae Moar
231a32fa6b2SRae Moar
232a32fa6b2SRae MoarMajor differences between TAP and KTAP
233a32fa6b2SRae Moar--------------------------------------
234a32fa6b2SRae Moar
235a693396fSFrank Rowand==================================================   =========  ===============
236a693396fSFrank RowandFeature                                              TAP        KTAP
237a693396fSFrank Rowand==================================================   =========  ===============
238a693396fSFrank Rowandyaml and json in diagnosic message                   ok         not recommended
239a693396fSFrank RowandTODO directive                                       ok         not recognized
240a693396fSFrank Rowandallows an arbitrary number of tests to be nested     no         yes
241a693396fSFrank Rowand"Unknown lines" are in category of "Anything else"   yes        no
242a693396fSFrank Rowand"Unknown lines" are                                  incorrect  allowed
243a693396fSFrank Rowand==================================================   =========  ===============
244a32fa6b2SRae Moar
245a32fa6b2SRae MoarThe TAP14 specification does permit nested tests, but instead of using another
246a32fa6b2SRae Moarnested version line, uses a line of the form
247a32fa6b2SRae Moar"Subtest: <name>" where <name> is the name of the parent test.
248a32fa6b2SRae Moar
249a32fa6b2SRae MoarExample KTAP output
250a32fa6b2SRae Moar--------------------
25162ce577bSFrank Rowand::
252a32fa6b2SRae Moar
253a32fa6b2SRae Moar	KTAP version 1
254a32fa6b2SRae Moar	1..1
255a32fa6b2SRae Moar	  KTAP version 1
256a32fa6b2SRae Moar	  1..3
257a32fa6b2SRae Moar	    KTAP version 1
258a32fa6b2SRae Moar	    1..1
259a32fa6b2SRae Moar	    # test_1: initializing test_1
260a32fa6b2SRae Moar	    ok 1 test_1
261a32fa6b2SRae Moar	  ok 1 example_test_1
262a32fa6b2SRae Moar	    KTAP version 1
263a32fa6b2SRae Moar	    1..2
264a32fa6b2SRae Moar	    ok 1 test_1 # SKIP test_1 skipped
265a32fa6b2SRae Moar	    ok 2 test_2
266a32fa6b2SRae Moar	  ok 2 example_test_2
267a32fa6b2SRae Moar	    KTAP version 1
268a32fa6b2SRae Moar	    1..3
269a32fa6b2SRae Moar	    ok 1 test_1
270a32fa6b2SRae Moar	    # test_2: FAIL
271a32fa6b2SRae Moar	    not ok 2 test_2
272a32fa6b2SRae Moar	    ok 3 test_3 # SKIP test_3 skipped
273a32fa6b2SRae Moar	  not ok 3 example_test_3
274a32fa6b2SRae Moar	not ok 1 main_test
275a32fa6b2SRae Moar
276a32fa6b2SRae MoarThis output defines the following hierarchy:
277a32fa6b2SRae Moar
278a32fa6b2SRae MoarA single test called "main_test", which fails, and has three subtests:
279a32fa6b2SRae Moar- "example_test_1", which passes, and has one subtest:
280a32fa6b2SRae Moar
281a32fa6b2SRae Moar   - "test_1", which passes, and outputs the diagnostic message "test_1: initializing test_1"
282a32fa6b2SRae Moar
283a32fa6b2SRae Moar- "example_test_2", which passes, and has two subtests:
284a32fa6b2SRae Moar
285a32fa6b2SRae Moar   - "test_1", which is skipped, with the explanation "test_1 skipped"
286a32fa6b2SRae Moar   - "test_2", which passes
287a32fa6b2SRae Moar
288a32fa6b2SRae Moar- "example_test_3", which fails, and has three subtests
289a32fa6b2SRae Moar
290a32fa6b2SRae Moar   - "test_1", which passes
291a32fa6b2SRae Moar   - "test_2", which outputs the diagnostic line "test_2: FAIL", and fails.
292a32fa6b2SRae Moar   - "test_3", which is skipped with the explanation "test_3 skipped"
293a32fa6b2SRae Moar
294a32fa6b2SRae MoarNote that the individual subtests with the same names do not conflict, as they
295a32fa6b2SRae Moarare found in different parent tests. This output also exhibits some sensible
296a32fa6b2SRae Moarrules for "bubbling up" test results: a test fails if any of its subtests fail.
297a32fa6b2SRae MoarSkipped tests do not affect the result of the parent test (though it often
298a32fa6b2SRae Moarmakes sense for a test to be marked skipped if _all_ of its subtests have been
299a32fa6b2SRae Moarskipped).
300a32fa6b2SRae Moar
301a32fa6b2SRae MoarSee also:
302a32fa6b2SRae Moar---------
303a32fa6b2SRae Moar
304a32fa6b2SRae Moar- The TAP specification:
305a32fa6b2SRae Moar  https://testanything.org/tap-version-13-specification.html
306a32fa6b2SRae Moar- The (stagnant) TAP version 14 specification:
307a32fa6b2SRae Moar  https://github.com/TestAnything/Specification/blob/tap-14-specification/specification.md
308a32fa6b2SRae Moar- The kselftest documentation:
309a32fa6b2SRae Moar  Documentation/dev-tools/kselftest.rst
310a32fa6b2SRae Moar- The KUnit documentation:
311a32fa6b2SRae Moar  Documentation/dev-tools/kunit/index.rst
312