Пример #1
0
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;
}
Пример #2
0
/* Test program */
int main(int argc, char** argv)
{
  CppUnit::TestResult testresult;
  CppUnit::TestResultCollector collectedresults;
  CppUnit::TestRunner testrunner;
  std::ofstream fb;
  std::list< std::string > test_list;

#ifdef HAVE_GETOPT_LONG
  int c;
  struct option long_options[] = {
    {"help", 0, 0, 'h'},
    {"list", 0, 0, 'l'},
    {0, 0, 0, 0}
  };

  while ((c = getopt_long(argc, argv, "hl", long_options, NULL)) != -1) {
    switch (c) {
      case 'h':
        std::cout << "Usage: " << argv[0] << " [test name]\n";
        std::cout << "\nOptions:\n" <<
          "  -h, --help           Show this help message and exit\n" <<
          "  -l, --list           List available tests and exit" <<
          std::endl;
        exit(EXIT_SUCCESS);
      case 'l':
      {
        CppUnit::Test *t = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
        std::cout << "Available test suites:\n";
        printTest(std::cout, t);
        delete t;
        exit(EXIT_SUCCESS);
      }
    }
  }

  if (optind < argc) {
    while (optind < argc) {
      test_list.push_back(argv[optind++]);
    }
  }
#endif

  testresult.addListener(&collectedresults);
  testrunner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
  if (test_list.empty())
    testrunner.run(testresult);
  else {
    std::list<std::string>::const_iterator it;
    for (it = test_list.begin(); it != test_list.end(); ++it) {
      testrunner.run(testresult, *it);
    }
  }

  fb.open((std::string(argv[0]) + ".xml").c_str());
  CppUnit::XmlOutputter xml_outputter(&collectedresults, fb);
  xml_outputter.write();
  fb.close();

  fb.open((std::string(argv[0]) + ".cmp").c_str());
  CppUnit::CompilerOutputter comp_outputter(&collectedresults, fb);
  comp_outputter.write();
  fb.close();

  CppUnit::TextOutputter txt_outputter(&collectedresults, std::cout);
  txt_outputter.write();

  return collectedresults.wasSuccessful() ? 0 : 1;
}