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) 2014, Siemens AG
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2
6  * 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.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software Foundation,
15  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16  */
17 
18 #include <cppunit/BriefTestProgressListener.h>
19 #include <cppunit/CompilerOutputter.h>
20 #include <cppunit/TestResult.h>
21 #include <cppunit/TestResultCollector.h>
22 #include <cppunit/TestRunner.h>
23 #include <cppunit/XmlOutputter.h>
24 #include <cppunit/extensions/TestFactoryRegistry.h>
25 #include <fstream>
26 #include <stdexcept>
27 
35 using namespace std;
36 
40 int main(int argc, char* argv[])
41 {
42  // Retrieve test path from command line first argument.
43  // Default to "" which resolves to the top level suite.
44  string testPath = (argc > 1) ? string(argv[1]) : string("");
45 
46  // Create the event manager and test controller.
47  CPPUNIT_NS::TestResult controller;
48 
49  // Add a listener that colllects test results.
50  CPPUNIT_NS::TestResultCollector result;
51  controller.addListener(&result);
52 
53  // Add a listener that prints dots as tests run.
54  CPPUNIT_NS::BriefTestProgressListener progress;
55  controller.addListener(&progress);
56 
57  // Add the top suite to the test runner.
58  CPPUNIT_NS::TestRunner runner;
59  runner.addTest(CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest());
60 
61  try
62  {
63  CPPUNIT_NS::stdCOut() << "Running " << (testPath.empty() ? "all tests" : testPath) << endl;
64  runner.run(controller, testPath);
65  CPPUNIT_NS::stdCOut() << endl;
66 
67  // Print test in a compiler compatible format.
68  CPPUNIT_NS::CompilerOutputter outputter(&result, CPPUNIT_NS::stdCOut());
69  outputter.write();
70 
71  // Generate XML output.
72  ofstream file("libcpp-Tests-Results.xml");
73  CPPUNIT_NS::XmlOutputter xml(&result, file);
74  xml.write();
75  file.close();
76  } catch (invalid_argument& e)
77  { // Test path not resolved.
78  CPPUNIT_NS::stdCOut() << endl << "ERROR: " << e.what() << endl;
79  return 1;
80  }
81 
82  return result.wasSuccessful() ? 0 : 1;
83 }
int main(int argc, char *argv[])
Definition: run_tests.cc:40