1#ifndef TESTUTILS_CPPUNIT_H
2#define TESTUTILS_CPPUNIT_H
9#include <cppunit/TestPath.h>
10#include <cppunit/extensions/TestFactoryRegistry.h>
11#include <cppunit/ui/text/TestRunner.h>
17using namespace CPPUNIT_NS;
24 for (
int index = 0, count = test->getChildTestCount(); index != count; ++index) {
25 const auto childTest = test->getChildTestAt(index);
26 cerr <<
'\n' << indentation <<
" - " << childTest->getName();
34int main(
int argc,
char **argv)
42 TestFactoryRegistry ®istry = TestFactoryRegistry::getRegistry();
44 cerr <<
"Available tests:";
51 TextUi::TestRunner runner;
54 runner.addTest(registry.makeTest());
57 Test *overallTest = registry.makeTest();
58 vector<const char *> unavailableUnits;
59 for (
const char *unit : testApp.
units()) {
61 runner.addTest(overallTest->findTest(unit));
62 }
catch (
const invalid_argument &) {
63 unavailableUnits.emplace_back(unit);
66 if (!unavailableUnits.empty()) {
67 cerr <<
"The following tests specified via --unit are not available:";
68 for (
const char *unitName : unavailableUnits) {
69 cerr <<
"\n - " << unitName;
71 cerr <<
"\nAvailable tests:";
77 cerr << EscapeCodes::TextAttribute::Bold <<
"Executing test cases ..." << EscapeCodes::Phrases::EndFlush;
78 const auto ok = runner.run(
string(),
false);
79 cerr << (ok ?
"Tests successful\n" :
"Tests failed\n");
The Indentation class allows printing indentation conveniently, eg.
The TestApplication class simplifies writing test applications that require opening test files.
bool onlyListUnits() const
Returns whether the test application should only list available units and not actually run any tests.
const std::vector< const char * > & units() const
Returns the specified test units.
bool unitsSpecified() const
Returns whether particular units have been specified.
int main(int argc, char **argv)
Performs unit tests using cppunit.
void printTestNames(Test *test, Indentation indentation)
Prints the names of all child tests of the specified test.
Contains all utilities provides by the c++utilities library.