The library provides functions for writing test scripts that pro‐ duce output consistent with the Test Anything Protocol. A test harness that parses this protocol can run these tests and produce useful reports indicating their success or failure. In the de‐ scriptions that follow, for any function that takes as the last two parameters it can be assumed that the is a ‐like format string, and the optional arguments are values to be placed in that string. You must first specify a test plan. This indicates how many tests you intend to run, and allows the test harness to notice if any tests were missed, or if the test program exited prematurely. To do this, use which always returns 0. The func‐ tion will cause your program to exit prematurely if you specify 0 tests. In some situations you may not know how many tests you will be running, or you are developing your test program, and do not want to update the parameter every time you make a change. For those situations use It returns 0, and indicates to the test harness that an indeterminate number of tests will be run. Both and will cause your test program to exit prematurely with a diag‐ nostic message if they are called more than once. If your test program detects at run time that some required functionality is missing (for example, it relies on a database connection which is not present, or a particular configuration option that has not been included in the running kernel) use passing as parameters a string to display indicating the reason for skipping the tests. Tests are implemented as expressions checked by calls to the and macros. In both cases should evaluate to true if the test suc‐ ceeded. allows you to specify a name, or comment, describing the test which will be included in the output. is for those times when the expression to be tested is self explanatory and does not need an associated comment. In those cases the test expression becomes the comment. These four calls are equivalent: int i = 5; ok(i == 5, "i equals 5"); /* Overly verbose */ ok(i == 5, "i equals %d", i); /* Just to demonstrate printf‐like behaviour of the test name */ ok(i == 5, "i == 5"); /* Needless repetition */ ok1(i == 5); /* Just right */ It is good practice to en‐ sure that the test name describes the meaning behind the test rather than what you are testing. Viz ok(db != NULL, "db is not NULL"); /* Not bad, but */ ok(db != NULL, "Database conn. succeeded"); /* this is better */ and return 1 if the ex‐ pression evaluated to true, and 0 if it evaluated to false. This lets you chain calls from to to only produce diagnostic output if the test failed. For example, this code will include diagnostic information about why the database connection failed, but only if the test failed. ok(db != NULL, "Database conn. succeeded") || diag("Database error code: %d", dberrno); You also have and From the Test::More documentation: Sometimes you just want to say that the tests have passed. Usually the case is you’ve got some complicated condition that is difficult to wedge into an ok(). In this case, you can simply use pass() (to declare the test ok) or fail (for not ok). Use these very, very, very sparingly. These are synonyms for ok(1, ...) and ok(0, ...). Sets of tests can be skipped. Ordi‐ narily you would do this because the test can’t be run in this particular testing environment. For example, suppose some tests should be run as root. If the test is not being run as root then the tests should be skipped. In this implementation, skipped tests are flagged as being ok, with a special message indicating that they were skipped. It is your responsibility to ensure that the number of tests skipped (the first parameter to is correct for the number of tests to skip. One way of implementing this is with a loop, or an construct, to ensure that there are no addi‐ tional side effects from the skipped tests. if(getuid() != 0) { skip(1, "because test only works as root"); } else { ok(do_something_as_root() == 0, "Did something as root"); } Two macros are provided to assist with this. The previous ex‐ ample could be re‐written as follows. skip_start(getuid() != 0, 1, "because test only works as root"); ok(do_something_as_root() == 0, "Did something as root"); skip_end; /* It’s a macro, no parentheses */ Sets of tests can be flagged as being These are tests that you expect to fail, probably because you haven’t fixed a bug, or finished a new fea‐ ture yet. These tests will still be run, but with additional output that indicates that they are expected to fail. Should a test start to succeed unexpectedly, tools like will indicate this, and you can move the test out of the todo block. This is much more useful than simply commenting out (or the tests. todo_start("dwim() not returning true yet"); ok(dwim(), "Did what the user wanted"); todo_end(); Should ever start succeeding you will know about it as soon as you run the tests. Note that the family, additional code between and executed. From the Test::More documentation; If it’s something the user might not be able to do, use SKIP. This includes optional modules that aren’t installed, running under an OS that doesn’t have some feature (like fork() or symlinks), or maybe you need an Internet connection and one isn’t available. If it’s something the programmer hasn’t done yet, use TODO. This is for any code you haven’t written yet, or bugs you have yet to fix, but want to put tests in your testing script (always a good idea). If your tests need to produce diagnostic output, use It ensures that the output will not be considered by the TAP test harness. adds the necessary trailing for you. diag("Expected return code 0, got return code %d", rcode); always returns 0. For maximum compatability your test program should return a par‐ ticular exit code. This is calculated by so it is sufficient to always return from with either or as appropriate. The directory in the source distribution contains numerous tests of functional‐ ity, written using Examine them for examples of how to construct test suites. strives to be compatible with the Perl Test::More and Test::Harness modules. The test suite verifies that is bug‐ for‐bug compatible with their behaviour. This is why some func‐ tions which would more naturally return nothing return constant values. If the is found at compile time, be thread safe. Indi‐ cations to the contrary (and test cases that expose incorrect be‐ haviour) are very welcome. requires a compiler. Some of the functionality is implemented as variadic macros, and that func‐ tionality was not formally codified until C99. Patches to use with earlier compilers that have their own implementation of variadic macros will be gratefully received. was written to help improve the quality and coverage of the FreeBSD regression test suite, and released in the hope that others find it a useful tool to help improve the quality of their code. would not exist with‐ out the efforts of and the countless others who have worked on the Perl QA programme. Ideally, running the tests would have no side effects on the behaviour of the application you are testing. However, it is not always possible to avoid them. The following side effects of using are known. stdout is set to unbuffered mode after calling any of the functions.