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: © 2024 Siemens AG
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
10 #include <cppunit/CompilerOutputter.h>
11 #include <cppunit/TestResult.h>
12 #include <cppunit/TestResultCollector.h>
13 #include <cppunit/TestRunner.h>
14 #ifdef WIN32
15 #include <cppunit/TextTestProgressListener.h>
16 #else
17 #include <cppunit/BriefTestProgressListener.h>
18 #endif
19 #include <cppunit/XmlOutputter.h>
20 #include <cppunit/extensions/TestFactoryRegistry.h>
21 #include <stdexcept>
22 
23 
24 int
25 main( int argc, char* argv[] )
26 {
27  // Retreive test path from command line first argument. Default to "" which resolve
28  // to the top level suite.
29  std::string testPath = (argc > 1) ? std::string(argv[1]) : std::string("");
30 
31  // Create the event manager and test controller
32  CPPUNIT_NS::TestResult controller;
33 
34  // Add a listener that colllects test result
35  CPPUNIT_NS::TestResultCollector result;
36  controller.addListener( &result );
37 
38  // Add a listener that print dots as test run.
39 #ifdef WIN32
40  CPPUNIT_NS::TextTestProgressListener progress;
41 #else
42  CPPUNIT_NS::BriefTestProgressListener progress;
43 #endif
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  try
50  {
51  CPPUNIT_NS::stdCOut() << "Running " << testPath;
52  runner.run( controller, testPath );
53 
54  CPPUNIT_NS::stdCOut() << "\n";
55 
56  // Print test in a compiler compatible format.
57  CPPUNIT_NS::CompilerOutputter outputter( &result, CPPUNIT_NS::stdCOut() );
58  outputter.write();
59 
60 // Uncomment this for XML output
61 // std::ofstream file( "tests.xml" );
62 // CPPUNIT_NS::XmlOutputter xml( &result, file );
63 // xml.setStyleSheet( "report.xsl" );
64 // xml.write();
65 // file.close();
66  }
67  catch ( std::invalid_argument &e ) // Test path not resolved
68  {
69  CPPUNIT_NS::stdCOut() << "\n"
70  << "ERROR: " << e.what()
71  << "\n";
72  return 1;
73  }
74 
75  return result.wasSuccessful() ? 0 : 1;
76 }
int main(int argc, char *argv[])
Definition: run_tests.cc:29