stdout/ stderr capturing

This commit is contained in:
Phil Nash 2010-11-30 06:48:21 +00:00
parent 9a703f73d4
commit ce319b79e5
6 changed files with 50 additions and 8 deletions

View File

@ -27,5 +27,11 @@ TEST_CASE( "succeeding/Misc/Sections", "random SECTION tests" )
{ {
EXPECT_NOT( a == b); EXPECT_NOT( a == b);
} }
}
TEST_CASE( "succeeding/Misc/stdout,stderr", "Sends stuff to stdout and stderr" )
{
std::cout << "Some information";
std::cerr << "An error";
} }

View File

@ -153,8 +153,14 @@ namespace Catch
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
virtual void EndTestCase( const TestCaseInfo& testInfo ) virtual void EndTestCase( const TestCaseInfo& testInfo, const std::string& stdOut, const std::string& stdErr )
{ {
if( !stdOut.empty() )
m_config.stream() << "[stdout: " << stdOut << "]\n";
if( !stdErr.empty() )
m_config.stream() << "[stderr: " << stdErr << "]\n";
m_config.stream() << "[Finished: " << testInfo.getName() << "]" << std::endl; m_config.stream() << "[Finished: " << testInfo.getName() << "]" << std::endl;
} }

View File

@ -91,7 +91,7 @@ namespace Catch
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
virtual void EndTestCase( const Catch::TestCaseInfo& ) virtual void EndTestCase( const Catch::TestCaseInfo&, const std::string& stdOut, const std::string& stdErr )
{ {
m_config.stream() << "\t<OverallResult success='" << (m_currentTestSuccess ? "true" : "false" ) << "/>\n"; m_config.stream() << "\t<OverallResult success='" << (m_currentTestSuccess ? "true" : "false" ) << "/>\n";
m_config.stream() << "</TestCase>" << std::endl; m_config.stream() << "</TestCase>" << std::endl;

View File

@ -32,6 +32,7 @@ namespace Catch
// create a T for use in sizeof expressions // create a T for use in sizeof expressions
template<typename T> T Synth(); template<typename T> T Synth();
} }
#endif // TWOBLUECUBES_CATCH_COMMON_H_INCLUDED #endif // TWOBLUECUBES_CATCH_COMMON_H_INCLUDED

View File

@ -89,7 +89,7 @@ namespace Catch
virtual void EndSection( const std::string& sectionName, std::size_t succeeded, std::size_t failed ) = 0; virtual void EndSection( const std::string& sectionName, std::size_t succeeded, std::size_t failed ) = 0;
virtual void StartTestCase( const TestCaseInfo& testInfo ) = 0; virtual void StartTestCase( const TestCaseInfo& testInfo ) = 0;
virtual void EndTestCase( const TestCaseInfo& testInfo ) = 0; virtual void EndTestCase( const TestCaseInfo& testInfo, const std::string& stdOut, const std::string& stdErr ) = 0;
virtual void Result( const ResultInfo& result ) = 0; virtual void Result( const ResultInfo& result ) = 0;
}; };

View File

@ -45,6 +45,30 @@ namespace Catch
bool m_isWildcarded; bool m_isWildcarded;
}; };
class StreamRedirect
{
public:
StreamRedirect( std::ostream& stream, std::string& targetString )
: m_stream( stream ),
m_prevBuf( stream.rdbuf() ),
m_targetString( targetString )
{
stream.rdbuf( m_oss.rdbuf() );
}
~StreamRedirect()
{
m_targetString = m_oss.str();
m_stream.rdbuf( m_prevBuf );
}
private:
std::ostream& m_stream;
std::streambuf* m_prevBuf;
std::ostringstream m_oss;
std::string& m_targetString;
};
class Runner : public IResultListener class Runner : public IResultListener
{ {
public: public:
@ -92,8 +116,13 @@ namespace Catch
IResultListener* prevListener = ResultsCapture::setListener( this ); IResultListener* prevListener = ResultsCapture::setListener( this );
m_reporter->StartTestCase( testInfo ); m_reporter->StartTestCase( testInfo );
std::string redirectedCout;
std::string redirectedCerr;
try try
{ {
StreamRedirect coutRedir( std::cout, redirectedCout );
StreamRedirect cerrRedir( std::cerr, redirectedCerr );
testInfo.invoke(); testInfo.invoke();
} }
catch( TestFailureException& ) catch( TestFailureException& )
@ -111,7 +140,7 @@ namespace Catch
ResultsCapture::acceptResult( ResultWas::ThrewException ); ResultsCapture::acceptResult( ResultWas::ThrewException );
} }
m_reporter->EndTestCase( testInfo ); m_reporter->EndTestCase( testInfo, redirectedCout, redirectedCerr );
ResultsCapture::setListener( prevListener ); ResultsCapture::setListener( prevListener );
} }