From ca54d372453fbbea136592bbab2e64d44af46373 Mon Sep 17 00:00:00 2001 From: Tor Lillqvist Date: Thu, 24 Apr 2014 10:32:38 +0300 Subject: Add a test listener that outputs failures to stderr as they happen Makes them intermix properly with debugging output from the tested functionality. After the test has run, don't repeat the failure messages, just print a short summary. Change-Id: I6b491d3a34094df2da11df5e404adb63299de342 --- sal/cppunittester/cppunittester.cxx | 41 ++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) (limited to 'sal') diff --git a/sal/cppunittester/cppunittester.cxx b/sal/cppunittester/cppunittester.cxx index 68858febcbb8..0888d1bd7836 100644 --- a/sal/cppunittester/cppunittester.cxx +++ b/sal/cppunittester/cppunittester.cxx @@ -37,6 +37,8 @@ #include "sal/main.h" #include "cppunit/CompilerOutputter.h" +#include "cppunit/Exception.h" +#include "cppunit/TestFailure.h" #include "cppunit/TestResult.h" #include "cppunit/TestResultCollector.h" #include "cppunit/TestRunner.h" @@ -130,6 +132,37 @@ public: }; #endif +class LogFailuresAsTheyHappen : public CppUnit::TestListener +{ +public: + virtual void addFailure( const CppUnit::TestFailure &failure ) SAL_OVERRIDE + { + printFailureLocation( failure.sourceLine() ); + printFailedTestName( failure ); + printFailureMessage( failure ); + } + +private: + void printFailureLocation( const CppUnit::SourceLine &sourceLine ) + { + if ( !sourceLine.isValid() ) + std::cerr << "unknown:0:"; + else + std::cerr << sourceLine.fileName() << ":" << sourceLine.lineNumber() << ":"; + } + + void printFailedTestName( const CppUnit::TestFailure &failure ) + { + std::cerr << failure.failedTestName() << std::endl; + } + + void printFailureMessage( const CppUnit::TestFailure &failure ) + { + std::cerr << failure.thrownException()->message().shortDescription() << std::endl; + std::cerr << failure.thrownException()->message().details() << std::endl; + } +}; + //Allow the whole uniting testing framework to be run inside a "Protector" //which knows about uno exceptions, so it can print the content of the //exception before falling over and dying @@ -182,6 +215,9 @@ public: CppUnit::TestResultCollector collector; result.addListener(&collector); + LogFailuresAsTheyHappen logger; + result.addListener(&logger); + #ifdef TIMETESTS TimingListener timer; result.addListener(&timer); @@ -199,7 +235,10 @@ public: for (size_t i = 0; i < protectors.size(); ++i) result.popProtector(); - CppUnit::CompilerOutputter(&collector, CppUnit::stdCErr()).write(); + if (collector.wasSuccessful()) + CppUnit::CompilerOutputter(&collector, CppUnit::stdCErr()).printSuccess(); + else + CppUnit::CompilerOutputter(&collector, CppUnit::stdCErr()).printStatistics(); return collector.wasSuccessful(); } virtual bool operator()() const SAL_OVERRIDE -- cgit v1.2.3