2017-10-24 01:02:07 +02:00
|
|
|
#ifndef TESTUTILS_CPPUNIT_H
|
|
|
|
#define TESTUTILS_CPPUNIT_H
|
2016-02-06 02:52:06 +01:00
|
|
|
|
|
|
|
#include "./testutils.h"
|
|
|
|
|
2019-06-14 18:07:45 +02:00
|
|
|
#include "../application/commandlineutils.h"
|
2020-04-11 21:50:55 +02:00
|
|
|
#include "../io/ansiescapecodes.h"
|
2019-06-14 18:07:45 +02:00
|
|
|
|
2017-05-01 03:13:11 +02:00
|
|
|
#include <cppunit/TestPath.h>
|
2016-02-06 02:52:06 +01:00
|
|
|
#include <cppunit/extensions/TestFactoryRegistry.h>
|
|
|
|
#include <cppunit/ui/text/TestRunner.h>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace std;
|
2019-06-10 21:56:46 +02:00
|
|
|
using namespace CppUtilities;
|
2016-02-06 02:52:06 +01:00
|
|
|
using namespace CPPUNIT_NS;
|
|
|
|
|
2019-06-14 18:07:45 +02:00
|
|
|
/*!
|
|
|
|
* \brief Prints the names of all child tests of the specified \a test.
|
|
|
|
*/
|
|
|
|
void printTestNames(Test *test, Indentation indentation)
|
|
|
|
{
|
|
|
|
for (int index = 0, count = test->getChildTestCount(); index != count; ++index) {
|
|
|
|
const auto childTest = test->getChildTestAt(index);
|
|
|
|
cerr << '\n' << indentation << " - " << childTest->getName();
|
|
|
|
printTestNames(childTest, indentation + 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-06 02:52:06 +01:00
|
|
|
/*!
|
|
|
|
* \brief Performs unit tests using cppunit.
|
|
|
|
*/
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
TestApplication testApp(argc, argv);
|
2017-10-24 01:02:07 +02:00
|
|
|
if (!testApp) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-06-14 18:07:45 +02:00
|
|
|
// list tests
|
|
|
|
TestFactoryRegistry ®istry = TestFactoryRegistry::getRegistry();
|
|
|
|
if (testApp.onlyListUnits()) {
|
|
|
|
cerr << "Available tests:";
|
|
|
|
printTestNames(registry.makeTest(), Indentation(0));
|
|
|
|
cerr << '\n';
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-24 01:02:07 +02:00
|
|
|
// run tests
|
|
|
|
TextUi::TestRunner runner;
|
|
|
|
if (!testApp.unitsSpecified() || testApp.units().empty()) {
|
|
|
|
// no units specified -> test all
|
|
|
|
runner.addTest(registry.makeTest());
|
|
|
|
} else {
|
|
|
|
// pick specified units from overall test
|
|
|
|
Test *overallTest = registry.makeTest();
|
2019-06-14 18:07:45 +02:00
|
|
|
vector<const char *> unavailableUnits;
|
2017-10-24 01:02:07 +02:00
|
|
|
for (const char *unit : testApp.units()) {
|
|
|
|
try {
|
|
|
|
runner.addTest(overallTest->findTest(unit));
|
|
|
|
} catch (const invalid_argument &) {
|
2019-06-14 18:07:45 +02:00
|
|
|
unavailableUnits.emplace_back(unit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!unavailableUnits.empty()) {
|
|
|
|
cerr << "The following tests specified via --unit are not available:";
|
|
|
|
for (const char *unitName : unavailableUnits) {
|
|
|
|
cerr << "\n - " << unitName;
|
2016-05-01 20:09:20 +02:00
|
|
|
}
|
2019-06-14 18:07:45 +02:00
|
|
|
cerr << "\nAvailable tests:";
|
|
|
|
printTestNames(overallTest, Indentation(0));
|
|
|
|
cerr << '\n';
|
|
|
|
return -1;
|
2016-05-01 20:09:20 +02:00
|
|
|
}
|
2016-02-06 02:52:06 +01:00
|
|
|
}
|
2020-04-11 21:50:55 +02:00
|
|
|
cerr << EscapeCodes::TextAttribute::Bold << "Executing test cases ..." << EscapeCodes::Phrases::EndFlush;
|
2019-05-04 15:47:31 +02:00
|
|
|
const auto ok = runner.run(string(), false);
|
|
|
|
cerr << (ok ? "Tests successful\n" : "Tests failed\n");
|
|
|
|
return !ok;
|
2016-02-06 02:52:06 +01:00
|
|
|
}
|
|
|
|
|
2017-10-24 01:02:07 +02:00
|
|
|
#endif // TESTUTILS_CPPUNIT_H
|