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