CppUnitTestDriver.h
Go to the documentation of this file.
1 #ifndef __CPPUNITTESTMAIN_H_
2 #define __CPPUNITTESTMAIN_H_
3 
4 #include <iostream>
5 #include <fstream>
6 #include <stdexcept>
7 #include <cppunit/TestResultCollector.h>
8 #include <cppunit/TestRunner.h>
9 #include <cppunit/TestResult.h>
10 #include <cppunit/TextTestProgressListener.h>
11 #include <cppunit/TextOutputter.h>
12 #include <cppunit/extensions/TestFactoryRegistry.h>
13 #include <cppunit/TestSuite.h>
14 #include <cppunit/XmlOutputter.h>
15 
16 int main ( int argc, char* argv[] ) {
17 
18  // Command line argument is test path. Defaults to "", which resolves to the top level suite.
19  std::string testPath = (argc > 1) ? std::string(argv[1]) : std::string("");
20 
21  // Create the test controller
22  CPPUNIT_NS::TestResult controller;
23  // Add a listener that collects test results
24  CPPUNIT_NS::TestResultCollector result;
25  controller.addListener(&result);
26  // Add a listener that prints a dot for each test as they run
27  CPPUNIT_NS::TextTestProgressListener progress;
28  controller.addListener(&progress);
29 
30  // Add the top suite to the test runner
31  CPPUNIT_NS::TestRunner runner;
32  runner.addTest(CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest());
33 
34  try {
35  std::cout << "Running "
36  << (testPath.size() > 0 ? testPath.c_str() : "all tests")
37  << std::endl;
38  runner.run (controller, testPath);
39 
40  // Output the results as text to std::cout
41  CPPUNIT_NS::TextOutputter textOutputter(&result,std::cout);
42  textOutputter.write();
43 
44  // Output the results in xml format to a file
45  // This is temporarily commented out until we can add user control
46  // of whether this file gets generated.
47  //std::ofstream xmlOutputFile("cppUnitTestResult.xml");
48  //CPPUNIT_NS::XmlOutputter xmlOutputter(&result,xmlOutputFile);
49  //xmlOutputter.write();
50  //xmlOutputFile.close();
51  }
52  catch (std::invalid_argument &e) { // Test path not resolved
53  std::cerr << "\n" << "ERROR: " << e.what() << std::endl;
54  return 1;
55  }
56 
57  return result.wasSuccessful() ? 0 : 1;
58 }
59 
60 #endif
OStream cerr
Definition: OStream.cxx:7
bool progress
Insert implicit nodes #####.
int main(int argc, char *argv[])
OStream cout
Definition: OStream.cxx:6
Float_t e
Definition: plot.C:35
enum BeamMode string