6 .Nd write tests that implement the Test Anything Protocol
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.
17 In the descriptions that follow, for any function that takes as the
19 .Dq Fa char * , Fa ...
20 it can be assumed that the
24 -like format string, and the optional arguments are values to be placed
27 .Bl -tag -width indent
30 .Fn plan_tests "unsigned int"
34 .Fn plan_no_plan "void"
38 .Fn plan_skip_all "char *" "..."
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.
48 which always returns 0. The function will cause your program to exit
49 prematurely if you specify 0 tests.
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
54 parameter every time you make a change. For those situations use
56 It returns 0, and indicates to the test harness that an indeterminate number
63 will cause your test program to exit prematurely with a diagnostic
64 message if they are called more than once.
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
71 passing as parameters a string to display indicating the reason for skipping
74 .Bl -tag -width indent
77 .Fn ok "expression" "char *" "..."
85 .Fn pass "char *" "..."
89 .Fn fail "char *" "..."
93 Tests are implemented as expressions checked by calls to the
99 should evaluate to true if the test succeeded.
102 allows you to specify a name, or comment, describing the test which will
103 be included in the output.
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.
109 These four calls are equivalent:
110 .Bd -literal -offset indent
113 ok(i == 5, "i equals 5"); /* Overly verbose */
114 ok(i == 5, "i equals %d", i); /* Just to demonstrate printf-like
115 behaviour of the test name */
116 ok(i == 5, "i == 5"); /* Needless repetition */
117 ok1(i == 5); /* Just right */
120 It is good practice to ensure that the test name describes the meaning
121 behind the test rather than what you are testing. Viz
122 .Bd -literal -offset indent
123 ok(db != NULL, "db is not NULL"); /* Not bad, but */
124 ok(db != NULL, "Database conn. succeeded"); /* this is better */
130 return 1 if the expression evaluated to true, and 0 if it evaluated to
131 false. This lets you chain calls from
135 to only produce diagnostic output if the test failed. For example, this
136 code will include diagnostic information about why the database connection
137 failed, but only if the test failed.
138 .Bd -literal -offset indent
139 ok(db != NULL, "Database conn. succeeded") ||
140 diag("Database error code: %d", dberrno);
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
155 Use these very, very, very sparingly.
158 These are synonyms for ok(1, ...) and ok(0, ...).
160 .Bl -tag -width indent
163 .Fn skip "unsigned int" "char *" "..."
166 .Fn skip_start "expression" "unsigned int" "char *" "..."
173 Sets of tests can be skipped. Ordinarily you would do this because
174 the test can't be run in this particular testing environment.
176 For example, suppose some tests should be run as root. If the test is
177 not being run as root then the tests should be skipped. In this
178 implementation, skipped tests are flagged as being ok, with a special
179 message indicating that they were skipped. It is your responsibility
180 to ensure that the number of tests skipped (the first parameter to
182 is correct for the number of tests to skip.
184 One way of implementing this is with a
187 .Dq if( ) { } else { }
188 construct, to ensure that there are no additional side effects from the
190 .Bd -literal -offset indent
192 skip(1, "because test only works as root");
194 ok(do_something_as_root() == 0, "Did something as root");
198 Two macros are provided to assist with this. The previous example could
199 be re-written as follows.
200 .Bd -literal -offset indent
201 skip_start(getuid() != 0, 1, "because test only works as root");
203 ok(do_something_as_root() == 0, "Did something as root");
205 skip_end; /* It's a macro, no parentheses */
207 .Ss MARKING TESTS AS Dq TODO
208 .Bl -tag -width indent
211 .Fn todo_start "char *" "..."
219 Sets of tests can be flagged as being
221 These are tests that you expect to fail, probably because you haven't
222 fixed a bug, or finished a new feature yet. These tests will still be
223 run, but with additional output that indicates that they are expected
224 to fail. Should a test start to succeed unexpectedly, tools like
226 will indicate this, and you can move the test out of the todo
227 block. This is much more useful than simply commenting out (or
228 .Dq #ifdef 0 ... #endif )
230 .Bd -literal -offset indent
231 todo_start("dwim() not returning true yet");
233 ok(dwim(), "Did what the user wanted");
240 ever start succeeding you will know about it as soon as you run the
245 family, additional code between
252 From the Test::More documentation;
253 .Bd -literal -offset indent
254 If it's something the user might not be able to do, use SKIP.
255 This includes optional modules that aren't installed, running
256 under an OS that doesn't have some feature (like fork() or
257 symlinks), or maybe you need an Internet connection and one
260 If it's something the programmer hasn't done yet, use TODO.
261 This is for any code you haven't written yet, or bugs you have
262 yet to fix, but want to put tests in your testing script
263 (always a good idea).
265 .Ss DIAGNOSTIC OUTPUT
266 .Bl -tag -width indent
269 .Fn diag "char *" "..."
273 If your tests need to produce diagnostic output, use
275 It ensures that the output will not be considered by the TAP test harness.
277 adds the necessary trailing
280 .Bd -literal -offset indent
281 diag("Expected return code 0, got return code %d", rcode);
287 .Bl -tag -width indent
294 For maximum compatability your test program should return a particular
295 exit code. This is calculated by
297 so it is sufficient to always return from
300 .Dq return exit_status();
302 .Dq exit(exit_status());
307 directory in the source distribution contains numerous tests of
309 functionality, written using
311 Examine them for examples of how to construct test suites.
314 strives to be compatible with the Perl Test::More and Test::Harness
315 modules. The test suite verifies that
317 is bug-for-bug compatible with their behaviour. This is why some
318 functions which would more naturally return nothing return constant
323 is found at compile time,
326 be thread safe. Indications to the contrary (and test cases that expose
327 incorrect behaviour) are very welcome.
330 .Xr Test::Harness 1 ,
336 compiler. Some of the
338 functionality is implemented as variadic macros, and that functionality
339 was not formally codified until C99. Patches to use
341 with earlier compilers that have their own implementation of variadic
342 macros will be gratefully received.
345 was written to help improve the quality and coverage of the FreeBSD
346 regression test suite, and released in the hope that others find it
347 a useful tool to help improve the quality of their code.
349 .An "Nik Clayton" Aq nik@ngo.org.uk ,
353 would not exist without the efforts of
354 .An "Michael G Schwern" Aq schqern@pobox.com ,
355 .An "Andy Lester" Aq andy@petdance.com ,
356 and the countless others who have worked on the Perl QA programme.
358 Ideally, running the tests would have no side effects on the behaviour
359 of the application you are testing. However, it is not always possible
360 to avoid them. The following side effects of using
363 .Bl -bullet -offset indent
365 stdout is set to unbuffered mode after calling any of the