TestRunner.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #ifndef CPPUNIT_TESTRUNNER_H
  2. #define CPPUNIT_TESTRUNNER_H
  3. #include <cppunit/TestSuite.h>
  4. #include <string>
  5. CPPUNIT_NS_BEGIN
  6. class Test;
  7. class TestResult;
  8. /*! \brief Generic test runner.
  9. * \ingroup ExecutingTest
  10. *
  11. * The TestRunner assumes ownership of all added tests: you can not add test
  12. * or suite that are local variable since they can't be deleted.
  13. *
  14. * Example of usage:
  15. * \code
  16. * #include <cppunit/extensions/TestFactoryRegistry.h>
  17. * #include <cppunit/CompilerOutputter.h>
  18. * #include <cppunit/TestResult.h>
  19. * #include <cppunit/TestResultCollector.h>
  20. * #include <cppunit/TestRunner.h>
  21. * #include <cppunit/TextTestProgressListener.h>
  22. *
  23. *
  24. * int
  25. * main( int argc, char* argv[] )
  26. * {
  27. * std::string testPath = (argc > 1) ? std::string(argv[1]) : "";
  28. *
  29. * // Create the event manager and test controller
  30. * CppUnit::TestResult controller;
  31. *
  32. * // Add a listener that colllects test result
  33. * CppUnit::TestResultCollector result;
  34. * controller.addListener( &result );
  35. *
  36. * // Add a listener that print dots as test run.
  37. * CppUnit::TextTestProgressListener progress;
  38. * controller.addListener( &progress );
  39. *
  40. * // Add the top suite to the test runner
  41. * CppUnit::TestRunner runner;
  42. * runner.addTest( CppUnit::TestFactoryRegistry::getRegistry().makeTest() );
  43. * try
  44. * {
  45. * std::cout << "Running " << testPath;
  46. * runner.run( controller, testPath );
  47. *
  48. * std::cerr << std::endl;
  49. *
  50. * // Print test in a compiler compatible format.
  51. * CppUnit::CompilerOutputter outputter( &result, std::cerr );
  52. * outputter.write();
  53. * }
  54. * catch ( std::invalid_argument &e ) // Test path not resolved
  55. * {
  56. * std::cerr << std::endl
  57. * << "ERROR: " << e.what()
  58. * << std::endl;
  59. * return 0;
  60. * }
  61. *
  62. * return result.wasSuccessful() ? 0 : 1;
  63. * }
  64. * \endcode
  65. */
  66. class CPPUNIT_API TestRunner
  67. {
  68. public:
  69. /*! \brief Constructs a TestRunner object.
  70. */
  71. TestRunner( );
  72. /// Destructor.
  73. virtual ~TestRunner();
  74. /*! \brief Adds the specified test.
  75. * \param test Test to add. The TestRunner takes ownership of the test.
  76. */
  77. virtual void addTest( Test *test );
  78. /*! \brief Runs a test using the specified controller.
  79. * \param controller Event manager and controller used for testing
  80. * \param testPath Test path string. See Test::resolveTestPath() for detail.
  81. * \exception std::invalid_argument if no test matching \a testPath is found.
  82. * see TestPath::TestPath( Test*, const std::string &)
  83. * for detail.
  84. */
  85. virtual void run( TestResult &controller,
  86. const std::string &testPath = "" );
  87. protected:
  88. /*! \brief (INTERNAL) Mutating test suite.
  89. */
  90. class CPPUNIT_API WrappingSuite : public TestSuite
  91. {
  92. public:
  93. WrappingSuite( const std::string &name = "All Tests" );
  94. int getChildTestCount() const;
  95. std::string getName() const;
  96. void run( TestResult *result );
  97. protected:
  98. Test *doGetChildTestAt( int index ) const;
  99. bool hasOnlyOneTest() const;
  100. Test *getUniqueChildTest() const;
  101. };
  102. protected:
  103. WrappingSuite *m_suite;
  104. private:
  105. /// Prevents the use of the copy constructor.
  106. TestRunner( const TestRunner &copy );
  107. /// Prevents the use of the copy operator.
  108. void operator =( const TestRunner &copy );
  109. private:
  110. };
  111. CPPUNIT_NS_END
  112. #endif // CPPUNIT_TESTRUNNER_H