mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-22 13:26:10 +01:00
stdout/ stderr capturing
This commit is contained in:
parent
9a703f73d4
commit
ce319b79e5
@ -22,10 +22,16 @@ TEST_CASE( "succeeding/Misc/Sections", "random SECTION tests" )
|
||||
EXPECT( a != b );
|
||||
EXPECT( b != a );
|
||||
}
|
||||
|
||||
|
||||
SECTION( "s2", "not equal" )
|
||||
{
|
||||
EXPECT_NOT( a == b);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TEST_CASE( "succeeding/Misc/stdout,stderr", "Sends stuff to stdout and stderr" )
|
||||
{
|
||||
std::cout << "Some information";
|
||||
|
||||
std::cerr << "An error";
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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() << "</TestCase>" << std::endl;
|
||||
|
@ -31,7 +31,8 @@ namespace Catch
|
||||
typedef int YesType;
|
||||
|
||||
// create a T for use in sizeof expressions
|
||||
template<typename T> T Synth();
|
||||
template<typename T> T Synth();
|
||||
|
||||
}
|
||||
|
||||
#endif // TWOBLUECUBES_CATCH_COMMON_H_INCLUDED
|
@ -89,7 +89,7 @@ namespace Catch
|
||||
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 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;
|
||||
};
|
||||
|
@ -45,6 +45,30 @@ namespace Catch
|
||||
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
|
||||
{
|
||||
public:
|
||||
@ -92,9 +116,14 @@ namespace Catch
|
||||
IResultListener* prevListener = ResultsCapture::setListener( this );
|
||||
m_reporter->StartTestCase( testInfo );
|
||||
|
||||
std::string redirectedCout;
|
||||
std::string redirectedCerr;
|
||||
|
||||
try
|
||||
{
|
||||
testInfo.invoke();
|
||||
StreamRedirect coutRedir( std::cout, redirectedCout );
|
||||
StreamRedirect cerrRedir( std::cerr, redirectedCerr );
|
||||
testInfo.invoke();
|
||||
}
|
||||
catch( TestFailureException& )
|
||||
{
|
||||
@ -111,7 +140,7 @@ namespace Catch
|
||||
ResultsCapture::acceptResult( ResultWas::ThrewException );
|
||||
}
|
||||
|
||||
m_reporter->EndTestCase( testInfo );
|
||||
m_reporter->EndTestCase( testInfo, redirectedCout, redirectedCerr );
|
||||
ResultsCapture::setListener( prevListener );
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user