summaryrefslogtreecommitdiff
path: root/src/cppunit/TextTestRunner.cpp
diff options
context:
space:
mode:
authorBaptiste Lepilleur <gaiacrtn@free.fr>2002-04-12 18:28:48 +0000
committerBaptiste Lepilleur <gaiacrtn@free.fr>2002-04-12 18:28:48 +0000
commited406a2966e62072fa6afaca8abc578db7c0c9fb (patch)
treeab8d2ffb462c3c955b2e339e8cc19f1a6be8bd0f /src/cppunit/TextTestRunner.cpp
parentfc9c76622b19adfcdebce682d9d49db8fb9336ef (diff)
downloadcppunit-ed406a2966e62072fa6afaca8abc578db7c0c9fb.tar.gz
Makefile.
Makefile.am: added examples/qt to tar ball release. * TODO: heavily updated. * contrib/msvc/CppUnit*.wwtpl: changed base class for unit test to TestFixture. * include/cppunit/Test.h: removed toString() method. Not used by the framework and source of confusions with getName(). Added getChildTestCount() and getChildTestAt(), introducing the composite pattern at top level. Added utility methods findTest() and findTestPath(). * src/cppunit/Test.cpp: added. Implementation of new utility methods. * include/cppunit/TestCase.h: * src/cppunit/TestCase.cpp: inherits TestLeaf. Removed toString(), run(void) and defaultResult(). Removed default constructor. * src/cppunit/TestCase.cpp: * src/cppunit/TestSuite.cpp: fixed some includes that used "" instead of <>. * include/cppunit/TestComposite.h: * src/cppunit/TestComposite.cpp: added. Common implementation of Test for composite tests (TestSuite). * include/cppunit/TestFailure.h: * src/cppunit/TestFailure.cpp: removed toString(). * include/cppunit/TestLeaf.h: * src/cppunit/TestLeaf.cpp: added. Common implementation of Test for single test (TestCase). * include/cppunit/TestListener.h: added TimingListener example to documentation. * include/cppunit/TestPath.h: * src/cppunit/TestPath.cpp: added. List of test traversed to access a test in the test hierarchy. * include/cppunit/TestRunner.h: added. Generic TestRunner. * src/cppunit/TestRunner.cpp: moved to TextTestRunner.cpp. Added new implementation of includecppunit/TestRunner.h. * include/cppunit/TestSuite.h: * src/cppunit/TestSuite.cpp: inherits TestComposite and implements new Test interface. Removed toString(). * src/cppunit/TextTestRunner.cpp: moved from TestRunner.cpp. Implementation of include/cppunit/ui/text/TestRunner.h. * include/cppunit/extensions/RepeatedTest.h: * src/cppunit/RepeatedTest.cpp: removed toString(). * include/cppunit/extensions/TestDecorator.h: inherits TestLeaf. Removed toString() * include/cppunit/XmlOutputter.h: * src/cppunit/XmlOutputter.cpp: * examples/cppunittest/XmlOutputterTest.cpp: * examples/cppunittest/XmlOutputterTest.h: XML outputter now escape node content. Add unit test for that bug (#540944). Added style sheet support. Modified XML structure: failure message as its own element. * src/msvc/testrunner/TestRunnerModel.h: * src/msvc/testrunner/TestRunnerModel.cpp: used Test::findTest() to find a test by name instead of using RTTI. Added toAnsiString() for convertion when compiling as UNICODE. * src/msvc/testrunner/TreeHierarchyDlg.h: * src/msvc/testrunner/TreeHierarchyDlg.cpp: used new composite interface of Test to explorer the test hierarchy instead of RTTI. * examples/cppunittest/TestPathTest.h: * examples/cppunittest/TestPathTest.cpp: added, unit tests for TestPath. * examples/cppunittest/TestCaseTest.h: * examples/cppunittest/TestCaseTest.cpp: added test for TestLeaf. * examples/cppunittest/TestSuiteTest.h: * examples/cppunittest/TestSuiteTest.cpp: added test for TestComposite and new Test interface.
Diffstat (limited to 'src/cppunit/TextTestRunner.cpp')
-rw-r--r--src/cppunit/TextTestRunner.cpp181
1 files changed, 181 insertions, 0 deletions
diff --git a/src/cppunit/TextTestRunner.cpp b/src/cppunit/TextTestRunner.cpp
new file mode 100644
index 0000000..d190ebe
--- /dev/null
+++ b/src/cppunit/TextTestRunner.cpp
@@ -0,0 +1,181 @@
+// ==> Implementation of cppunit/ui/text/TestRunner.h
+
+#include <cppunit/TestSuite.h>
+#include <cppunit/TextTestResult.h>
+#include <cppunit/TextOutputter.h>
+#include <cppunit/TextTestProgressListener.h>
+#include <cppunit/TestResult.h>
+#include <cppunit/ui/text/TestRunner.h>
+#include <iostream>
+
+
+namespace CppUnit {
+namespace TextUi {
+
+/*! Constructs a new text runner.
+ * \param outputter used to print text result. Owned by the runner.
+ */
+TestRunner::TestRunner( Outputter *outputter )
+ : m_outputter( outputter )
+ , m_suite( new TestSuite( "All Tests" ) )
+ , m_result( new TestResultCollector() )
+ , m_eventManager( new TestResult() )
+{
+ if ( !m_outputter )
+ m_outputter = new TextOutputter( m_result, std::cout );
+ m_eventManager->addListener( m_result );
+}
+
+
+TestRunner::~TestRunner()
+{
+ delete m_eventManager;
+ delete m_outputter;
+ delete m_result;
+ delete m_suite;
+}
+
+
+/*! Adds the specified test.
+ *
+ * \param test Test to add.
+ */
+void
+TestRunner::addTest( Test *test )
+{
+ if ( test != NULL )
+ m_suite->addTest( test );
+}
+
+
+/*! Runs the named test case.
+ *
+ * \param testName Name of the test case to run. If an empty is given, then
+ * all added test are run. The name must be the name of
+ * of an added test.
+ * \param doWait if \c true then the user must press the RETURN key
+ * before the run() method exit.
+ * \param doPrintResult if \c true (default) then the test result are printed
+ * on the standard output.
+ * \param doPrintProgress if \c true (default) then TextTestProgressListener is
+ * used to show the progress.
+ * \return \c true is the test was successful, \c false if the test
+ * failed or was not found.
+ */
+bool
+TestRunner::run( std::string testName,
+ bool doWait,
+ bool doPrintResult,
+ bool doPrintProgress )
+{
+ runTestByName( testName, doPrintProgress );
+ printResult( doPrintResult );
+ wait( doWait );
+ return m_result->wasSuccessful();
+}
+
+
+bool
+TestRunner::runTestByName( std::string testName,
+ bool doPrintProgress )
+{
+ if ( testName.empty() )
+ return runTest( m_suite, doPrintProgress );
+
+ Test *test = findTestByName( testName );
+ if ( test != NULL )
+ return runTest( test, doPrintProgress );
+
+ std::cout << "Test " << testName << " not found." << std::endl;
+ return false;
+}
+
+
+void
+TestRunner::wait( bool doWait )
+{
+ if ( doWait )
+ {
+ std::cout << "<RETURN> to continue" << std::endl;
+ std::cin.get ();
+ }
+}
+
+
+void
+TestRunner::printResult( bool doPrintResult )
+{
+ std::cout << std::endl;
+ if ( doPrintResult )
+ m_outputter->write();
+}
+
+
+Test *
+TestRunner::findTestByName( std::string name ) const
+{
+ for ( std::vector<Test *>::const_iterator it = m_suite->getTests().begin();
+ it != m_suite->getTests().end();
+ ++it )
+ {
+ Test *test = *it;
+ if ( test->getName() == name )
+ return test;
+ }
+ return NULL;
+}
+
+
+bool
+TestRunner::runTest( Test *test,
+ bool doPrintProgress )
+{
+ TextTestProgressListener progress;
+ if ( doPrintProgress )
+ m_eventManager->addListener( &progress );
+
+ test->run( m_eventManager );
+
+ if ( doPrintProgress )
+ m_eventManager->removeListener( &progress );
+ return m_result->wasSuccessful();
+}
+
+
+/*! Returns the result of the test run.
+ * Use this after calling run() to access the result of the test run.
+ */
+TestResultCollector &
+TestRunner::result() const
+{
+ return *m_result;
+}
+
+
+/*! Returns the event manager.
+ * The instance of TestResult results returned is the one that is used to run the
+ * test. Use this to register additional TestListener before running the tests.
+ */
+TestResult &
+TestRunner::eventManager() const
+{
+ return *m_eventManager;
+}
+
+
+/*! Specifies an alternate outputter.
+ *
+ * Notes that the outputter will be use after the test run only if \a printResult was
+ * \c true.
+ * \see CompilerOutputter, XmlOutputter, TextOutputter.
+ */
+void
+TestRunner::setOutputter( Outputter *outputter )
+{
+ delete m_outputter;
+ m_outputter = outputter;
+}
+
+
+} // namespace TextUi
+} // namespace CppUnit