FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
run_tests.cc
Go to the documentation of this file.
1 /*********************************************************************
2 Copyright (C) 2019, Siemens AG
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 version 2 as published by the Free Software Foundation.
7 
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 *********************************************************************/
21 #include <cppunit/CompilerOutputter.h>
22 #include <cppunit/TestResult.h>
23 #include <cppunit/TestResultCollector.h>
24 #include <cppunit/TestRunner.h>
25 #ifdef WIN32
26 #include <cppunit/TextTestProgressListener.h>
27 #else
28 #include <cppunit/BriefTestProgressListener.h>
29 #endif
30 #include <cppunit/XmlOutputter.h>
31 #include <cppunit/extensions/TestFactoryRegistry.h>
32 #include <stdexcept>
33 #include <fstream>
34 
35 
36 int
37 main( int argc, char* argv[] )
38 {
39  // Retrieve test path from command line first argument. Default to "" which resolve
40  // to the top level suite.
41  std::string testPath = (argc > 1) ? std::string(argv[1]) : std::string("");
42 
43  // Create the event manager and test controller
44  CPPUNIT_NS::TestResult controller;
45 
46  // Add a listener that collects test result
47  CPPUNIT_NS::TestResultCollector result;
48  controller.addListener( &result );
49 
50  // Add a listener that print dots as test run.
51 #ifdef WIN32
52  CPPUNIT_NS::TextTestProgressListener progress;
53 #else
54  CPPUNIT_NS::BriefTestProgressListener progress;
55 #endif
56  controller.addListener( &progress );
57 
58  // Add the top suite to the test runner
59  CPPUNIT_NS::TestRunner runner;
60  runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest() );
61  try
62  {
63  CPPUNIT_NS::stdCOut() << "Running " << testPath;
64  runner.run( controller, testPath );
65 
66  CPPUNIT_NS::stdCOut() << "\n";
67 
68  // Print test in a compiler compatible format.
69  CPPUNIT_NS::CompilerOutputter outputter( &result, CPPUNIT_NS::stdCOut() );
70  outputter.write();
71 
72 // Uncomment this for XML output
73 // std::ofstream file( "tests.xml" );
74 // CPPUNIT_NS::XmlOutputter xml( &result, file );
75 // xml.setStyleSheet( "report.xsl" );
76 // xml.write();
77 // file.close();
78  }
79  catch ( std::invalid_argument &e ) // Test path not resolved
80  {
81  CPPUNIT_NS::stdCOut() << "\n"
82  << "ERROR: " << e.what()
83  << "\n";
84  return 1;
85  }
86 
87  return result.wasSuccessful() ? 0 : 1;
88 }
int main(int argc, char *argv[])
Definition: run_tests.cc:40