Error in the latest FindBin patch, noticed by Nicholas
[p5sagit/p5-mst-13.2.git] / lib / Test / Harness / TAP.pod
CommitLineData
3c87ea76 1=head1 NAME
2
3Test::Harness::TAP - Documentation for the TAP format
4
5=head1 SYNOPSIS
6
c0c1f8c2 7TAP, the Test Anything Protocol, is Perl's simple text-based interface
8between testing modules such as Test::More and the test harness
9Test::Harness.
3c87ea76 10
c0c1f8c2 11=head1 TODO
3c87ea76 12
c0c1f8c2 13Exit code of the process.
3c87ea76 14
15=head1 THE TAP FORMAT
16
c0c1f8c2 17TAP's general format is:
3c87ea76 18
c0c1f8c2 19 1..N
20 ok 1 Description # Directive
21 # Diagnostic
22 ....
23 ok 47 Description
24 ok 48 Description
25 more tests....
3c87ea76 26
c0c1f8c2 27For example, a test file's output might look like:
3c87ea76 28
c0c1f8c2 29 1..4
30 ok 1 - Input file opened
31 not ok 2 - First line of the input valid
32 ok 3 - Read the rest of the file
33 not ok 4 - Summarized correctly # TODO Not written yet
3c87ea76 34
c0c1f8c2 35=head1 HARNESS BEHAVIOR
36
37In this document, the "harness" is any program analyzing TAP output.
38Typically this will be Perl's I<prove> program, or the underlying
39C<Test::Harness::runtests> subroutine.
40
41A harness must only read TAP output from standard output and not
42from standard error. Lines written to standard output matching
43C</^(not )?ok\b/> must be interpreted as test lines. All other
44lines must not be considered test output.
3c87ea76 45
c0c1f8c2 46=head1 TESTS LINES AND THE PLAN
3c87ea76 47
c0c1f8c2 48=head2 The plan
3c87ea76 49
c0c1f8c2 50The plan tells how many tests will be run, or how many tests have
51run. It's a check that the test file hasn't stopped prematurely.
52It must appear once, whether at the beginning or end of the output.
3c87ea76 53
c0c1f8c2 54The plan is usually the first line of TAP output and it specifies how
55many test points are to follow. For example,
3c87ea76 56
c0c1f8c2 57 1..10
3c87ea76 58
c0c1f8c2 59means you plan on running 10 tests. This is a safeguard in case your test
60file dies silently in the middle of its run. The plan is optional but if
61there is a plan before the test points it must be the first non-diagnostic
62line output by the test file.
3c87ea76 63
c0c1f8c2 64In certain instances a test file may not know how many test points
65it will ultimately be running. In this case the plan can be the last
66non-diagnostic line in the output.
3c87ea76 67
c0c1f8c2 68The plan cannot appear in the middle of the output, nor can it appear more
69than once.
3c87ea76 70
c0c1f8c2 71=head2 The test line
72
73The core of TAP is the test line. A test file prints one test line test
74point executed. There must be at least one test line in TAP output. Each
75test line comprises the following elements:
76
77=over 4
3c87ea76 78
c0c1f8c2 79=item * C<ok> or C<not ok>
80
81This tells whether the test point passed or failed. It must be
82at the beginning of the line. C</^not ok/> indicates a failed test
83point. C</^ok/> is a successful test point. This is the only mandatory
84part of the line.
85
86Note that unlike the Directives below, C<ok> and C<not ok> are
87case-sensitive.
88
89=item * Test number
90
91TAP expects the C<ok> or C<not ok> to be followed by a test point
92number. If there is no number the harness must maintain
93its own counter until the script supplies test numbers again. So
94the following test output
3c87ea76 95
3c87ea76 96 1..6
97 not ok
98 ok
99 not ok
100 ok
101 ok
3c87ea76 102
c0c1f8c2 103has five tests. The sixth is missing. Test::Harness will generate
3c87ea76 104
105 FAILED tests 1, 3, 6
106 Failed 3/6 tests, 50.00% okay
107
c0c1f8c2 108=item * Description
109
110Any text after the test number but before a C<#> is the description of
111the test point.
3c87ea76 112
c0c1f8c2 113 ok 42 this is the description of the test
3c87ea76 114
c0c1f8c2 115Descriptions should not begin with a digit so that they are not confused
116with the test point number.
3c87ea76 117
c0c1f8c2 118The harness may do whatever it wants with the description.
3c87ea76 119
c0c1f8c2 120=item * Directive
3c87ea76 121
c0c1f8c2 122The test point may include a directive, following a hash on the
123test line. There are currently two directives allowed: C<TODO> and
124C<SKIP>. These are discussed below.
3c87ea76 125
c0c1f8c2 126=back
127
128To summarize:
129
130=over 4
131
132=item * ok/not ok (required)
133
134=item * Test number (recommended)
135
136=item * Description (recommended)
137
138=item * Directive (only when necessary)
139
140=back
3c87ea76 141
c0c1f8c2 142=head1 DIRECTIVES
3c87ea76 143
c0c1f8c2 144Directives are special notes that follow a C<#> on the test line.
145Only two are currently defined: C<TODO> and C<SKIP>. Note that
146these two keywords are not case-sensitive.
3c87ea76 147
c0c1f8c2 148=head2 TODO tests
3c87ea76 149
c0c1f8c2 150If the directive starts with C<# TODO>, the test is counted as a
151todo test, and the text after C<TODO> is the the explanation.
3c87ea76 152
c0c1f8c2 153 not ok 13 # TODO bend space and time
3c87ea76 154
c0c1f8c2 155Note that if the TODO has an explanation it must be separated from
156C<TODO> by a space.
3c87ea76 157
158These tests represent a feature to be implemented or a bug to be fixed
c0c1f8c2 159and act as something of an executable "things to do" list. They are
160B<not> expected to succeed. Should a todo test point begin succeeding,
161the harness should report it as a bonus. This indicates that whatever
3c87ea76 162you were supposed to do has been done and you should promote this to a
c0c1f8c2 163normal test point.
3c87ea76 164
c0c1f8c2 165=head2 Skipping tests
3c87ea76 166
c0c1f8c2 167If the directive starts with C<# SKIP>, the test is counted as having
168been skipped. If the whole test file succeeds, the count of skipped
169tests is included in the generated output. The harness should report
170the text after C< # SKIP\S*\s+> as a reason for skipping.
171
172 ok 23 # skip Insufficient flogiston pressure.
173
174Similarly, one can include an explanation in a plan line,
175emitted if the test file is skipped completely:
176
177 1..0 # Skipped: WWW::Mechanize not installed
178
179=head1 OTHER LINES
180
181=head2 Bail out!
182
183As an emergency measure a test script can decide that further tests
3c87ea76 184are useless (e.g. missing dependencies) and testing should stop
185immediately. In that case the test script prints the magic words
186
c0c1f8c2 187 Bail out!
3c87ea76 188
189to standard output. Any message after these words must be displayed
c0c1f8c2 190by the interpreter as the reason why testing must be stopped, as
191in
3c87ea76 192
c0c1f8c2 193 Bail out! MySQL is not running.
3c87ea76 194
c0c1f8c2 195=head2 Diagnostics
3c87ea76 196
c0c1f8c2 197Additional information may be put into the testing output on separate
198lines. Diagnostic lines should begin with a C<#>, which the harness must
199ignore, at least as far as analyzing the test results. The harness is
200free, however, to display the diagnostics. Typically diagnostics are
201used to provide information about the environment in which test file is
202running, or to delineate a group of tests.
203 ...
204 ok 18 - Closed database connection
205 # End of database section.
206 # This starts the network part of the test.
207 # Daemon started on port 2112
208 ok 19 - Opened socket
209 ...
210 ok 47 - Closed socket
211 # End of network tests
3c87ea76 212
c0c1f8c2 213=head2 Anything else
3c87ea76 214
c0c1f8c2 215Any output line that is not a plan, a test line or a diagnostic is
216incorrect. How a harness handles the incorrect line is undefined.
217Test::Harness silently ignores incorrect lines, but will become more
218stringent in the future.
3c87ea76 219
c0c1f8c2 220=head1 EXAMPLES
3c87ea76 221
c0c1f8c2 222All names, places, and events depicted in any example are wholly
223fictitious and bear no resemblance to, connection with, or relation to any
224real entity. Any such similarity is purely coincidental, unintentional,
225and unintended.
3c87ea76 226
c0c1f8c2 227=head2 Common with explanation
3c87ea76 228
c0c1f8c2 229The following TAP listing declares that six tests follow as well as
230provides handy feedback as to what the test is about to do. All six
231tests pass.
232
233 1..6
234 #
235 # Create a new Board and Tile, then place
236 # the Tile onto the board.
237 #
238 ok 1 - The object isa Board
239 ok 2 - Board size is zero
240 ok 3 - The object isa Tile
241 ok 4 - Get possible places to put the Tile
242 ok 5 - Placing the tile produces no error
243 ok 6 - Board size is 1
244
245=head2 Unknown amount and failures
246
247This hypothetical test program ensures that a handful of servers are
248online and network-accessible. Because it retrieves the hypothetical
249servers from a database, it doesn't know exactly how many servers it
250will need to ping. Thus, the test count is declared at the bottom after
251all the test points have run. Also, two of the tests fail.
252
253 ok 1 - retrieving servers from the database
254 # need to ping 6 servers
255 ok 2 - pinged diamond
256 ok 3 - pinged ruby
257 not ok 4 - pinged saphire
258 ok 5 - pinged onyx
259 not ok 6 - pinged quartz
260 ok 7 - pinged gold
261 1..7
262
263=head2 Giving up
264
265This listing reports that a pile of tests are going to be run. However,
266the first test fails, reportedly because a connection to the database
267could not be established. The program decided that continuing was
268pointless and exited.
269
270 1..573
271 not ok 1 - database handle
272 Bail out! Couldn't connect to database.
273
274=head2 Skipping a few
275
276The following listing plans on running 5 tests. However, our program
277decided to not run tests 2 thru 5 at all. To properly report this,
278the tests are marked as being skipped.
279
280 1..5
281 ok 1 - approved operating system
282 # $^0 is solaris
283 ok 2 - # SKIP no /sys directory
284 ok 3 - # SKIP no /sys directory
285 ok 4 - # SKIP no /sys directory
286 ok 5 - # SKIP no /sys directory
287
288=head2 Skipping everything
289
290This listing shows that the entire listing is a skip. No tests were run.
291
292 1..0 # skip because English-to-French translator isn't installed
293
294=head2 Got spare tuits?
295
296The following example reports that four tests are run and the last two
297tests failed. However, becauses the failing tests are marked as things
298to do later, they are considered successes. Thus, a harness should report
299this entire listing as a success.
300
301 1..4
302 ok 1 - Creating test program
303 ok 2 - Test program runs, no error
304 not ok 3 - infinite loop # TODO halting problem unsolved
305 not ok 4 - infinite loop 2 # TODO halting problem unsolved
306
307=head2 Creative liberties
308
309This listing shows an alternate output where the test numbers aren't
310provided. The test also reports the state of a ficticious board game in
311diagnostic form. Finally, the test count is reported at the end.
312
313 ok - created Board
314 ok
315 ok
316 ok
317 ok
318 ok
319 ok
320 ok
321 # +------+------+------+------+
322 # | |16G | |05C |
323 # | |G N C | |C C G |
324 # | | G | | C +|
325 # +------+------+------+------+
326 # |10C |01G | |03C |
327 # |R N G |G A G | |C C C |
328 # | R | G | | C +|
329 # +------+------+------+------+
330 # | |01G |17C |00C |
331 # | |G A G |G N R |R N R |
332 # | | G | R | G |
333 # +------+------+------+------+
334 ok - board has 7 tiles + starter tile
335 1..9
3c87ea76 336
337=head1 AUTHORS
338
339Andy Lester, based on the original Test::Harness documentation by Michael Schwern.
340
c0c1f8c2 341=head1 ACKNOWLEDGEMENTS
342
343Thanks to
344Pete Krawczyk,
345Paul Johnson,
346Ian Langworth
347and Nik Clayton
348for help and contributions on this document.
349
350The basis for the TAP format was created by Larry Wall in the
351original test script for Perl 1. Tim Bunce and Andreas Koenig
352developed it further with their modifications to Test::Harness.
353
3c87ea76 354=head1 COPYRIGHT
355
c0c1f8c2 356Copyright 2003-2005 by
3c87ea76 357Michael G Schwern C<< <schwern@pobox.com> >>,
358Andy Lester C<< <andy@petdance.com> >>.
359
360This program is free software; you can redistribute it and/or
361modify it under the same terms as Perl itself.
362
363See L<http://www.perl.com/perl/misc/Artistic.html>.
364
365=cut