1a32fa6b2SRae Moar.. SPDX-License-Identifier: GPL-2.0 2a32fa6b2SRae Moar 3*a693396fSFrank Rowand=================================================== 4*a693396fSFrank RowandThe Kernel Test Anything Protocol (KTAP), version 1 5*a693396fSFrank 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 71a32fa6b2SRae Moar.. code-block:: 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 83a32fa6b2SRae Moarthe test, and can be any string of words (can't include #). The 84a32fa6b2SRae Moardescription 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 118a32fa6b2SRae MoarExample result lines include: 119a32fa6b2SRae Moar 120a32fa6b2SRae Moar.. code-block:: 121a32fa6b2SRae Moar 122a32fa6b2SRae Moar ok 1 test_case_name 123a32fa6b2SRae Moar 124a32fa6b2SRae MoarThe test "test_case_name" passed. 125a32fa6b2SRae Moar 126a32fa6b2SRae Moar.. code-block:: 127a32fa6b2SRae Moar 128a32fa6b2SRae Moar not ok 1 test_case_name 129a32fa6b2SRae Moar 130a32fa6b2SRae MoarThe test "test_case_name" failed. 131a32fa6b2SRae Moar 132a32fa6b2SRae Moar.. code-block:: 133a32fa6b2SRae Moar 134a32fa6b2SRae Moar ok 1 test # SKIP necessary dependency unavailable 135a32fa6b2SRae Moar 136a32fa6b2SRae MoarThe test "test" was SKIPPED with the diagnostic message "necessary dependency 137a32fa6b2SRae Moarunavailable". 138a32fa6b2SRae Moar 139a32fa6b2SRae Moar.. code-block:: 140a32fa6b2SRae Moar 141a32fa6b2SRae Moar not ok 1 test # TIMEOUT 30 seconds 142a32fa6b2SRae Moar 143a32fa6b2SRae MoarThe test "test" timed out, with diagnostic data "30 seconds". 144a32fa6b2SRae Moar 145a32fa6b2SRae Moar.. code-block:: 146a32fa6b2SRae Moar 147a32fa6b2SRae Moar ok 5 check return code # rcode=0 148a32fa6b2SRae Moar 149a32fa6b2SRae MoarThe test "check return code" passed, with additional diagnostic data “rcode=0” 150a32fa6b2SRae Moar 151a32fa6b2SRae Moar 152a32fa6b2SRae MoarDiagnostic lines 153a32fa6b2SRae Moar---------------- 154a32fa6b2SRae Moar 155a32fa6b2SRae MoarIf tests wish to output any further information, they should do so using 156a32fa6b2SRae Moar"diagnostic lines". Diagnostic lines are optional, freeform text, and are 157a32fa6b2SRae Moaroften used to describe what is being tested and any intermediate results in 158a32fa6b2SRae Moarmore detail than the final result and diagnostic data line provides. 159a32fa6b2SRae Moar 160a32fa6b2SRae MoarDiagnostic lines are formatted as "# <diagnostic_description>", where the 161a32fa6b2SRae Moardescription can be any string. Diagnostic lines can be anywhere in the test 162a32fa6b2SRae Moaroutput. As a rule, diagnostic lines regarding a test are directly before the 163a32fa6b2SRae Moartest result line for that test. 164a32fa6b2SRae Moar 165a32fa6b2SRae MoarNote that most tools will treat unknown lines (see below) as diagnostic lines, 166a32fa6b2SRae Moareven if they do not start with a "#": this is to capture any other useful 167a32fa6b2SRae Moarkernel output which may help debug the test. It is nevertheless recommended 168a32fa6b2SRae Moarthat tests always prefix any diagnostic output they have with a "#" character. 169a32fa6b2SRae Moar 170a32fa6b2SRae MoarUnknown lines 171a32fa6b2SRae Moar------------- 172a32fa6b2SRae Moar 173a32fa6b2SRae MoarThere may be lines within KTAP output that do not follow the format of one of 174a32fa6b2SRae Moarthe four formats for lines described above. This is allowed, however, they will 175a32fa6b2SRae Moarnot influence the status of the tests. 176a32fa6b2SRae Moar 177*a693396fSFrank RowandThis is an important difference from TAP. Kernel tests may print messages 178*a693396fSFrank Rowandto the system console or a log file. Both of these destinations may contain 179*a693396fSFrank Rowandmessages either from unrelated kernel or userspace activity, or kernel 180*a693396fSFrank Rowandmessages from non-test code that is invoked by the test. The kernel code 181*a693396fSFrank Rowandinvoked by the test likely is not aware that a test is in progress and 182*a693396fSFrank Rowandthus can not print the message as a diagnostic message. 183*a693396fSFrank Rowand 184a32fa6b2SRae MoarNested tests 185a32fa6b2SRae Moar------------ 186a32fa6b2SRae Moar 187a32fa6b2SRae MoarIn KTAP, tests can be nested. This is done by having a test include within its 188a32fa6b2SRae Moaroutput an entire set of KTAP-formatted results. This can be used to categorize 189a32fa6b2SRae Moarand group related tests, or to split out different results from the same test. 190a32fa6b2SRae Moar 191a32fa6b2SRae MoarThe "parent" test's result should consist of all of its subtests' results, 192a32fa6b2SRae Moarstarting with another KTAP version line and test plan, and end with the overall 193a32fa6b2SRae Moarresult. If one of the subtests fail, for example, the parent test should also 194a32fa6b2SRae Moarfail. 195a32fa6b2SRae Moar 196*a693396fSFrank RowandAdditionally, all lines in a subtest should be indented. One level of 197a32fa6b2SRae Moarindentation is two spaces: " ". The indentation should begin at the version 198a32fa6b2SRae Moarline and should end before the parent test's result line. 199a32fa6b2SRae Moar 200*a693396fSFrank Rowand"Unknown lines" are not considered to be lines in a subtest and thus are 201*a693396fSFrank Rowandallowed to be either indented or not indented. 202*a693396fSFrank Rowand 203a32fa6b2SRae MoarAn example of a test with two nested subtests: 204a32fa6b2SRae Moar 205a32fa6b2SRae Moar.. code-block:: 206a32fa6b2SRae Moar 207a32fa6b2SRae Moar KTAP version 1 208a32fa6b2SRae Moar 1..1 209a32fa6b2SRae Moar KTAP version 1 210a32fa6b2SRae Moar 1..2 211a32fa6b2SRae Moar ok 1 test_1 212a32fa6b2SRae Moar not ok 2 test_2 213a32fa6b2SRae Moar # example failed 214a32fa6b2SRae Moar not ok 1 example 215a32fa6b2SRae Moar 216a32fa6b2SRae MoarAn example format with multiple levels of nested testing: 217a32fa6b2SRae Moar 218a32fa6b2SRae Moar.. code-block:: 219a32fa6b2SRae Moar 220a32fa6b2SRae Moar KTAP version 1 221a32fa6b2SRae Moar 1..2 222a32fa6b2SRae Moar KTAP version 1 223a32fa6b2SRae Moar 1..2 224a32fa6b2SRae Moar KTAP version 1 225a32fa6b2SRae Moar 1..2 226a32fa6b2SRae Moar not ok 1 test_1 227a32fa6b2SRae Moar ok 2 test_2 228a32fa6b2SRae Moar not ok 1 test_3 229a32fa6b2SRae Moar ok 2 test_4 # SKIP 230a32fa6b2SRae Moar not ok 1 example_test_1 231a32fa6b2SRae Moar ok 2 example_test_2 232a32fa6b2SRae Moar 233a32fa6b2SRae Moar 234a32fa6b2SRae MoarMajor differences between TAP and KTAP 235a32fa6b2SRae Moar-------------------------------------- 236a32fa6b2SRae Moar 237*a693396fSFrank Rowand================================================== ========= =============== 238*a693396fSFrank RowandFeature TAP KTAP 239*a693396fSFrank Rowand================================================== ========= =============== 240*a693396fSFrank Rowandyaml and json in diagnosic message ok not recommended 241*a693396fSFrank RowandTODO directive ok not recognized 242*a693396fSFrank Rowandallows an arbitrary number of tests to be nested no yes 243*a693396fSFrank Rowand"Unknown lines" are in category of "Anything else" yes no 244*a693396fSFrank Rowand"Unknown lines" are incorrect allowed 245*a693396fSFrank Rowand================================================== ========= =============== 246a32fa6b2SRae Moar 247a32fa6b2SRae MoarThe TAP14 specification does permit nested tests, but instead of using another 248a32fa6b2SRae Moarnested version line, uses a line of the form 249a32fa6b2SRae Moar"Subtest: <name>" where <name> is the name of the parent test. 250a32fa6b2SRae Moar 251a32fa6b2SRae MoarExample KTAP output 252a32fa6b2SRae Moar-------------------- 253a32fa6b2SRae Moar.. code-block:: 254a32fa6b2SRae Moar 255a32fa6b2SRae Moar KTAP version 1 256a32fa6b2SRae Moar 1..1 257a32fa6b2SRae Moar KTAP version 1 258a32fa6b2SRae Moar 1..3 259a32fa6b2SRae Moar KTAP version 1 260a32fa6b2SRae Moar 1..1 261a32fa6b2SRae Moar # test_1: initializing test_1 262a32fa6b2SRae Moar ok 1 test_1 263a32fa6b2SRae Moar ok 1 example_test_1 264a32fa6b2SRae Moar KTAP version 1 265a32fa6b2SRae Moar 1..2 266a32fa6b2SRae Moar ok 1 test_1 # SKIP test_1 skipped 267a32fa6b2SRae Moar ok 2 test_2 268a32fa6b2SRae Moar ok 2 example_test_2 269a32fa6b2SRae Moar KTAP version 1 270a32fa6b2SRae Moar 1..3 271a32fa6b2SRae Moar ok 1 test_1 272a32fa6b2SRae Moar # test_2: FAIL 273a32fa6b2SRae Moar not ok 2 test_2 274a32fa6b2SRae Moar ok 3 test_3 # SKIP test_3 skipped 275a32fa6b2SRae Moar not ok 3 example_test_3 276a32fa6b2SRae Moar not ok 1 main_test 277a32fa6b2SRae Moar 278a32fa6b2SRae MoarThis output defines the following hierarchy: 279a32fa6b2SRae Moar 280a32fa6b2SRae MoarA single test called "main_test", which fails, and has three subtests: 281a32fa6b2SRae Moar- "example_test_1", which passes, and has one subtest: 282a32fa6b2SRae Moar 283a32fa6b2SRae Moar - "test_1", which passes, and outputs the diagnostic message "test_1: initializing test_1" 284a32fa6b2SRae Moar 285a32fa6b2SRae Moar- "example_test_2", which passes, and has two subtests: 286a32fa6b2SRae Moar 287a32fa6b2SRae Moar - "test_1", which is skipped, with the explanation "test_1 skipped" 288a32fa6b2SRae Moar - "test_2", which passes 289a32fa6b2SRae Moar 290a32fa6b2SRae Moar- "example_test_3", which fails, and has three subtests 291a32fa6b2SRae Moar 292a32fa6b2SRae Moar - "test_1", which passes 293a32fa6b2SRae Moar - "test_2", which outputs the diagnostic line "test_2: FAIL", and fails. 294a32fa6b2SRae Moar - "test_3", which is skipped with the explanation "test_3 skipped" 295a32fa6b2SRae Moar 296a32fa6b2SRae MoarNote that the individual subtests with the same names do not conflict, as they 297a32fa6b2SRae Moarare found in different parent tests. This output also exhibits some sensible 298a32fa6b2SRae Moarrules for "bubbling up" test results: a test fails if any of its subtests fail. 299a32fa6b2SRae MoarSkipped tests do not affect the result of the parent test (though it often 300a32fa6b2SRae Moarmakes sense for a test to be marked skipped if _all_ of its subtests have been 301a32fa6b2SRae Moarskipped). 302a32fa6b2SRae Moar 303a32fa6b2SRae MoarSee also: 304a32fa6b2SRae Moar--------- 305a32fa6b2SRae Moar 306a32fa6b2SRae Moar- The TAP specification: 307a32fa6b2SRae Moar https://testanything.org/tap-version-13-specification.html 308a32fa6b2SRae Moar- The (stagnant) TAP version 14 specification: 309a32fa6b2SRae Moar https://github.com/TestAnything/Specification/blob/tap-14-specification/specification.md 310a32fa6b2SRae Moar- The kselftest documentation: 311a32fa6b2SRae Moar Documentation/dev-tools/kselftest.rst 312a32fa6b2SRae Moar- The KUnit documentation: 313a32fa6b2SRae Moar Documentation/dev-tools/kunit/index.rst 314