4 libtap - Write tests in C
11 int foo () {return 3;}
12 char *bar () {return "fnord";}
18 ok(foo() <= 8732, "foo <= %d", 8732);
19 like(bar(), "f(yes|no)r*[a-f]$", "is like");
20 cmp_ok(foo(), ">=", 10, "foo is greater than ten");
29 # Failed test at synopsis.c line 9.
34 not ok 5 - foo is greater than ten
35 # Failed test 'foo is greater than ten'
36 # at synopsis.c line 12.
40 # Looks like you failed 2 tests of 5 run.
45 tap is an easy to read and easy to write way of creating tests for your
46 software. This library creates functions that can be used to generate it for
47 your C programs. It is mostly based on the Test::More Perl module.
55 - plan(SKIP_ALL, fmt, ...)
57 Use this to start a series of tests. When you know how many tests there
58 will be, you can put a number as a number of tests you expect to run. If
59 you do not know how many tests there will be, you can use plan(NO_PLAN)
60 or not call this function. When you pass it a number of tests to run, a
61 message similar to the following will appear in the output:
65 If you pass it SKIP_ALL, the whole test will be skipped.
70 Specify a test. the test can be any statement returning a true or false
71 value. You may optionally pass a format string describing the test.
73 ok(r = reader_new("Of Mice and Men"), "create a new reader");
74 ok(reader_go_to_page(r, 55), "can turn the page");
75 ok(r->page == 55, "page turned to the right one");
79 ok 1 - create a new reader
80 ok 2 - can turn the page
81 ok 3 - page turned to the right one
83 On failure, a diagnostic message will be printed out.
85 not ok 3 - page turned to the right one
86 # Failed test 'page turned to the right one'
87 # at reader.c line 13.
90 - is(got, expected, fmt, ...)
92 - isnt(got, expected, fmt, ...)
94 Tests that the string you got is what you expected. with isnt, it is the
97 is("this", "that", "this is that");
101 not ok 1 - this is that
102 # Failed test 'this is that'
108 - cmp_ok(a, op, b, fmt, ...)
110 Compares two ints with any binary operator that doesn't require an lvalue.
111 This is nice to use since it provides a better error message than an
114 cmp_ok(420, ">", 666);
119 # Failed test at cmpok.c line 5.
124 - like(got, expected)
125 - like(got, expected, fmt, ...)
126 - unlike(got, expected)
127 - unlike(got, expected, fmt, ...)
129 Tests that the string you got matches the expected extended POSIX regex.
130 unlike is the reverse. These macros are the equivalent of a skip on
133 like("stranger", "^s.(r).*\\1$", "matches the regex");
137 ok 1 - matches the regex
144 Speciy that a test succeeded or failed. Use these when the statement is
145 longer than you can fit into the argument given to an ok() test.
148 - dies_ok(code, fmt, ...)
150 - lives_ok(code, fmt, ...)
152 Tests whether the given code causes your program to exit. The code gets
153 passed to a macro that will test it in a forked process. If the code
154 succeeds it will be executed in the parent process. You can test things
155 like passing a function a null pointer and make sure it doesnt
156 dereference it and crash.
158 dies_ok({abort();}, "abort does close your program");
159 dies_ok({int x = 0/0;}, "divide by zero crash");
160 lives_ok({pow(3.0, 5.0);}, "nothing wrong with taking 3**5");
162 On Windows, these macros are the equivalent of a skip.
166 Summarizes the tests that occurred and exits the main function. If
167 there was no plan, it will print out the number of tests as.
171 It will also print a diagnostic message about how many
174 # Looks like you failed 2 tests of 3 run.
176 If all planned tests were successful, it will return 0. If any test fails,
177 it will return the number of failed tests (including ones that were
178 missing). If they all passed, but there were missing tests, it will return
184 print out a message to the tap output. note prints to stdout and diag
185 prints to stderr. Each line is preceeded by a "# " so that you know its a
188 note("This is\na note\nto describe\nsomething.");
197 ok() and these functions return ints so you can use them like:
199 ok(1) && note("yo!");
200 ok(0) || diag("I have no idea what just happened");
203 - skip(test, n, fmt, ...)
206 Skip a series of n tests if test is true. You may give a reason why you are
207 skipping them or not. The (possibly) skipped tests must occur between the
208 skip and endskip macros.
224 Specifies a series of tests that you expect to fail because they are not
234 # Failed (TODO) test at todo.c line 7
239 Immediately stops all testing.
241 BAIL_OUT("Can't go no further");
245 Bail out! Can't go no further