FOSSology  4.4.0
Open Source License Compliance by Open Source Software
run_tests.cc
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2014 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 
24 using namespace std;
25 
29 int main(int argc, char* argv[])
30 {
31  // Retrieve test path from command line first argument.
32  // Default to "" which resolves to the top level suite.
33  string testPath = (argc > 1) ? string(argv[1]) : string("");
34 
35  // Create the event manager and test controller.
36  CPPUNIT_NS::TestResult controller;
37 
38  // Add a listener that colllects test results.
39  CPPUNIT_NS::TestResultCollector result;
40  controller.addListener(&result);
41 
42  // Add a listener that prints dots as tests run.
43  CPPUNIT_NS::BriefTestProgressListener progress;
44  controller.addListener(&progress);
45 
46  // Add the top suite to the test runner.
47  CPPUNIT_NS::TestRunner runner;
48  runner.addTest(CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest());
49 
50  try
51  {
52  CPPUNIT_NS::stdCOut() << "Running " << (testPath.empty() ? "all tests" : testPath) << endl;
53  runner.run(controller, testPath);
54  CPPUNIT_NS::stdCOut() << endl;
55 
56  // Print test in a compiler compatible format.
57  CPPUNIT_NS::CompilerOutputter outputter(&result, CPPUNIT_NS::stdCOut());
58  outputter.write();
59 
60  // Generate XML output.
61  ofstream file("libcpp-Tests-Results.xml");
62  CPPUNIT_NS::XmlOutputter xml(&result, file);
63  xml.write();
64  file.close();
65  } catch (invalid_argument& e)
66  { // Test path not resolved.
67  CPPUNIT_NS::stdCOut() << endl << "ERROR: " << e.what() << endl;
68  return 1;
69  }
70 
71  return result.wasSuccessful() ? 0 : 1;
72 }
int main(int argc, char *argv[])
Definition: run_tests.cc:29