catch3
Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.
Clonegit clone https://git.progmasoft.com/catch3
stdout/ stderr capturing
- Commit
ce319b79e5e7c3351d01707807fa4d9c67ab7e16- Author
- Phil Nash ·
- Parents
9a703f73d4e2
Changes
Test/MiscTests.cpp | 10 ++++++++-- catch_reporter_basic.hpp | 8 +++++++- catch_reporter_xml.hpp | 2 +- internal/catch_common.h | 3 ++- internal/catch_reporter_registry.hpp | 2 +- internal/catch_runner_impl.hpp | 33 +++++++++++++++++++++++++++++++-- 6 files changed, 50 insertions(+), 8 deletions(-) diff --git a/Test/MiscTests.cpp b/Test/MiscTests.cpp index ea004cc8..5ec21d31 100644 --- a/Test/MiscTests.cpp +++ b/Test/MiscTests.cpp @@ -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"; +} \ No newline at end of file diff --git a/catch_reporter_basic.hpp b/catch_reporter_basic.hpp index 3026f70a..4a656fb0 100644 --- a/catch_reporter_basic.hpp +++ b/catch_reporter_basic.hpp @@ -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; } diff --git a/catch_reporter_xml.hpp b/catch_reporter_xml.hpp index c9953da2..f5e1cd18 100644 --- a/catch_reporter_xml.hpp +++ b/catch_reporter_xml.hpp @@ -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; diff --git a/internal/catch_common.h b/internal/catch_common.h index 46460853..7a882e64 100644 --- a/internal/catch_common.h +++ b/internal/catch_common.h @@ -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 \ No newline at end of file diff --git a/internal/catch_reporter_registry.hpp b/internal/catch_reporter_registry.hpp index bf89ff31..df4e3241 100644 --- a/internal/catch_reporter_registry.hpp +++ b/internal/catch_reporter_registry.hpp @@ -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; }; diff --git a/internal/catch_runner_impl.hpp b/internal/catch_runner_impl.hpp index 596bfb78..4023b60f 100644 --- a/internal/catch_runner_impl.hpp +++ b/internal/catch_runner_impl.hpp @@ -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 ); }