diff options
| author | Robert Collins <robertc@robertcollins.net> | 2009-10-03 23:33:53 +1000 |
|---|---|---|
| committer | Robert Collins <robertc@robertcollins.net> | 2009-10-03 23:33:53 +1000 |
| commit | 493a10dc9146e4ea9dd98ca69fe658a8139f35fc (patch) | |
| tree | 6b8b21aa693bd783626ca3cec772fd1f987b678c /c++ | |
| parent | a0fece2a82603e4cc7037313795b08e937e8a758 (diff) | |
| download | subunit-git-493a10dc9146e4ea9dd98ca69fe658a8139f35fc.tar.gz | |
Move the C++ Listener from a patch against cppunit to a usable external module for cppunit.
Diffstat (limited to 'c++')
| -rw-r--r-- | c++/README | 59 | ||||
| -rw-r--r-- | c++/SubunitTestProgressListener.cpp | 63 | ||||
| -rw-r--r-- | c++/SubunitTestProgressListener.h | 56 | ||||
| -rw-r--r-- | c++/cppunit-subunit-1.10.2.patch | 134 |
4 files changed, 149 insertions, 163 deletions
@@ -18,32 +18,33 @@ # Currently there are no native C++ bindings for subunit. However the C library -can be used from C++ safely. There is also a patch for cppunit -(http://cppunit.sourceforge.net/) to enable reporting via subunit -(cppunit-subunit-1.10.2.patch). - -To use the patch, apply it and rebuild your cppunit. Then in your main do -{ - // Create the event manager and test controller - CPPUNIT_NS::TestResult controller; - - // Add a listener that collects test result - // so we can get the overall status. - // note this isn't needed for subunit... - CPPUNIT_NS::TestResultCollector result; - controller.addListener( &result ); - - // Add a listener that print test activity in subunit format. - CPPUNIT_NS::SubunitTestProgressListener progress; - controller.addListener( &progress ); - - // Add the top suite to the test runner - CPPUNIT_NS::TestRunner runner; - runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest() ); - runner.run( controller ); - - return result.wasSuccessful() ? 0 : 1; -} - - -See the c/README for documentation on the C bindings for subunit. +can be used from C++ safely. A CPPUnit listener is built as part of Subunit to +allow CPPUnit users to simply get Subunit output. + +To use the listener, use pkg-config (or your preferred replacement) to get the +cflags and link settings from libcppunit_subunit.pc. + +In your test driver main, use SubunitTestProgressListener, as shown in this +example main:: + + { + // Create the event manager and test controller + CPPUNIT_NS::TestResult controller; + + // Add a listener that collects test result + // so we can get the overall status. + // note this isn't needed for subunit... + CPPUNIT_NS::TestResultCollector result; + controller.addListener( &result ); + + // Add a listener that print test activity in subunit format. + CPPUNIT_NS::SubunitTestProgressListener progress; + controller.addListener( &progress ); + + // Add the top suite to the test runner + CPPUNIT_NS::TestRunner runner; + runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest() ); + runner.run( controller ); + + return result.wasSuccessful() ? 0 : 1; + } diff --git a/c++/SubunitTestProgressListener.cpp b/c++/SubunitTestProgressListener.cpp new file mode 100644 index 0000000..76cd9e1 --- /dev/null +++ b/c++/SubunitTestProgressListener.cpp @@ -0,0 +1,63 @@ +/* Subunit test listener for cppunit (http://cppunit.sourceforge.net). + * Copyright (C) 2006 Robert Collins <robertc@robertcollins.net> + * + * Licensed under either the Apache License, Version 2.0 or the BSD 3-clause + * license at the users choice. A copy of both licenses are available in the + * project source as Apache-2.0 and BSD. You may not use this file except in + * compliance with one of these two licences. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under these licenses is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license you chose for the specific language governing permissions + * and limitations under that license. + */ + +#include <cppunit/Exception.h> +#include <cppunit/Test.h> +#include <cppunit/TestFailure.h> +#include <cppunit/TextOutputter.h> +#include <iostream> + +// Have to be able to import the public interface without config.h. +#include "SubunitTestProgressListener.h" +#include "config.h" +#include "subunit/child.h" + + +CPPUNIT_NS_BEGIN + + +void +SubunitTestProgressListener::startTest( Test *test ) +{ + subunit_test_start(test->getName().c_str()); + last_test_failed = false; +} + +void +SubunitTestProgressListener::addFailure( const TestFailure &failure ) +{ + std::ostringstream capture_stream; + TextOutputter outputter(NULL, capture_stream); + outputter.printFailureLocation(failure.sourceLine()); + outputter.printFailureDetail(failure.thrownException()); + + if (failure.isError()) + subunit_test_error(failure.failedTestName().c_str(), + capture_stream.str().c_str()); + else + subunit_test_fail(failure.failedTestName().c_str(), + capture_stream.str().c_str()); + last_test_failed = true; +} + +void +SubunitTestProgressListener::endTest( Test *test) +{ + if (!last_test_failed) + subunit_test_pass(test->getName().c_str()); +} + + +CPPUNIT_NS_END diff --git a/c++/SubunitTestProgressListener.h b/c++/SubunitTestProgressListener.h new file mode 100644 index 0000000..5206d83 --- /dev/null +++ b/c++/SubunitTestProgressListener.h @@ -0,0 +1,56 @@ +/* Subunit test listener for cppunit (http://cppunit.sourceforge.net). + * Copyright (C) 2006 Robert Collins <robertc@robertcollins.net> + * + * Licensed under either the Apache License, Version 2.0 or the BSD 3-clause + * license at the users choice. A copy of both licenses are available in the + * project source as Apache-2.0 and BSD. You may not use this file except in + * compliance with one of these two licences. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under these licenses is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license you chose for the specific language governing permissions + * and limitations under that license. + */ +#ifndef CPPUNIT_SUBUNITTESTPROGRESSLISTENER_H +#define CPPUNIT_SUBUNITTESTPROGRESSLISTENER_H + +#include <cppunit/TestListener.h> + + +CPPUNIT_NS_BEGIN + + +/*! + * \brief TestListener that outputs subunit + * (http://www.robertcollins.net/unittest/subunit) compatible output. + * \ingroup TrackingTestExecution + */ +class CPPUNIT_API SubunitTestProgressListener : public TestListener +{ +public: + + SubunitTestProgressListener() {} + + void startTest( Test *test ); + + void addFailure( const TestFailure &failure ); + + void endTest( Test *test ); + +private: + /// Prevents the use of the copy constructor. + SubunitTestProgressListener( const SubunitTestProgressListener © ); + + /// Prevents the use of the copy operator. + void operator =( const SubunitTestProgressListener © ); + +private: + int last_test_failed; +}; + + +CPPUNIT_NS_END + +#endif // CPPUNIT_SUBUNITTESTPROGRESSLISTENER_H + diff --git a/c++/cppunit-subunit-1.10.2.patch b/c++/cppunit-subunit-1.10.2.patch deleted file mode 100644 index b3c353b..0000000 --- a/c++/cppunit-subunit-1.10.2.patch +++ /dev/null @@ -1,134 +0,0 @@ -diff -u cppunit-1.10.2/src/cppunit/Makefile.am cppunit-1.10.2/src/cppunit/Makefile.am ---- cppunit-1.10.2/src/cppunit/Makefile.am -+++ cppunit-1.10.2/src/cppunit/Makefile.am -@@ -28,6 +28,7 @@ - ProtectorChain.cpp \ - SourceLine.cpp \ - StringTools.cpp \ -+ SubunitTestProgressListener.cpp \ - SynchronizedObject.cpp \ - Test.cpp \ - TestAssert.cpp \ -@@ -74,0 +76,2 @@ -+ -+LIBRARY_LIBADD = $(LIBS) ---- cppunit-1.10.2.orig/src/cppunit/SubunitTestProgressListener.cpp -+++ cppunit-1.10.2/src/cppunit/SubunitTestProgressListener.cpp -@@ -0,0 +1,50 @@ -+#include <cppunit/Exception.h> -+#include <cppunit/Test.h> -+#include <cppunit/TestFailure.h> -+#include <cppunit/SubunitTestProgressListener.h> -+#include <cppunit/TextOutputter.h> -+#include <iostream> -+ -+#include "config.h" -+#ifdef HAVE_LIBSUBUNIT -+#include <subunit/child.h> -+ -+ -+CPPUNIT_NS_BEGIN -+ -+ -+void -+SubunitTestProgressListener::startTest( Test *test ) -+{ -+ subunit_test_start(test->getName().c_str()); -+ last_test_failed = false; -+} -+ -+void -+SubunitTestProgressListener::addFailure( const TestFailure &failure ) -+{ -+ std::ostringstream capture_stream; -+ TextOutputter outputter(NULL, capture_stream); -+ outputter.printFailureLocation(failure.sourceLine()); -+ outputter.printFailureDetail(failure.thrownException()); -+ -+ if (failure.isError()) -+ subunit_test_error(failure.failedTestName().c_str(), -+ capture_stream.str().c_str()); -+ else -+ subunit_test_fail(failure.failedTestName().c_str(), -+ capture_stream.str().c_str()); -+ last_test_failed = true; -+} -+ -+void -+SubunitTestProgressListener::endTest( Test *test) -+{ -+ if (!last_test_failed) -+ subunit_test_pass(test->getName().c_str()); -+} -+ -+ -+CPPUNIT_NS_END -+ -+#endif ---- cppunit-1.10.2.orig/configure.in -+++ cppunit-1.10.2/configure.in -@@ -65,6 +65,8 @@ - # check for doxygen - BB_ENABLE_DOXYGEN - -+# check for subunit -+AC_CHECK_LIB(subunit, subunit_test_start) - - # Check for headers - # Note that the fourth argument to AC_CHECK_HEADERS is non-empty to force ---- cppunit-1.10.2.orig/include/cppunit/Makefile.am -+++ cppunit-1.10.2/include/cppunit/Makefile.am -@@ -15,6 +15,7 @@ - Portability.h \ - Protector.h \ - SourceLine.h \ -+ SubunitTestProgressListener.h \ - SynchronizedObject.h \ - Test.h \ - TestAssert.h \ -only in patch2: -unchanged: ---- cppunit-1.10.2.orig/include/cppunit/SubunitTestProgressListener.h -+++ cppunit-1.10.2/include/cppunit/SubunitTestProgressListener.h -@@ -0,0 +1,41 @@ -+#ifndef CPPUNIT_SUBUNITTESTPROGRESSLISTENER_H -+#define CPPUNIT_SUBUNITTESTPROGRESSLISTENER_H -+ -+#include <cppunit/TestListener.h> -+ -+ -+CPPUNIT_NS_BEGIN -+ -+ -+/*! -+ * \brief TestListener that outputs subunit -+ * (http://www.robertcollins.net/unittest/subunit) compatible output. -+ * \ingroup TrackingTestExecution -+ */ -+class CPPUNIT_API SubunitTestProgressListener : public TestListener -+{ -+public: -+ -+ SubunitTestProgressListener() {} -+ -+ void startTest( Test *test ); -+ -+ void addFailure( const TestFailure &failure ); -+ -+ void endTest( Test *test ); -+ -+private: -+ /// Prevents the use of the copy constructor. -+ SubunitTestProgressListener( const SubunitTestProgressListener © ); -+ -+ /// Prevents the use of the copy operator. -+ void operator =( const SubunitTestProgressListener © ); -+ -+private: -+ int last_test_failed; -+}; -+ -+ -+CPPUNIT_NS_END -+ -+#endif // CPPUNIT_SUBUNITTESTPROGRESSLISTENER_H |
