FOSSology  4.4.0
Open Source License Compliance by Open Source Software
run_tests.cc
1 /*
2  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
7 #include <cppunit/BriefTestProgressListener.h>
8 #include <cppunit/CompilerOutputter.h>
9 #include <cppunit/TestResult.h>
10 #include <cppunit/TestResultCollector.h>
11 #include <cppunit/TestRunner.h>
12 #include <cppunit/XmlOutputter.h>
13 #include <cppunit/extensions/TestFactoryRegistry.h>
14 #include <fstream>
15 #include <stdexcept>
16 
17 using namespace std;
18 
19 int main(int argc, char* argv[])
20 {
21  // Retrieve test path from command line first argument.
22  // Default to "" which resolves to the top level suite.
23  string testPath = (argc > 1) ? string(argv[1]) : string("");
24 
25  // Create the event manager and test controller.
26  CPPUNIT_NS::TestResult controller;
27 
28  // Add a listener that colllects test results.
29  CPPUNIT_NS::TestResultCollector result;
30  controller.addListener(&result);
31 
32  // Add a listener that prints dots as tests run.
33  CPPUNIT_NS::BriefTestProgressListener progress;
34  controller.addListener(&progress);
35 
36  // Add the top suite to the test runner.
37  CPPUNIT_NS::TestRunner runner;
38  runner.addTest(CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest());
39 
40  try
41  {
42  CPPUNIT_NS::stdCOut() << "Running " << (testPath.empty() ? "all tests" : testPath) << endl;
43  runner.run(controller, testPath);
44  CPPUNIT_NS::stdCOut() << endl;
45 
46  // Print test in a compiler compatible format.
47  CPPUNIT_NS::CompilerOutputter outputter(&result, CPPUNIT_NS::stdCOut());
48  outputter.write();
49 
50  // Generate XML output.
51  ofstream file("ninka-Tests-Results.xml");
52  CPPUNIT_NS::XmlOutputter xml(&result, file);
53  xml.write();
54  file.close();
55  } catch (invalid_argument& e)
56  { // Test path not resolved.
57  CPPUNIT_NS::stdCOut() << endl << "ERROR: " << e.what() << endl;
58  return 1;
59  }
60 
61  return result.wasSuccessful() ? 0 : 1;
62 }
int main(int argc, char *argv[])
Definition: run_tests.cc:29