C++ Utilities: tests/cppunit.h Source File

C++ Utilities 5.34.1 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
6#include "../application/commandlineutils.h"
7#include "../io/ansiescapecodes.h"
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 CppUtilities;
16
20 void printTestNames(CPPUNIT_NS::Test *test, Indentation indentation)
21{
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();
25 printTestNames(childTest, indentation + 4);
26 }
27}
28
32 int main(int argc, char **argv)
33{
34 TestApplication testApp(argc, argv);
35 if (!testApp) {
36 return -1;
37 }
38
39 // list tests
40 CPPUNIT_NS::TestFactoryRegistry &registry = CPPUNIT_NS::TestFactoryRegistry::getRegistry();
41 if (testApp.onlyListUnits()) {
42 std::cerr << "Available tests:";
43 printTestNames(registry.makeTest(), Indentation(0));
44 std::cerr << '\n';
45 return 0;
46 }
47
48 // run tests
49 CPPUNIT_NS::TextUi::TestRunner runner;
50 if (!testApp.unitsSpecified() || testApp.units().empty()) {
51 // no units specified -> test all
52 runner.addTest(registry.makeTest());
53 } else {
54 // pick specified units from overall test
55 CPPUNIT_NS::Test *overallTest = registry.makeTest();
56 std::vector<const char *> unavailableUnits;
57 for (const char *unit : testApp.units()) {
58 try {
59 runner.addTest(overallTest->findTest(unit));
60 } catch (const std::invalid_argument &) {
61 unavailableUnits.emplace_back(unit);
62 }
63 }
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;
68 }
69 std::cerr << "\nAvailable tests:";
70 printTestNames(overallTest, Indentation(0));
71 std::cerr << '\n';
72 return -1;
73 }
74 }
75 std::cerr << EscapeCodes::TextAttribute::Bold << "Executing test cases ..." << EscapeCodes::Phrases::EndFlush;
76 const auto ok = runner.run(std::string(), false);
77 std::cerr << (ok ? "Tests successful\n" : "Tests failed\n");
78 return !ok;
79}
80
81#endif // TESTUTILS_CPPUNIT_H
ansiescapecodes.h
CppUtilities::Indentation
The Indentation class allows printing indentation conveniently, eg.
Definition commandlineutils.h:71
CppUtilities::TestApplication
The TestApplication class simplifies writing test applications that require opening test files.
Definition testutils.h:34
CppUtilities::TestApplication::onlyListUnits
bool onlyListUnits() const
Returns whether the test application should only list available units and not actually run any tests.
Definition testutils.h:152
CppUtilities::TestApplication::units
const std::vector< const char * > & units() const
Returns the specified test units.
Definition testutils.h:144
CppUtilities::TestApplication::unitsSpecified
bool unitsSpecified() const
Returns whether particular units have been specified.
Definition testutils.h:135
commandlineutils.h
printTestNames
void printTestNames(CPPUNIT_NS::Test *test, Indentation indentation)
Prints the names of all child tests of the specified test.
Definition cppunit.h:20
main
int main(int argc, char **argv)
Performs unit tests using cppunit.
Definition cppunit.h:32
CppUtilities::EscapeCodes::TextAttribute::Bold
@ Bold
Definition ansiescapecodes.h:22
CppUtilities::EscapeCodes::Phrases::EndFlush
@ EndFlush
Definition ansiescapecodes.h:147
CppUtilities
Contains all utilities provided by the c++utilities library.
Definition argumentparser.h:18
testutils.h