Progmasoft Git

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

Don't send full stdout so far to testCasePartialEnded event

Commit
6b55f5d780c5d8e935720f9d7d8ed28a3ba8617f
Author
Martin Hořeňovský ·
Parents
c9c3b748054c

Files · Download .tar.gz

Changes

 src/catch2/internal/catch_run_context.cpp          |  2 +-
 tests/ExtraTests/CMakeLists.txt                    | 14 ++++
 .../X27-CapturedStdoutInTestCaseEvents.cpp         | 81 ++++++++++++++++++++++
 3 files changed, 96 insertions(+), 1 deletion(-)

diff --git a/src/catch2/internal/catch_run_context.cpp b/src/catch2/internal/catch_run_context.cpp
index b8e74e5f..41d819ba 100644
--- a/src/catch2/internal/catch_run_context.cpp
+++ b/src/catch2/internal/catch_run_context.cpp
@@ -206,7 +206,7 @@ namespace Catch {
             redirectedCerr += oneRunCerr;
 
             const auto singleRunTotals = m_totals.delta(beforeRunTotals);
-            auto statsForOneRun = TestCaseStats(testInfo, singleRunTotals, redirectedCout, oneRunCerr, aborting());
+            auto statsForOneRun = TestCaseStats(testInfo, singleRunTotals, oneRunCout, oneRunCerr, aborting());
 
             m_reporter->testCasePartialEnded(statsForOneRun, testRuns);
             ++testRuns;
diff --git a/tests/ExtraTests/CMakeLists.txt b/tests/ExtraTests/CMakeLists.txt
index b3b8dbec..3244e10f 100644
--- a/tests/ExtraTests/CMakeLists.txt
+++ b/tests/ExtraTests/CMakeLists.txt
@@ -241,6 +241,20 @@ set_tests_properties(
     PASS_REGULAR_EXPRESSION "TestReporter constructed"
 )
 
+add_executable(CapturedStdoutInTestCaseEvents ${TESTS_DIR}/X27-CapturedStdoutInTestCaseEvents.cpp)
+target_link_libraries(CapturedStdoutInTestCaseEvents PRIVATE Catch2::Catch2WithMain)
+add_test(
+  NAME Reporters::CapturedStdOutInEvents
+  COMMAND CapturedStdoutInTestCaseEvents
+    --reporter test-reporter
+)
+set_tests_properties(
+    Reporters::CapturedStdOutInEvents
+  PROPERTIES
+    PASS_REGULAR_EXPRESSION "X27 - TestReporter constructed"
+    FAIL_REGULAR_EXPRESSION "X27 ERROR"
+)
+
 
 add_executable(DuplicatedTestCases-SameNameAndTags ${TESTS_DIR}/X31-DuplicatedTestCases.cpp)
 target_link_libraries(DuplicatedTestCases-SameNameAndTags PRIVATE Catch2::Catch2WithMain)
diff --git a/tests/ExtraTests/X27-CapturedStdoutInTestCaseEvents.cpp b/tests/ExtraTests/X27-CapturedStdoutInTestCaseEvents.cpp
new file mode 100644
index 00000000..871ed53c
--- /dev/null
+++ b/tests/ExtraTests/X27-CapturedStdoutInTestCaseEvents.cpp
@@ -0,0 +1,81 @@
+
+//              Copyright Catch2 Authors
+// Distributed under the Boost Software License, Version 1.0.
+//   (See accompanying file LICENSE_1_0.txt or copy at
+//        https://www.boost.org/LICENSE_1_0.txt)
+
+// SPDX-License-Identifier: BSL-1.0
+
+/**\file
+ * Test that the captured stdout/err in (partial) testCaseEnded events
+ * is correct (e.g. that the partial test case event does not get accumulated
+ * output).
+ *
+ * This is done by having a single test case that is entered multiple
+ * times through generator, and a custom capturing reporter that knows
+ * what it should expect captured from the test case.
+ */
+
+
+#include <catch2/catch_test_macros.hpp>
+#include <catch2/generators/catch_generators.hpp>
+#include <catch2/generators/catch_generators_range.hpp>
+#include <catch2/reporters/catch_reporter_streaming_base.hpp>
+#include <catch2/reporters/catch_reporter_registrars.hpp>
+
+
+#include <iostream>
+#include <string>
+
+class TestReporter : public Catch::StreamingReporterBase {
+    std::string stdOutString( uint64_t iter ){
+        return "stdout " + std::to_string( iter ) + '\n';
+    }
+    std::string stdErrString(uint64_t iter) {
+        return "stderr " + std::to_string( iter ) + '\n';
+    }
+
+public:
+    TestReporter( Catch::ReporterConfig const& _config ):
+        StreamingReporterBase( _config ) {
+        m_preferences.shouldRedirectStdOut = true;
+        std::cout << "X27 - TestReporter constructed\n";
+    }
+
+    static std::string getDescription() {
+        return "X27 test reporter";
+    }
+
+    void testCasePartialEnded( Catch::TestCaseStats const& stats,
+                               uint64_t iter ) override {
+        if ( stats.stdOut != stdOutString( iter ) ) {
+            std::cerr << "X27 ERROR in partial stdout\n" << stats.stdOut;
+        }
+        if ( stats.stdErr != stdErrString( iter ) ) {
+            std::cerr << "X27 ERROR in partial stderr\n" << stats.stdErr;
+        }
+    }
+
+    void testCaseEnded( Catch::TestCaseStats const& stats ) override {
+        if ( stats.stdOut != "stdout 0\nstdout 1\nstdout 2\nstdout 3\nstdout 4\nstdout 5\n" ) {
+            std::cerr << "X27 ERROR in full stdout\n" << stats.stdOut;
+        }
+        if ( stats.stdErr != "stderr 0\nstderr 1\nstderr 2\nstderr 3\nstderr 4\nstderr 5\n" ) {
+            std::cerr << "X27 ERROR in full stderr\n" << stats.stdErr;
+        }
+    }
+
+    ~TestReporter() override;
+};
+
+TestReporter::~TestReporter() = default;
+
+CATCH_REGISTER_REPORTER( "test-reporter", TestReporter )
+
+TEST_CASE( "repeatedly entered test case" ) {
+    auto i = GENERATE( range(0, 6) );
+    std::cout << "stdout " << i << '\n';
+    // Switch between writing to std::cerr and std::clog just to make sure
+    // both are properly captured and redirected.
+    ( ( i % 2 == 0 ) ? std::cerr : std::clog ) << "stderr " << i << '\n';
+}