tap.3 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. .Dd December 20, 2004
  2. .Os
  3. .Dt TAP 3
  4. .Sh NAME
  5. .Nm tap
  6. .Nd write tests that implement the Test Anything Protocol
  7. .Sh SYNOPSIS
  8. .In tap.h
  9. .Sh DESCRIPTION
  10. The
  11. .Nm
  12. library provides functions for writing test scripts that produce output
  13. consistent with the Test Anything Protocol. A test harness that parses
  14. this protocol can run these tests and produce useful reports indicating
  15. their success or failure.
  16. .Ss PRINTF STRINGS
  17. In the descriptions that follow, for any function that takes as the
  18. last two parameters
  19. .Dq Fa char * , Fa ...
  20. it can be assumed that the
  21. .Fa char *
  22. is a
  23. .Fn printf
  24. -like format string, and the optional arguments are values to be placed
  25. in that string.
  26. .Ss TEST PLANS
  27. .Bl -tag -width indent
  28. .It Xo
  29. .Ft void
  30. .Fn plan_tests "unsigned int"
  31. .Xc
  32. .It Xo
  33. .Ft void
  34. .Fn plan_no_plan "void"
  35. .Xc
  36. .It Xo
  37. .Ft void
  38. .Fn plan_skip_all "char *" "..."
  39. .Xc
  40. .El
  41. .Pp
  42. You must first specify a test plan. This indicates how many tests you
  43. intend to run, and allows the test harness to notice if any tests were
  44. missed, or if the test program exited prematurely.
  45. .Pp
  46. To do this, use
  47. .Fn plan_tests .
  48. The function will cause your program to exit prematurely if you specify
  49. 0 tests.
  50. .Pp
  51. In some situations you may not know how many tests you will be running, or
  52. you are developing your test program, and do not want to update the
  53. .Fn plan_tests
  54. parameter every time you make a change. For those situations use
  55. .Fn plan_no_plan .
  56. It indicates to the test harness that an indeterminate number
  57. of tests will be run.
  58. .Pp
  59. Both
  60. .Fn plan_tests
  61. and
  62. .Fn plan_no_plan
  63. will cause your test program to exit prematurely with a diagnostic
  64. message if they are called more than once.
  65. .Pp
  66. If your test program detects at run time that some required functionality
  67. is missing (for example, it relies on a database connection which is not
  68. present, or a particular configuration option that has not been included
  69. in the running kernel) use
  70. .Fn plan_skip_all ,
  71. passing as parameters a string to display indicating the reason for skipping
  72. the tests.
  73. .Ss SIMPLE TESTS
  74. .Bl -tag -width indent
  75. .It Xo
  76. .Ft unsigned int
  77. .Fn ok "expression" "char *" "..."
  78. .Xc
  79. .It Xo
  80. .Ft unsigned int
  81. .Fn ok1 "expression"
  82. .Xc
  83. .It Xo
  84. .Ft unsigned int
  85. .Fn pass "char *" "..."
  86. .Xc
  87. .It Xo
  88. .Ft unsigned int
  89. .Fn fail "char *" "..."
  90. .Xc
  91. .El
  92. .Pp
  93. Tests are implemented as expressions checked by calls to the
  94. .Fn ok
  95. and
  96. .Fn ok1
  97. macros. In both cases
  98. .Fa expression
  99. should evaluate to true if the test succeeded.
  100. .Pp
  101. .Fn ok
  102. allows you to specify a name, or comment, describing the test which will
  103. be included in the output.
  104. .Fn ok1
  105. is for those times when the expression to be tested is self
  106. explanatory and does not need an associated comment. In those cases
  107. the test expression becomes the comment.
  108. .Pp
  109. These four calls are equivalent:
  110. .Bd -literal -offset indent
  111. int i = 5;
  112. ok(i == 5, "i equals 5"); /* Overly verbose */
  113. ok(i == 5, "i equals %d", i); /* Just to demonstrate printf-like
  114. behaviour of the test name */
  115. ok(i == 5, "i == 5"); /* Needless repetition */
  116. ok1(i == 5); /* Just right */
  117. .Ed
  118. .Pp
  119. It is good practice to ensure that the test name describes the meaning
  120. behind the test rather than what you are testing. Viz
  121. .Bd -literal -offset indent
  122. ok(db != NULL, "db is not NULL"); /* Not bad, but */
  123. ok(db != NULL, "Database conn. succeeded"); /* this is better */
  124. .Ed
  125. .Pp
  126. .Fn ok
  127. and
  128. .Fn ok1
  129. return 1 if the expression evaluated to true, and 0 if it evaluated to
  130. false. This lets you chain calls from
  131. .Fn ok
  132. to
  133. .Fn diag
  134. to only produce diagnostic output if the test failed. For example, this
  135. code will include diagnostic information about why the database connection
  136. failed, but only if the test failed.
  137. .Bd -literal -offset indent
  138. if (!ok(db != NULL, "Database conn. succeeded")) {
  139. diag("Database error code: %d", dberrno);
  140. }
  141. .Ed
  142. .Pp
  143. You also have
  144. .Fn pass
  145. and
  146. .Fn fail .
  147. From the Test::More documentation:
  148. .Bd -literal -offset indent
  149. Sometimes you just want to say that the tests have passed.
  150. Usually the case is you've got some complicated condition
  151. that is difficult to wedge into an ok(). In this case,
  152. you can simply use pass() (to declare the test ok) or fail
  153. (for not ok).
  154. Use these very, very, very sparingly.
  155. .Ed
  156. .Pp
  157. These are synonyms for ok(1, ...) and ok(0, ...).
  158. .Ss SKIPPING TESTS
  159. .Bl -tag -width indent
  160. .It Xo
  161. .Ft void
  162. .Fn skip "unsigned int" "char *" "..."
  163. .Xc
  164. .It Xo
  165. .Fn skip_if "expression" "unsigned int" "char *" "..."
  166. .Xc
  167. .El
  168. .Pp
  169. Sets of tests can be skipped. Ordinarily you would do this because
  170. the test can't be run in this particular testing environment.
  171. .Pp
  172. For example, suppose some tests should be run as root. If the test is
  173. not being run as root then the tests should be skipped. In this
  174. implementation, skipped tests are flagged as being ok, with a special
  175. message indicating that they were skipped. It is your responsibility
  176. to ensure that the number of tests skipped (the first parameter to
  177. .Fn skip )
  178. is correct for the number of tests to skip.
  179. .Pp
  180. One way of implementing this is with a
  181. .Dq do { } while(0);
  182. loop, or an
  183. .Dq if( ) { } else { }
  184. construct, to ensure that there are no additional side effects from the
  185. skipped tests.
  186. .Bd -literal -offset indent
  187. if(getuid() != 0) {
  188. skip(1, "because test only works as root");
  189. } else {
  190. ok(do_something_as_root() == 0, "Did something as root");
  191. }
  192. .Ed
  193. .Pp
  194. A convenient macro is provided to assist with this. The previous example could
  195. be re-written as follows.
  196. .Bd -literal -offset indent
  197. skip_if(getuid() != 0, 1, "because test only works as root") {
  198. ok(do_something_as_root() == 0, "Did something as root");
  199. }
  200. .Ed
  201. .Ss MARKING TESTS AS Dq TODO
  202. .Bl -tag -width indent
  203. .It Xo
  204. .Ft void
  205. .Fn todo_start "char *" "..."
  206. .Xc
  207. .It Xo
  208. .Ft void
  209. .Fn todo_end "void"
  210. .Xc
  211. .El
  212. .Pp
  213. Sets of tests can be flagged as being
  214. .Dq TODO .
  215. These are tests that you expect to fail, probably because you haven't
  216. fixed a bug, or finished a new feature yet. These tests will still be
  217. run, but with additional output that indicates that they are expected
  218. to fail. Should a test start to succeed unexpectedly, tools like
  219. .Xr prove 1
  220. will indicate this, and you can move the test out of the todo
  221. block. This is much more useful than simply commenting out (or
  222. .Dq #ifdef 0 ... #endif )
  223. the tests.
  224. .Bd -literal -offset indent
  225. todo_start("dwim() not returning true yet");
  226. ok(dwim(), "Did what the user wanted");
  227. todo_end();
  228. .Ed
  229. .Pp
  230. Should
  231. .Fn dwim
  232. ever start succeeding you will know about it as soon as you run the
  233. tests. Note that
  234. .Em unlike
  235. the
  236. .Fn skip_*
  237. family, additional code between
  238. .Fn todo_start
  239. and
  240. .Fn todo_end
  241. .Em is
  242. executed.
  243. .Ss SKIP vs. TODO
  244. From the Test::More documentation;
  245. .Bd -literal -offset indent
  246. If it's something the user might not be able to do, use SKIP.
  247. This includes optional modules that aren't installed, running
  248. under an OS that doesn't have some feature (like fork() or
  249. symlinks), or maybe you need an Internet connection and one
  250. isn't available.
  251. If it's something the programmer hasn't done yet, use TODO.
  252. This is for any code you haven't written yet, or bugs you have
  253. yet to fix, but want to put tests in your testing script
  254. (always a good idea).
  255. .Ed
  256. .Ss DIAGNOSTIC OUTPUT
  257. .Bl -tag -width indent
  258. .It Xo
  259. .Fr void
  260. .Fn diag "char *" "..."
  261. .Xc
  262. .El
  263. .Pp
  264. If your tests need to produce diagnostic output, use
  265. .Fn diag .
  266. It ensures that the output will not be considered by the TAP test harness.
  267. .Fn diag
  268. adds the necessary trailing
  269. .Dq \en
  270. for you.
  271. .Bd -literal -offset indent
  272. diag("Expected return code 0, got return code %d", rcode);
  273. .Ed
  274. .Ss EXIT STATUS
  275. .Bl -tag -width indent
  276. .It Xo
  277. .Fr int
  278. .Fn exit_status void
  279. .Xc
  280. .El
  281. .Pp
  282. For maximum compatability your test program should return a particular
  283. exit code. This is calculated by
  284. .Fn exit_status
  285. so it is sufficient to always return from
  286. .Fn main
  287. with either
  288. .Dq return exit_status();
  289. or
  290. .Dq exit(exit_status());
  291. as appropriate.
  292. .Sh EXAMPLES
  293. The
  294. .Pa tests
  295. directory in the source distribution contains numerous tests of
  296. .Nm
  297. functionality, written using
  298. .Nm .
  299. Examine them for examples of how to construct test suites.
  300. .Sh COMPATABILITY
  301. .Nm
  302. strives to be compatible with the Perl Test::More and Test::Harness
  303. modules. The test suite verifies that
  304. .Nm
  305. is bug-for-bug compatible with their behaviour. This is why some
  306. functions which would more naturally return nothing return constant
  307. values.
  308. .Pp
  309. If the
  310. .Lb libpthread
  311. is found at compile time,
  312. .Nm
  313. .Em should
  314. be thread safe. Indications to the contrary (and test cases that expose
  315. incorrect behaviour) are very welcome.
  316. .Sh SEE ALSO
  317. .Xr Test::More 1 ,
  318. .Xr Test::Harness 1 ,
  319. .Xr prove 1
  320. .Sh STANDARDS
  321. .Nm
  322. requires a
  323. .St -isoC-99
  324. compiler. Some of the
  325. .Nm
  326. functionality is implemented as variadic macros, and that functionality
  327. was not formally codified until C99. Patches to use
  328. .Nm
  329. with earlier compilers that have their own implementation of variadic
  330. macros will be gratefully received.
  331. .Sh HISTORY
  332. .Nm
  333. was written to help improve the quality and coverage of the FreeBSD
  334. regression test suite, and released in the hope that others find it
  335. a useful tool to help improve the quality of their code.
  336. .Sh AUTHORS
  337. .An "Nik Clayton" Aq nik@ngo.org.uk ,
  338. .Aq nik@FreeBSD.org
  339. .Pp
  340. .Nm
  341. would not exist without the efforts of
  342. .An "Michael G Schwern" Aq schqern@pobox.com ,
  343. .An "Andy Lester" Aq andy@petdance.com ,
  344. and the countless others who have worked on the Perl QA programme.
  345. .Sh BUGS
  346. Ideally, running the tests would have no side effects on the behaviour
  347. of the application you are testing. However, it is not always possible
  348. to avoid them. The following side effects of using
  349. .Nm
  350. are known.
  351. .Bl -bullet -offset indent
  352. .It
  353. stdout is set to unbuffered mode after calling any of the
  354. .Fn plan_*
  355. functions.
  356. .El