|
| 1 | +# NAME |
| 2 | + |
| 3 | +libtap++ - C++ unit tests for the Test Anything Protocol |
| 4 | + |
| 5 | +# SYNOPSIS |
| 6 | + |
| 7 | + #include <tap++/tap++.h> |
| 8 | + #include <string> |
| 9 | + |
| 10 | + using namespace TAP; |
| 11 | + |
| 12 | + int foo(int ronkle = 1) { |
| 13 | + return ronkle; |
| 14 | + } |
| 15 | + |
| 16 | + std::string bar() { |
| 17 | + return "a string"; |
| 18 | + } |
| 19 | + |
| 20 | + int main() { |
| 21 | + plan(4); |
| 22 | + ok(true, "This test passes"); |
| 23 | + is(foo(), 1, "foo() should be 1"); |
| 24 | + is(bar(), "a string", "bar() should be \"a string\""); |
| 25 | + |
| 26 | + TODO="Foo is not completely implemented"; |
| 27 | + is(foo(2), 5, "foo(2) should be 5"); |
| 28 | + TODO=""; |
| 29 | + |
| 30 | + return exit_status(); |
| 31 | + } |
| 32 | + |
| 33 | +# DESCRIPTION |
| 34 | + |
| 35 | +`libtap++` is a TAP producer for C++ programs. |
| 36 | + |
| 37 | +# HISTORY |
| 38 | + |
| 39 | +libtap++ was originally developed as a part of |
| 40 | +[libperl++](https://github.com/Leont/libperl--) project by Leon Timmermans |
| 41 | + |
| 42 | +Then forked into separate [libtap++](https://github.com/cbab/libtappp) |
| 43 | +project by Christian Babeux |
| 44 | + |
| 45 | +Now libtap++ us supported by Nikolay Shaplov at |
| 46 | +[https://gitlab.com/dhyannataraj/libtappp](https://gitlab.com/dhyannataraj/libtappp) |
| 47 | + |
| 48 | +# FUNCTIONS |
| 49 | + |
| 50 | +All functions and variables are defined in the `TAP` namespace. |
| 51 | + |
| 52 | +## I love it when a plan comes together |
| 53 | + |
| 54 | +Before anything else, you need a testing plan. This basically declares |
| 55 | +how many tests your script is going to run to protect against premature |
| 56 | +failure. |
| 57 | + |
| 58 | +- **plan()** |
| 59 | + |
| 60 | + void plan(int number_of_tests); |
| 61 | + void plan(skip_all, const std::string& reason=""); |
| 62 | + void plan(no_plan); |
| 63 | + |
| 64 | + The function `plan` is used to indicate the plan of your test run. Usually you |
| 65 | + will just give it the number of tests as argument. |
| 66 | + |
| 67 | + Alternatively, you can give it the `skip_all` or `no_plan` constants as |
| 68 | + arguments. The first means you will not run the tests at all, the second means |
| 69 | + you will run an unknown number of tests (the latter is not recommended). |
| 70 | + |
| 71 | +- **done\_testing()** |
| 72 | + |
| 73 | + void done_testing(); |
| 74 | + void done_testing(int number_of_tests); |
| 75 | + |
| 76 | + If you don't know how many tests you're going to run, you can issue the plan |
| 77 | + when you're done running tests. |
| 78 | + |
| 79 | + number\_of\_tests is the same as plan(), it's the number of tests you expected to |
| 80 | + run. You can omit this, in which case the number of tests you ran doesn't |
| 81 | + matter, just the fact that your tests ran to conclusion. |
| 82 | + |
| 83 | + This is safer than and replaces the "no\_plan" plan. |
| 84 | + |
| 85 | +## Test names |
| 86 | + |
| 87 | +By convention, each test is assigned a number in order. This is |
| 88 | +largely done automatically for you. However, it's often very useful to |
| 89 | +assign a name to each test. Which would you rather see: |
| 90 | + |
| 91 | + ok 4 |
| 92 | + not ok 5 |
| 93 | + ok 6 |
| 94 | + |
| 95 | +or |
| 96 | + |
| 97 | + ok 4 - basic multi-variable |
| 98 | + not ok 5 - simple exponential |
| 99 | + ok 6 - force == mass * acceleration |
| 100 | + |
| 101 | +The later gives you some idea of what failed. It also makes it easier |
| 102 | +to find the test in your script, simply search for "simple |
| 103 | +exponential". |
| 104 | + |
| 105 | +All test functions take a name argument. It's optional, but highly |
| 106 | +suggested that you use it. |
| 107 | + |
| 108 | +## I'm ok, you're not ok. |
| 109 | + |
| 110 | +The basic purpose of this module is to print out either "ok #" or "not |
| 111 | +ok #" depending on if a given test succeeded or failed. Everything |
| 112 | +else is just gravy. |
| 113 | + |
| 114 | +All of the following print "ok" or "not ok" depending on if the test |
| 115 | +succeeded or failed. They all also return true or false, |
| 116 | +respectively. |
| 117 | + |
| 118 | +- **ok()** |
| 119 | + |
| 120 | + bool ok(bool condition, const std::string& test_name = ""); |
| 121 | + |
| 122 | + `ok` is the basic test expression in TAP. It simply evaluates any expression, |
| 123 | + for example, _got == expected_, taking a true value to mean that the test |
| 124 | + passed and a false value to mean that the test failed. |
| 125 | + |
| 126 | + `test_name` is a very short description of the test that will be printed out. |
| 127 | + It makes it very easy to find a test in your script when it fails and gives |
| 128 | + others an idea of your intentions. `test_name` is optional, but we very |
| 129 | + strongly encourage its use. |
| 130 | + |
| 131 | +- **is()** |
| 132 | +- **isnt()** |
| 133 | + |
| 134 | + template<typename T, typename U> bool is(const T& got, const U& expected, std::string& test_name = ""); |
| 135 | + template<typename T, typename U> bool isnt(const T& got, const U& expected, std::string& test_name = ""); |
| 136 | + |
| 137 | + Similar to ok(), is() and isnt() compare their two arguments |
| 138 | + with `==` and `!=` respectively and use the result of that to |
| 139 | + determine if the test succeeded or failed. So these: |
| 140 | + |
| 141 | + # Is the ultimate answer 42? |
| 142 | + is( ultimate_answer(), 42, "Meaning of Life" ); |
| 143 | + |
| 144 | + # foo isn't empty |
| 145 | + isnt( foo, "", "Got some foo" ); |
| 146 | + |
| 147 | + are similar to these: |
| 148 | + |
| 149 | + ok( ultimate_answer() == 42, "Meaning of Life" ); |
| 150 | + ok( foo != "", "Got some foo" ); |
| 151 | + |
| 152 | + (Mnemonic: "This is that." "This isn't that.") |
| 153 | + |
| 154 | + So why use these? They produce better diagnostics on failure. ok() cannot know |
| 155 | + what you are testing for (beyond the name), but is() and isnt() know what the |
| 156 | + test was and why it failed. For example this test: |
| 157 | + |
| 158 | + std::string foo("waffle"), bar("yarblokos"); |
| 159 | + is( foo, bar, 'Is foo the same as bar?' ); |
| 160 | + |
| 161 | + Will produce something like this: |
| 162 | + |
| 163 | + not ok 17 - Is foo the same as bar? |
| 164 | + # Failed test 'Is foo the same as bar?' |
| 165 | + # got: 'waffle' |
| 166 | + # expected: 'yarblokos' |
| 167 | + |
| 168 | +- **pass()** |
| 169 | +- **fail()** |
| 170 | + |
| 171 | + bool pass(const std::string& test_name = ""); |
| 172 | + bool fail(const std::string& test_name = ""); |
| 173 | + |
| 174 | + Sometimes you just want to say that the tests have passed. Usually |
| 175 | + the case is you've got some complicated condition that is difficult to |
| 176 | + wedge into an ok(). In this case, you can simply use pass() (to |
| 177 | + declare the test ok) or fail (for not ok). They are synonyms for |
| 178 | + ok(true, test\_name) and ok(false, test\_name). |
| 179 | + |
| 180 | + Use these very, very, very sparingly. |
| 181 | + |
| 182 | +## Conditional testing |
| 183 | + |
| 184 | +- **skip()** |
| 185 | + |
| 186 | + void skip(int number, const std::string& reason = ""); |
| 187 | + |
| 188 | + `skip` tells the TAP harness that you're skipping a _number_ of tests for the |
| 189 | + given _reason_. Note that you have to do the skipping yourself. |
| 190 | + |
| 191 | +- **TODO** |
| 192 | + |
| 193 | + { |
| 194 | + todo_guard why; |
| 195 | + TODO="why" |
| 196 | + my_tests_here ... |
| 197 | + } |
| 198 | + |
| 199 | + `TODO` is a global string variable that tells TAP harness the reason |
| 200 | + the current test is expected to fail. You set TODO before a block of |
| 201 | + tests that you expect to fail and then unset it afterwards. When TODO |
| 202 | + is the empty string, then the harness considers that there is no |
| 203 | + reason for the test to fail. However, when TODO is non-empty, any |
| 204 | + failing test is not counted against the test suite and any succeeding |
| 205 | + test is reported as an unexpected success. |
| 206 | + |
| 207 | + The nice part about todo tests, as opposed to simply commenting out a |
| 208 | + block of tests, is it's like having a programmatic todo list. You know |
| 209 | + how much work is left to be done, you're aware of what bugs there are, |
| 210 | + and you'll know immediately when they're fixed. |
| 211 | + |
| 212 | + Note that TODO manipulates a global variable. Thus, you should be |
| 213 | + careful to set it to "" before going to another section of the |
| 214 | + program. An easy mistake to make is to have a failing section of code |
| 215 | + that throws an exception, taking you out of the current scope without |
| 216 | + resetting TODO. To make it easier to deal with this in a thread-safe |
| 217 | + manner, the todo\_guard class is provided. Objects of this class will |
| 218 | + reset TODO when they fall out of scope. |
| 219 | + |
| 220 | +## Diagnostics |
| 221 | + |
| 222 | +If you pick the right test function, you'll usually get a good idea of |
| 223 | +what went wrong when it failed. But sometimes it doesn't work out |
| 224 | +that way. So here we have ways for you to write your own diagnostic |
| 225 | +messages which are safer than just `print STDERR`. |
| 226 | + |
| 227 | +- **diag** |
| 228 | + |
| 229 | + diag(diagnostic_message...); |
| 230 | + |
| 231 | + Prints a diagnostic message which is guaranteed not to interfere with |
| 232 | + test output. The arguments are simply concatenated together. |
| 233 | + |
| 234 | + Returns false, so as to preserve failure. |
| 235 | + |
| 236 | + Handy for this sort of thing: |
| 237 | + |
| 238 | + ok( has_user("foo"), "There's a foo user" ) or |
| 239 | + diag("Since there's no foo, check that /etc/bar is set up right"); |
| 240 | + |
| 241 | + which would produce: |
| 242 | + |
| 243 | + not ok 42 - There's a foo user |
| 244 | + # Failed test 'There's a foo user' |
| 245 | + # Since there's no foo, check that /etc/bar is set up right. |
| 246 | + |
| 247 | + You might remember `ok() or diag()` with the mnemonic `open() or |
| 248 | + die()`. |
| 249 | + |
| 250 | + **NOTE** The exact formatting of the diagnostic output is still |
| 251 | + changing, but it is guaranteed that whatever you throw at it it won't |
| 252 | + interfere with the test. |
| 253 | + |
| 254 | +- **note** |
| 255 | + |
| 256 | + note(diagnostic_message...); |
| 257 | + |
| 258 | + Like diag(), except the message will not be seen when the test is run |
| 259 | + in a harness. It will only be visible in the verbose TAP stream. |
| 260 | + |
| 261 | + Handy for putting in notes which might be useful for debugging, but |
| 262 | + don't indicate a problem. |
| 263 | + |
| 264 | + note("Tempfile is ", tempfile); |
| 265 | + |
| 266 | + `diag` simply catenates its arguments to the error output, while `note` |
| 267 | + prints diagnostics to the TAP stream. |
| 268 | + |
| 269 | +- **set\_output()** |
| 270 | +- **set\_error()** |
| 271 | + |
| 272 | + void set_output(std::ofstream& new_output); |
| 273 | + void set_error(std::ofstream& new_error); |
| 274 | + |
| 275 | + These set the filehandle of the TAP stream and the error stream. They default |
| 276 | + to `std::cout` and `std::cerr`, respectively. These can only be set before any |
| 277 | + output is written to them. |
| 278 | + |
| 279 | +## Ending a test run |
| 280 | + |
| 281 | +- **exit\_status()** |
| 282 | + |
| 283 | + If all your tests passed, Test::Builder will exit with zero (which is |
| 284 | + normal). If anything failed it will exit with how many failed. If |
| 285 | + you run less (or more) tests than you planned, the missing (or extras) |
| 286 | + will be considered failures. If the test died, even after having |
| 287 | + successfully completed all its tests, it will still be considered a failure |
| 288 | + and will exit with 255. |
| 289 | + |
| 290 | + So the exit codes are... |
| 291 | + |
| 292 | + 0 all tests successful |
| 293 | + 255 test died or all passed but wrong # of tests run |
| 294 | + any other number how many failed (including missing or extras) |
| 295 | + |
| 296 | + If you fail more than 254 tests, it will be reported as 254. |
| 297 | + |
| 298 | +- **bail\_out()** |
| 299 | + |
| 300 | + int exit_status(); |
| 301 | + void bail_out(const std::string& reason); |
| 302 | + |
| 303 | + **bail\_out** terminates the current test program with exit code 255, indicating |
| 304 | + to the test harness that all subsequent testing should halt. Typically this |
| 305 | + is used to indicate that testing cannot continue at all. |
| 306 | + |
| 307 | +# SEE ALSO |
| 308 | + |
| 309 | +[http://www.testanything.org](http://www.testanything.org) |
| 310 | + |
| 311 | +[Test::More](https://metacpan.org/pod/Test%3A%3AMore) and [prove(1)](http://man.he.net/man1/prove) are the traditional perl client library and TAP |
| 312 | +harness, respectively. This library is modeled after Test::More. |
| 313 | + |
| 314 | +# AUTHORS |
| 315 | + |
| 316 | +Leon Timmermans wrote `libtap++`. He stole much of this documentation from |
| 317 | +Test::More. Mike Pomraning also contributed this documentation. |
| 318 | + |
| 319 | +# COPYRIGHT |
| 320 | + |
| 321 | +Copyright (c) 2008, 2009, 2010 Leon Timmermans. |
| 322 | + |
| 323 | +See the LICENSE file for details. |
0 commit comments