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