C++ Utilities 5.27.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
Loading...
Searching...
No Matches
cppunit.h
Go to the documentation of this file.
1#ifndef TESTUTILS_CPPUNIT_H
2#define TESTUTILS_CPPUNIT_H
3
4#include "./testutils.h"
5
8
9#include <cppunit/TestPath.h>
10#include <cppunit/extensions/TestFactoryRegistry.h>
11#include <cppunit/ui/text/TestRunner.h>
12
13#include <iostream>
14
15using namespace std;
16using namespace CppUtilities;
17using namespace CPPUNIT_NS;
18
22void printTestNames(Test *test, Indentation indentation)
23{
24 for (int index = 0, count = test->getChildTestCount(); index != count; ++index) {
25 const auto childTest = test->getChildTestAt(index);
26 cerr << '\n' << indentation << " - " << childTest->getName();
27 printTestNames(childTest, indentation + 4);
28 }
29}
30
34int main(int argc, char **argv)
35{
36 TestApplication testApp(argc, argv);
37 if (!testApp) {
38 return -1;
39 }
40
41 // list tests
42 TestFactoryRegistry &registry = TestFactoryRegistry::getRegistry();
43 if (testApp.onlyListUnits()) {
44 cerr << "Available tests:";
45 printTestNames(registry.makeTest(), Indentation(0));
46 cerr << '\n';
47 return 0;
48 }
49
50 // run tests
51 TextUi::TestRunner runner;
52 if (!testApp.unitsSpecified() || testApp.units().empty()) {
53 // no units specified -> test all
54 runner.addTest(registry.makeTest());
55 } else {
56 // pick specified units from overall test
57 Test *overallTest = registry.makeTest();
58 vector<const char *> unavailableUnits;
59 for (const char *unit : testApp.units()) {
60 try {
61 runner.addTest(overallTest->findTest(unit));
62 } catch (const invalid_argument &) {
63 unavailableUnits.emplace_back(unit);
64 }
65 }
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;
70 }
71 cerr << "\nAvailable tests:";
72 printTestNames(overallTest, Indentation(0));
73 cerr << '\n';
74 return -1;
75 }
76 }
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");
80 return !ok;
81}
82
83#endif // TESTUTILS_CPPUNIT_H
The Indentation class allows printing indentation conveniently, eg.
The TestApplication class simplifies writing test applications that require opening test files.
Definition testutils.h:34
bool onlyListUnits() const
Returns whether the test application should only list available units and not actually run any tests.
Definition testutils.h:152
const std::vector< const char * > & units() const
Returns the specified test units.
Definition testutils.h:144
bool unitsSpecified() const
Returns whether particular units have been specified.
Definition testutils.h:135
int main(int argc, char **argv)
Performs unit tests using cppunit.
Definition cppunit.h:34
void printTestNames(Test *test, Indentation indentation)
Prints the names of all child tests of the specified test.
Definition cppunit.h:22
Contains all utilities provides by the c++utilities library.
STL namespace.