Example of how to run tests, without any test cases.
Example of how to run tests, without any test cases.
#include "ExpectedFailureLogger.h"
#include <array>
#include <iostream>
#include <vector>
namespace
{
void doHelp(
const std::string& program,
const std::string& error = std::string(""),
const std::string& errorInfo = std::string("")
)
{
std::cout
<< program << " -- Run Unit Tests\n"
<< "\n"
<< "Runs the automatically build=defined unit tests.\n"
<< "\tExit code is number of failed tests, or 1 for program error.\n"
<< "\n"
<< "Usage: " << program << " [<options>] [<test>...]\n"
<< "\n"
<< "Options:\t\t(alias)\n"
<< "\t--help\t\t-h\tGets this help information\n"
<< "\t--verbose\t-v\tEnables verbose output\n"
<< "\t--list\t\t-l\tList available Test Suites\n"
<< "\t--cases\t\t-c\tList available Test Cases\n"
<< "Note: aliases may NOT be concatenated, such as \"-hv\"\n"
<< "\n"
<< "A <test> is one of:\n"
<< "\t<suite>\t\tname of Test Suite to run\n"
<< "\t<suite>:<case>\tname of Test Case in Test Suite to run\n"
;
if(!error.empty())
{
std::cout << "\n Error: " << error;
if(!errorInfo.empty())
{
std::cout << ": " << errorInfo;
}
std::cout << "\n";
}
}
template<typename S, typename T, long unsigned int length>
bool
contains(
std::array<S, length> matches,
const T& value
)
{
for(auto const match : matches)
{
if(value == match)
{
return true;
}
}
return false;
}
}
int
main(
int argc,
char**argv
)
{
bool verbose = false;
std::vector<std::string> tests;
{
const std::string program(argv[0]);
for(int i = 1; i < argc; ++i)
{
const std::string arg(argv[i]);
if(contains(std::array{"--verbose", "-v"}, arg)) { verbose = true; }
else if(contains(std::array{"--help", "-h"}, arg)) { doHelp(program); exit(0); }
else if(0 == arg.find("-")) { doHelp(program, "Unknown option", arg.c_str()); exit(1); }
else { doHelp(program, "Unknown test", arg.c_str()); exit(1); }
}
}
ExpectedFailureLogger logger(std::cout, verbose);
const int fails = counts.
fails - logger.getExpectedFailuresFailed();
return fails;
}
Count of tests run, and fails.
int fails
Number of tests that have failed.
Count run(Logger &logger) const
Run all tests that have been add()ed.
void listTestSuites(std::ostream &out) const
List available Test Suites to out.
static TestRunner & get()
Get the singleton.
void listTestCases(std::ostream &out) const
List available Test Cases to out.