Introduction
libtap++ is a TAP producer for C++ programs.
Overview
libtap++ defines the following functions:
void plan(int number);
void plan(skip_all, const std::string& reason= "");
void plan(no_plan);
The function plan is used to indicate the plan of your test run. Usually you will just give it the number of tests as argument.
Alternatively, you can give it skip_all or no_plan as arguments. The first means you will not run the tests at all, the second means you will run an unknown number of tests (the latter is not recommended).
bool ok(bool condition, const std::string& test_name = "");
ok simply evaluates any expression (for example got == expected) and uses that to determine if the test succeeded or failed. A true expression passes, a false one fails.
template<typename T, typename U> bool is(const T& left, const U& right, std::string& test_name = "");
template<typename T, typename U> bool isnt(const T& left, const U& right, std::string& test_name = "");
These functions compare two values, and use the result of that to determine if the test succeeded or failed.
void skip(int number, const std::string& reason = "");
This function indicates you're skipping a number of tests (note that you have to do the skipping yourself, this just tells the TAP reader that you've done it).
void diag(...);
void note(...);
These functions print out diagnostic information. diag prints it to the error output, while note prints it into the TAP stream.
void set_output(std::ofstream& new_output);
void set_error(std::ofstream& new_error);
These set the filehandle of the TAP stream and the error stream, respectively.
bool pass(const std::string& test_name = "");
bool fail(const std::string& test_name = "");
These are equal to ok(true, test_name) and ok(false, test_name). Use these very, very, very sparingly.
int exit_status();
Return the appropriate exit status (255 if it encountered issues, else the number of failed test, thus 0 on success).
void bail_out(const std::string& reason);
bail_out indicates to the harness that things are going so badly all testing should terminate. This includes the running any additional test scripts. This is typically used when testing cannot continue such as a critical module failing to load or a necessary external utility not being available such as a database connection failing. The test will exit with 255.
Example
#include <tap++.h>
#include <string>
using namespace TAP;
int foo() {
return 1;
}
std::string bar() {
return "Hello World";
}
int main() {
plan(3);
ok(true, "We like true values");
is(foo, 1, "foo() should be 1");
is(bar(), "Hello World", "bar() should be \"Hello World\"");
return exit_status();
}