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>
22 for (
int index = 0, count = test->getChildTestCount(); index != count; ++index) {
23 const auto childTest = test->getChildTestAt(index);
24 std::cerr <<
'\n' << indentation <<
" - " << childTest->getName();
32int main(
int argc,
char **argv)
40 CPPUNIT_NS::TestFactoryRegistry ®istry = CPPUNIT_NS::TestFactoryRegistry::getRegistry();
42 std::cerr <<
"Available tests:";
49 CPPUNIT_NS::TextUi::TestRunner runner;
52 runner.addTest(registry.makeTest());
55 CPPUNIT_NS::Test *overallTest = registry.makeTest();
56 std::vector<const char *> unavailableUnits;
57 for (
const char *unit : testApp.
units()) {
59 runner.addTest(overallTest->findTest(unit));
60 }
catch (
const std::invalid_argument &) {
61 unavailableUnits.emplace_back(unit);
64 if (!unavailableUnits.empty()) {
65 std::cerr <<
"The following tests specified via --unit are not available:";
66 for (
const char *unitName : unavailableUnits) {
67 std::cerr <<
"\n - " << unitName;
69 std::cerr <<
"\nAvailable tests:";
76 const auto ok = runner.run(std::string(),
false);
77 std::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.
void printTestNames(CPPUNIT_NS::Test *test, Indentation indentation)
Prints the names of all child tests of the specified test.
int main(int argc, char **argv)
Performs unit tests using cppunit.
Contains all utilities provided by the c++utilities library.