ktap.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ===================================================
  3. The Kernel Test Anything Protocol (KTAP), version 1
  4. ===================================================
  5. TAP, or the Test Anything Protocol is a format for specifying test results used
  6. by a number of projects. It's website and specification are found at this `link
  7. <https://testanything.org/>`_. The Linux Kernel largely uses TAP output for test
  8. results. However, Kernel testing frameworks have special needs for test results
  9. which don't align with the original TAP specification. Thus, a "Kernel TAP"
  10. (KTAP) format is specified to extend and alter TAP to support these use-cases.
  11. This specification describes the generally accepted format of KTAP as it is
  12. currently used in the kernel.
  13. KTAP test results describe a series of tests (which may be nested: i.e., test
  14. can have subtests), each of which can contain both diagnostic data -- e.g., log
  15. lines -- and a final result. The test structure and results are
  16. machine-readable, whereas the diagnostic data is unstructured and is there to
  17. aid human debugging.
  18. KTAP output is built from four different types of lines:
  19. - Version lines
  20. - Plan lines
  21. - Test case result lines
  22. - Diagnostic lines
  23. In general, valid KTAP output should also form valid TAP output, but some
  24. information, in particular nested test results, may be lost. Also note that
  25. there is a stagnant draft specification for TAP14, KTAP diverges from this in
  26. a couple of places (notably the "Subtest" header), which are described where
  27. relevant later in this document.
  28. Version lines
  29. -------------
  30. All KTAP-formatted results begin with a "version line" which specifies which
  31. version of the (K)TAP standard the result is compliant with.
  32. For example:
  33. - "KTAP version 1"
  34. - "TAP version 13"
  35. - "TAP version 14"
  36. Note that, in KTAP, subtests also begin with a version line, which denotes the
  37. start of the nested test results. This differs from TAP14, which uses a
  38. separate "Subtest" line.
  39. While, going forward, "KTAP version 1" should be used by compliant tests, it
  40. is expected that most parsers and other tooling will accept the other versions
  41. listed here for compatibility with existing tests and frameworks.
  42. Plan lines
  43. ----------
  44. A test plan provides the number of tests (or subtests) in the KTAP output.
  45. Plan lines must follow the format of "1..N" where N is the number of tests or subtests.
  46. Plan lines follow version lines to indicate the number of nested tests.
  47. While there are cases where the number of tests is not known in advance -- in
  48. which case the test plan may be omitted -- it is strongly recommended one is
  49. present where possible.
  50. Test case result lines
  51. ----------------------
  52. Test case result lines indicate the final status of a test.
  53. They are required and must have the format:
  54. .. code-block:: none
  55. <result> <number> [<description>][ # [<directive>] [<diagnostic data>]]
  56. The result can be either "ok", which indicates the test case passed,
  57. or "not ok", which indicates that the test case failed.
  58. <number> represents the number of the test being performed. The first test must
  59. have the number 1 and the number then must increase by 1 for each additional
  60. subtest within the same test at the same nesting level.
  61. The description is a description of the test, generally the name of
  62. the test, and can be any string of words (can't include #). The
  63. description is optional, but recommended.
  64. The directive and any diagnostic data is optional. If either are present, they
  65. must follow a hash sign, "#".
  66. A directive is a keyword that indicates a different outcome for a test other
  67. than passed and failed. The directive is optional, and consists of a single
  68. keyword preceding the diagnostic data. In the event that a parser encounters
  69. a directive it doesn't support, it should fall back to the "ok" / "not ok"
  70. result.
  71. Currently accepted directives are:
  72. - "SKIP", which indicates a test was skipped (note the result of the test case
  73. result line can be either "ok" or "not ok" if the SKIP directive is used)
  74. - "TODO", which indicates that a test is not expected to pass at the moment,
  75. e.g. because the feature it is testing is known to be broken. While this
  76. directive is inherited from TAP, its use in the kernel is discouraged.
  77. - "XFAIL", which indicates that a test is expected to fail. This is similar
  78. to "TODO", above, and is used by some kselftest tests.
  79. - “TIMEOUT”, which indicates a test has timed out (note the result of the test
  80. case result line should be “not ok” if the TIMEOUT directive is used)
  81. - “ERROR”, which indicates that the execution of a test has failed due to a
  82. specific error that is included in the diagnostic data. (note the result of
  83. the test case result line should be “not ok” if the ERROR directive is used)
  84. The diagnostic data is a plain-text field which contains any additional details
  85. about why this result was produced. This is typically an error message for ERROR
  86. or failed tests, or a description of missing dependencies for a SKIP result.
  87. The diagnostic data field is optional, and results which have neither a
  88. directive nor any diagnostic data do not need to include the "#" field
  89. separator.
  90. Example result lines include::
  91. ok 1 test_case_name
  92. The test "test_case_name" passed.
  93. ::
  94. not ok 1 test_case_name
  95. The test "test_case_name" failed.
  96. ::
  97. ok 1 test # SKIP necessary dependency unavailable
  98. The test "test" was SKIPPED with the diagnostic message "necessary dependency
  99. unavailable".
  100. ::
  101. not ok 1 test # TIMEOUT 30 seconds
  102. The test "test" timed out, with diagnostic data "30 seconds".
  103. ::
  104. ok 5 check return code # rcode=0
  105. The test "check return code" passed, with additional diagnostic data “rcode=0”
  106. Diagnostic lines
  107. ----------------
  108. If tests wish to output any further information, they should do so using
  109. "diagnostic lines". Diagnostic lines are optional, freeform text, and are
  110. often used to describe what is being tested and any intermediate results in
  111. more detail than the final result and diagnostic data line provides.
  112. Diagnostic lines are formatted as "# <diagnostic_description>", where the
  113. description can be any string. Diagnostic lines can be anywhere in the test
  114. output. As a rule, diagnostic lines regarding a test are directly before the
  115. test result line for that test.
  116. Note that most tools will treat unknown lines (see below) as diagnostic lines,
  117. even if they do not start with a "#": this is to capture any other useful
  118. kernel output which may help debug the test. It is nevertheless recommended
  119. that tests always prefix any diagnostic output they have with a "#" character.
  120. Unknown lines
  121. -------------
  122. There may be lines within KTAP output that do not follow the format of one of
  123. the four formats for lines described above. This is allowed, however, they will
  124. not influence the status of the tests.
  125. This is an important difference from TAP. Kernel tests may print messages
  126. to the system console or a log file. Both of these destinations may contain
  127. messages either from unrelated kernel or userspace activity, or kernel
  128. messages from non-test code that is invoked by the test. The kernel code
  129. invoked by the test likely is not aware that a test is in progress and
  130. thus can not print the message as a diagnostic message.
  131. Nested tests
  132. ------------
  133. In KTAP, tests can be nested. This is done by having a test include within its
  134. output an entire set of KTAP-formatted results. This can be used to categorize
  135. and group related tests, or to split out different results from the same test.
  136. The "parent" test's result should consist of all of its subtests' results,
  137. starting with another KTAP version line and test plan, and end with the overall
  138. result. If one of the subtests fail, for example, the parent test should also
  139. fail.
  140. Additionally, all lines in a subtest should be indented. One level of
  141. indentation is two spaces: " ". The indentation should begin at the version
  142. line and should end before the parent test's result line.
  143. "Unknown lines" are not considered to be lines in a subtest and thus are
  144. allowed to be either indented or not indented.
  145. An example of a test with two nested subtests:
  146. ::
  147. KTAP version 1
  148. 1..1
  149. KTAP version 1
  150. 1..2
  151. ok 1 test_1
  152. not ok 2 test_2
  153. # example failed
  154. not ok 1 example
  155. An example format with multiple levels of nested testing:
  156. ::
  157. KTAP version 1
  158. 1..2
  159. KTAP version 1
  160. 1..2
  161. KTAP version 1
  162. 1..2
  163. not ok 1 test_1
  164. ok 2 test_2
  165. not ok 1 test_3
  166. ok 2 test_4 # SKIP
  167. not ok 1 example_test_1
  168. ok 2 example_test_2
  169. Major differences between TAP and KTAP
  170. --------------------------------------
  171. ================================================== ========= ===============
  172. Feature TAP KTAP
  173. ================================================== ========= ===============
  174. yaml and json in diagnosic message ok not recommended
  175. TODO directive ok not recognized
  176. allows an arbitrary number of tests to be nested no yes
  177. "Unknown lines" are in category of "Anything else" yes no
  178. "Unknown lines" are incorrect allowed
  179. ================================================== ========= ===============
  180. The TAP14 specification does permit nested tests, but instead of using another
  181. nested version line, uses a line of the form
  182. "Subtest: <name>" where <name> is the name of the parent test.
  183. Example KTAP output
  184. --------------------
  185. ::
  186. KTAP version 1
  187. 1..1
  188. KTAP version 1
  189. 1..3
  190. KTAP version 1
  191. 1..1
  192. # test_1: initializing test_1
  193. ok 1 test_1
  194. ok 1 example_test_1
  195. KTAP version 1
  196. 1..2
  197. ok 1 test_1 # SKIP test_1 skipped
  198. ok 2 test_2
  199. ok 2 example_test_2
  200. KTAP version 1
  201. 1..3
  202. ok 1 test_1
  203. # test_2: FAIL
  204. not ok 2 test_2
  205. ok 3 test_3 # SKIP test_3 skipped
  206. not ok 3 example_test_3
  207. not ok 1 main_test
  208. This output defines the following hierarchy:
  209. A single test called "main_test", which fails, and has three subtests:
  210. - "example_test_1", which passes, and has one subtest:
  211. - "test_1", which passes, and outputs the diagnostic message "test_1: initializing test_1"
  212. - "example_test_2", which passes, and has two subtests:
  213. - "test_1", which is skipped, with the explanation "test_1 skipped"
  214. - "test_2", which passes
  215. - "example_test_3", which fails, and has three subtests
  216. - "test_1", which passes
  217. - "test_2", which outputs the diagnostic line "test_2: FAIL", and fails.
  218. - "test_3", which is skipped with the explanation "test_3 skipped"
  219. Note that the individual subtests with the same names do not conflict, as they
  220. are found in different parent tests. This output also exhibits some sensible
  221. rules for "bubbling up" test results: a test fails if any of its subtests fail.
  222. Skipped tests do not affect the result of the parent test (though it often
  223. makes sense for a test to be marked skipped if _all_ of its subtests have been
  224. skipped).
  225. See also:
  226. ---------
  227. - The TAP specification:
  228. https://testanything.org/tap-version-13-specification.html
  229. - The (stagnant) TAP version 14 specification:
  230. https://github.com/TestAnything/Specification/blob/tap-14-specification/specification.md
  231. - The kselftest documentation:
  232. Documentation/dev-tools/kselftest.rst
  233. - The KUnit documentation:
  234. Documentation/dev-tools/kunit/index.rst