int CppUnitTestRunner::Run(int argc, char* argv[]) { std::vector<CPPUNIT_NS::Test *> tests; CPPUNIT_NS::Test * root = CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest(); for (int i = 1; i < argc; i++) { if (! strcmp(argv[i], "/?") || ! strcmp(argv[i], "-?") || ! strcmp(argv[i], "--help")) { std::cout << "usage: " << argv[0] << " <test-suite|test> [<test-suite1|test1> ... ]"; std::cout << std::endl << Help(); return 0; } else { try { CPPUNIT_NS::Test * test = NULL; test = root->findTest(argv[i]); tests.push_back(test); } catch(std::invalid_argument&) { std::cout << "invalid test: " << argv[i]; std::cout << std::endl << Help(); return -1; } } } return tests.size() ? Run(tests) : Run(); }
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; }