int main() { // Create the event manager and test controller CPPUNIT_NS::TestResult controller; // Add a listener that colllects test result CPPUNIT_NS::TestResultCollector result; controller.addListener(&result); // Add a listener that print dots as test run. CPPUNIT_NS::BriefTestProgressListener progress; controller.addListener(&progress); // Add the top suite to the test runner CPPUNIT_NS::TestRunner runner; runner.addTest(CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest()); runner.run(controller); // Print test in a compiler compatible format. CPPUNIT_NS::CompilerOutputter outputter(&result, CPPUNIT_NS::stdCOut()); outputter.write(); return result.wasSuccessful() ? 0 : 1; }
int CppUnitTestRunner::Run(std::vector<CPPUNIT_NS::Test *>& tests) { CPPUNIT_NS::TestRunner runner; CPPUNIT_NS::TestResult controller; CPPUNIT_NS::TestResultCollector result; CPPUNIT_NS::BriefTestProgressListener progress; CPPUNIT_NS::CompilerOutputter outputter( &result, std::cerr ); std::string xmlFileName = (s_XmlPath=="" ? "" : s_XmlPath + "\\") + m_TestSuiteName + ".xml"; std::ofstream output_file( xmlFileName.c_str() , std::ios::out ); CPPUNIT_NS::XmlOutputter xmloutputter( &result, output_file ); controller.addListener( &result ); controller.addListener( &progress ); for each (CPPUNIT_NS::Test * test in tests) runner.addTest(test); runner.run( controller ); outputter.write(); xmloutputter.write(); return result.wasSuccessful() ? EXIT_SUCCESS : EXIT_FAILURE; }
WARNING_PUSH DISABLE_EXTERNAL_HEADER_WARNINGS #include <string> #include <cstring> /* cpplint Supression * cpplint HATES streams for readability but we need a stream for cppunit to * output XML */ #include <fstream> // NOLINT(readability/streams) #include "cppunit/TestRunner.h" #include "cppunit/TestResult.h" #include "cppunit/TestResultCollector.h" #include "cppunit/XmlOutputter.h" #include "cppunit/BriefTestProgressListener.h" #include "cppunit/extensions/HelperMacros.h" #include "cppunit/extensions/TestFactoryRegistry.h" WARNING_POP #include "junit_outputter.hpp" /** * @~english * The binary entry point */ int main(int argc, char *argv[]) { // Create the event manager and test controller CPPUNIT_NS::TestResult controller; // Add a listener that collects test result CPPUNIT_NS::TestResultCollector result; controller.addListener(&result); // Outputs tests to command line so that waf can parse the output CPPUNIT_NS::BriefTestProgressListener progress; controller.addListener(&progress); // Add the top suite to the test runner CPPUNIT_NS::TestRunner runner; runner.addTest(CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest()); runner.run(controller); // Decide what the file name should be const char * name = argv[0]; // Prepare for the XML output std::ofstream xml_file; const char * const extension = "_cppunit.xml"; size_t size = std::strlen(name); size += std::strlen("_Zunit"); size += std::strlen(extension); size += 1; // For null character char * const filename = new char[size]; // Output xUnit XML file #ifdef _MSC_VER _snprintf_s(filename, size, size, "%s_xunit%s", argv[0], extension); #else std::snprintf(filename, size, "%s_xunit%s", argv[0], extension); #endif filename[size - 1] = '\0'; xml_file.open(filename); CPPUNIT_NS::XmlOutputter xunit_outputter(&result, xml_file); xunit_outputter.write(); xml_file.close(); // Output jUnit XML file #ifdef _MSC_VER _snprintf_s(filename, size, size, "%s_junit%s", argv[0], extension); #else std::snprintf(filename, size, "%s_junit%s", argv[0], extension); #endif filename[size - 1] = '\0'; xml_file.open(filename); JunitOutputter junit_outputter; junit_outputter.Write(result, &xml_file); xml_file.close(); // Clean up delete [] filename; // Do something with argc so there are no unused parameter warnings return (result.wasSuccessful() && argc) ? 0 : 1; }
int CppUnitTestHelper::runTest (int argc, char** argv, const string & reportName) { int out = 0; if (argc > 0 && argv != NULL) { CppUnitTestHelper::argc = argc; CppUnitTestHelper::argv = argv; } CPPUNIT_NS::Test * rootTest = CPPUNIT_NS::TestFactoryRegistry::getRegistry ().makeTest (); CPPUNIT_NS::Test * test = NULL; if (argc > 1) { if (strcmp (argv [1], "help") == 0 || strcmp (argv [1], "--help") == 0 || strcmp (argv [1], "-h") == 0) { CppUnitTestHelper::listTest (rootTest); } else { try { test = rootTest->findTest (argv [1]); } catch (std::exception &e) { CppUnitTestHelper::listTest (rootTest); throw; } } } else { test = rootTest; } if (test != NULL) { // Create the event manager and test controller CPPUNIT_NS::TestResult controller; // Add a listener that colllects test result CPPUNIT_NS::TestResultCollector result; controller.addListener (&result); // Add a listener that print dots as test run. CPPUNIT_NS::BriefTestProgressListener progress; controller.addListener (&progress); // Add the top suite to the test runner CPPUNIT_NS::TestRunner runner; runner.addTest (test); runner.run (controller); // Print test in a compiler compatible format. CPPUNIT_NS::CompilerOutputter outputter (&result, CPPUNIT_NS::stdCOut ()); outputter.write (); ofstream outFile (reportName.c_str ()); CPPUNIT_NS::XmlOutputter xml_outputter (&result, outFile); xml_outputter.write (); out = result.wasSuccessful () ? 0 : -1; // runner destructor should do the cleaning work } else { CPPUNIT_NS::TestRunner deleter; deleter.addTest (rootTest); } return out; }