4
0

tap.3 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 int
  30. .Fn plan_tests "unsigned int"
  31. .Xc
  32. .It Xo
  33. .Ft int
  34. .Fn plan_no_plan "void"
  35. .Xc
  36. .It Xo
  37. .Ft int
  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. which always returns 0. The function will cause your program to exit
  49. prematurely if you specify 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 returns 0, and 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. ok(db != NULL, "Database conn. succeeded") ||
  139. diag("Database error code: %d", dberrno);
  140. .Ed
  141. .Pp
  142. You also have
  143. .Fn pass
  144. and
  145. .Fn fail .
  146. From the Test::More documentation:
  147. .Bd -literal -offset indent
  148. Sometimes you just want to say that the tests have passed.
  149. Usually the case is you've got some complicated condition
  150. that is difficult to wedge into an ok(). In this case,
  151. you can simply use pass() (to declare the test ok) or fail
  152. (for not ok).
  153. Use these very, very, very sparingly.
  154. .Ed
  155. .Pp
  156. These are synonyms for ok(1, ...) and ok(0, ...).
  157. .Ss SKIPPING TESTS
  158. .Bl -tag -width indent
  159. .It Xo
  160. .Ft int
  161. .Fn skip "unsigned int" "char *" "..."
  162. .Xc
  163. .It Xo
  164. .Fn skip_start "expression" "unsigned int" "char *" "..."
  165. .Xc
  166. .It Xo
  167. .Sy skip_end
  168. .Xc
  169. .El
  170. .Pp
  171. Sets of tests can be skipped. Ordinarily you would do this because
  172. the test can't be run in this particular testing environment.
  173. .Pp
  174. For example, suppose some tests should be run as root. If the test is
  175. not being run as root then the tests should be skipped. In this
  176. implementation, skipped tests are flagged as being ok, with a special
  177. message indicating that they were skipped. It is your responsibility
  178. to ensure that the number of tests skipped (the first parameter to
  179. .Fn skip )
  180. is correct for the number of tests to skip.
  181. .Pp
  182. One way of implementing this is with a
  183. .Dq do { } while(0);
  184. loop, or an
  185. .Dq if( ) { } else { }
  186. construct, to ensure that there are no additional side effects from the
  187. skipped tests.
  188. .Bd -literal -offset indent
  189. if(getuid() != 0) {
  190. skip(1, "because test only works as root");
  191. } else {
  192. ok(do_something_as_root() == 0, "Did something as root");
  193. }
  194. .Ed
  195. .Pp
  196. Two macros are provided to assist with this. The previous example could
  197. be re-written as follows.
  198. .Bd -literal -offset indent
  199. skip_start(getuid() != 0, 1, "because test only works as root");
  200. ok(do_something_as_root() == 0, "Did something as root");
  201. skip_end; /* It's a macro, no parentheses */
  202. .Ed
  203. .Ss MARKING TESTS AS Dq TODO
  204. .Bl -tag -width indent
  205. .It Xo
  206. .Ft void
  207. .Fn todo_start "char *" "..."
  208. .Xc
  209. .It Xo
  210. .Ft void
  211. .Fn todo_end "void"
  212. .Xc
  213. .El
  214. .Pp
  215. Sets of tests can be flagged as being
  216. .Dq TODO .
  217. These are tests that you expect to fail, probably because you haven't
  218. fixed a bug, or finished a new feature yet. These tests will still be
  219. run, but with additional output that indicates that they are expected
  220. to fail. Should a test start to succeed unexpectedly, tools like
  221. .Xr prove 1
  222. will indicate this, and you can move the test out of the todo
  223. block. This is much more useful than simply commenting out (or
  224. .Dq #ifdef 0 ... #endif )
  225. the tests.
  226. .Bd -literal -offset indent
  227. todo_start("dwim() not returning true yet");
  228. ok(dwim(), "Did what the user wanted");
  229. todo_end();
  230. .Ed
  231. .Pp
  232. Should
  233. .Fn dwim
  234. ever start succeeding you will know about it as soon as you run the
  235. tests. Note that
  236. .Em unlike
  237. the
  238. .Fn skip_*
  239. family, additional code between
  240. .Fn todo_start
  241. and
  242. .Fn todo_end
  243. .Em is
  244. executed.
  245. .Ss SKIP vs. TODO
  246. From the Test::More documentation;
  247. .Bd -literal -offset indent
  248. If it's something the user might not be able to do, use SKIP.
  249. This includes optional modules that aren't installed, running
  250. under an OS that doesn't have some feature (like fork() or
  251. symlinks), or maybe you need an Internet connection and one
  252. isn't available.
  253. If it's something the programmer hasn't done yet, use TODO.
  254. This is for any code you haven't written yet, or bugs you have
  255. yet to fix, but want to put tests in your testing script
  256. (always a good idea).
  257. .Ed
  258. .Ss DIAGNOSTIC OUTPUT
  259. .Bl -tag -width indent
  260. .It Xo
  261. .Fr unsigned int
  262. .Fn diag "char *" "..."
  263. .Xc
  264. .El
  265. .Pp
  266. If your tests need to produce diagnostic output, use
  267. .Fn diag .
  268. It ensures that the output will not be considered by the TAP test harness.
  269. .Fn diag
  270. adds the necessary trailing
  271. .Dq \en
  272. for you.
  273. .Bd -literal -offset indent
  274. diag("Expected return code 0, got return code %d", rcode);
  275. .Ed
  276. .Pp
  277. .Fn diag
  278. always returns 0.
  279. .Ss EXIT STATUS
  280. .Bl -tag -width indent
  281. .It Xo
  282. .Fr int
  283. .Fn exit_status void
  284. .Xc
  285. .El
  286. .Pp
  287. For maximum compatibility your test program should return a particular
  288. exit code. This is calculated by
  289. .Fn exit_status
  290. so it is sufficient to always return from
  291. .Fn main
  292. with either
  293. .Dq return exit_status();
  294. or
  295. .Dq exit(exit_status());
  296. as appropriate.
  297. .Sh EXAMPLES
  298. The
  299. .Pa tests
  300. directory in the source distribution contains numerous tests of
  301. .Nm
  302. functionality, written using
  303. .Nm .
  304. Examine them for examples of how to construct test suites.
  305. .Sh COMPATIBILITY
  306. .Nm
  307. strives to be compatible with the Perl Test::More and Test::Harness
  308. modules. The test suite verifies that
  309. .Nm
  310. is bug-for-bug compatible with their behaviour. This is why some
  311. functions which would more naturally return nothing return constant
  312. values.
  313. .Pp
  314. If the
  315. .Lb libpthread
  316. is found at compile time,
  317. .Nm
  318. .Em should
  319. be thread safe. Indications to the contrary (and test cases that expose
  320. incorrect behaviour) are very welcome.
  321. .Sh SEE ALSO
  322. .Xr Test::More 1 ,
  323. .Xr Test::Harness 1 ,
  324. .Xr prove 1
  325. .Sh STANDARDS
  326. .Nm
  327. requires a
  328. .St -isoC-99
  329. compiler. Some of the
  330. .Nm
  331. functionality is implemented as variadic macros, and that functionality
  332. was not formally codified until C99. Patches to use
  333. .Nm
  334. with earlier compilers that have their own implementation of variadic
  335. macros will be gratefully received.
  336. .Sh HISTORY
  337. .Nm
  338. was written to help improve the quality and coverage of the FreeBSD
  339. regression test suite, and released in the hope that others find it
  340. a useful tool to help improve the quality of their code.
  341. .Sh AUTHORS
  342. .An "Nik Clayton" Aq nik@ngo.org.uk ,
  343. .Aq nik@FreeBSD.org
  344. .Pp
  345. .Nm
  346. would not exist without the efforts of
  347. .An "Michael G Schwern" Aq schqern@pobox.com ,
  348. .An "Andy Lester" Aq andy@petdance.com ,
  349. and the countless others who have worked on the Perl QA programme.
  350. .Sh BUGS
  351. Ideally, running the tests would have no side effects on the behaviour
  352. of the application you are testing. However, it is not always possible
  353. to avoid them. The following side effects of using
  354. .Nm
  355. are known.
  356. .Bl -bullet -offset indent
  357. .It
  358. stdout is set to unbuffered mode after calling any of the
  359. .Fn plan_*
  360. functions.
  361. .El