1#include <cppunit/TestSuite.h> 2#include <cppunit/TextTestResult.h> 3#include <cppunit/TextOutputter.h> 4#include <cppunit/TextTestProgressListener.h> 5#include <cppunit/TestResult.h> 6#include <cppunit/ui/text/TestRunner.h> 7#include <iostream> 8 9using std::cin; 10using std::cout; 11using std::endl; 12 13namespace CppUnit { 14namespace TextUi { 15 16/*! Constructs a new text runner. 17 * \param outputter used to print text result. Owned by the runner. 18 */ 19TestRunner::TestRunner( Outputter *outputter ) 20 : m_suite( new TestSuite( "All Tests" ) ) 21 , m_result( new TestResultCollector() ) 22 , m_eventManager( new TestResult() ) 23 , m_outputter( outputter ) 24{ 25 if ( !m_outputter ) 26 m_outputter = new TextOutputter( m_result, cout ); 27 m_eventManager->addListener( m_result ); 28} 29 30 31TestRunner::~TestRunner() 32{ 33 delete m_eventManager; 34 delete m_outputter; 35 delete m_result; 36 delete m_suite; 37} 38 39 40/*! Adds the specified test. 41 * 42 * \param test Test to add. 43 */ 44void 45TestRunner::addTest( Test *test ) 46{ 47 if ( test != NULL ) 48 m_suite->addTest( test ); 49} 50 51 52/*! Runs the named test case. 53 * 54 * \param testName Name of the test case to run. If an empty is given, then 55 * all added test are run. The name must be the name of 56 * of an added test. 57 * \param doWait if \c true then the user must press the RETURN key 58 * before the run() method exit. 59 * \param doPrintResult if \c true (default) then the test result are printed 60 * on the standard output. 61 * \param doPrintProgress if \c true (default) then TextTestProgressListener is 62 * used to show the progress. 63 * \return \c true is the test was successful, \c false if the test 64 * failed or was not found. 65 */ 66bool 67TestRunner::run( string testName, 68 bool doWait, 69 bool doPrintResult, 70 bool doPrintProgress ) 71{ 72 runTestByName( testName, doPrintProgress ); 73 printResult( doPrintResult ); 74 wait( doWait ); 75 return m_result->wasSuccessful(); 76} 77 78 79bool 80TestRunner::runTestByName( string testName, 81 bool doPrintProgress ) 82{ 83 if ( testName.empty() ) 84 return runTest( m_suite, doPrintProgress ); 85 86 Test *test = findTestByName( testName ); 87 if ( test != NULL ) 88 return runTest( test, doPrintProgress ); 89 90 cout << "Test " << testName << " not found." << endl; 91 return false; 92} 93 94 95void 96TestRunner::wait( bool doWait ) 97{ 98 if ( doWait ) 99 { 100 cout << "<RETURN> to continue" << endl; 101 cin.get (); 102 } 103} 104 105 106void 107TestRunner::printResult( bool doPrintResult ) 108{ 109 cout << endl; 110 if ( doPrintResult ) 111 m_outputter->write(); 112} 113 114 115Test * 116TestRunner::findTestByName( string name ) const 117{ 118 for ( vector<Test *>::const_iterator it = m_suite->getTests().begin(); 119 it != m_suite->getTests().end(); 120 ++it ) 121 { 122 Test *test = *it; 123 if ( test->getName() == name ) 124 return test; 125 } 126 return NULL; 127} 128 129 130bool 131TestRunner::runTest( Test *test, 132 bool doPrintProgress ) 133{ 134 TextTestProgressListener progress; 135 if ( doPrintProgress ) 136 m_eventManager->addListener( &progress ); 137 138 test->run( m_eventManager ); 139 140 if ( doPrintProgress ) 141 m_eventManager->removeListener( &progress ); 142 return m_result->wasSuccessful(); 143} 144 145 146/*! Returns the result of the test run. 147 * Use this after calling run() to access the result of the test run. 148 */ 149TestResultCollector & 150TestRunner::result() const 151{ 152 return *m_result; 153} 154 155 156/*! Returns the event manager. 157 * The instance of TestResult results returned is the one that is used to run the 158 * test. Use this to register additional TestListener before running the tests. 159 */ 160TestResult & 161TestRunner::eventManager() const 162{ 163 return *m_eventManager; 164} 165 166 167/*! Specifies an alternate outputter. 168 * 169 * Notes that the outputter will be use after the test run only if \a printResult was 170 * \c true. 171 * \see CompilerOutputter, XmlOutputter, TextOutputter. 172 */ 173void 174TestRunner::setOutputter( Outputter *outputter ) 175{ 176 delete m_outputter; 177 m_outputter = outputter; 178} 179 180 181} // namespace TextUi 182} // namespace CppUnit 183