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

Add test for handling of Listeners requesting stdout capture

Commit
b4efa4751aa905ceb0b3795f7222b55fbeab8e29
Author
Martin Hořeňovský ·
Parents
22547a3c5f6e

Files · Download .tar.gz

Changes

 tests/ExtraTests/CMakeLists.txt                    | 45 +++++++++++++++++++++
 .../X24-ListenerStdoutCaptureInMultireporter.cpp   | 40 ++++++++++++++++++
 .../X25-ListenerCanAskForCapturedStdout.cpp        | 47 ++++++++++++++++++++++
 3 files changed, 132 insertions(+)

diff --git a/tests/ExtraTests/CMakeLists.txt b/tests/ExtraTests/CMakeLists.txt
index 3244e10f..b503ca44 100644
--- a/tests/ExtraTests/CMakeLists.txt
+++ b/tests/ExtraTests/CMakeLists.txt
@@ -256,6 +256,51 @@ set_tests_properties(
 )
 
 
+if (MSVC)
+  set(_NullFile "NUL")
+else()
+  set(_NullFile "/dev/null")
+endif()
+
+add_executable(ListenerStdoutCaptureInMultireporter ${TESTS_DIR}/X24-ListenerStdoutCaptureInMultireporter.cpp)
+target_link_libraries(ListenerStdoutCaptureInMultireporter PRIVATE Catch2::Catch2WithMain)
+
+# This test checks that there is nothing written out from the process,
+# but if CMake is running the tests under Valgrind or similar tool, then
+# that will write its own output to stdout and the test would fail.
+if (NOT MEMORYCHECK_COMMAND)
+  add_test(
+    NAME MultiReporter::NoncapturingListenerDoesntCauseStdoutPassThrough
+    COMMAND ListenerStdoutCaptureInMultireporter
+      --reporter xml::${_NullFile}
+      --reporter junit::${_NullFile}
+  )
+
+  set_tests_properties(
+      MultiReporter::NoncapturingListenerDoesntCauseStdoutPassThrough
+    PROPERTIES
+      PASS_REGULAR_EXPRESSION "X24 - NonCapturingListener initialized"
+      FAIL_REGULAR_EXPRESSION "X24 - FooBarBaz"
+  )
+endif()
+
+
+add_executable(ListenerCanAskForCapturedStdout ${TESTS_DIR}/X25-ListenerCanAskForCapturedStdout.cpp)
+target_link_libraries(ListenerCanAskForCapturedStdout PRIVATE Catch2::Catch2WithMain)
+add_test(
+  NAME MultiReporter::CapturingListenerCausesStdoutCapture
+  COMMAND ListenerCanAskForCapturedStdout
+    --reporter compact::${_NullFile}
+    --reporter console::${_NullFile}
+)
+set_tests_properties(
+    MultiReporter::CapturingListenerCausesStdoutCapture
+  PROPERTIES
+    PASS_REGULAR_EXPRESSION "CapturingListener initialized"
+    FAIL_REGULAR_EXPRESSION "X25 - ERROR"
+)
+
+
 add_executable(DuplicatedTestCases-SameNameAndTags ${TESTS_DIR}/X31-DuplicatedTestCases.cpp)
 target_link_libraries(DuplicatedTestCases-SameNameAndTags PRIVATE Catch2::Catch2WithMain)
 add_test(
diff --git a/tests/ExtraTests/X24-ListenerStdoutCaptureInMultireporter.cpp b/tests/ExtraTests/X24-ListenerStdoutCaptureInMultireporter.cpp
new file mode 100644
index 00000000..4c6148fd
--- /dev/null
+++ b/tests/ExtraTests/X24-ListenerStdoutCaptureInMultireporter.cpp
@@ -0,0 +1,40 @@
+
+//              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
+ * Registers custom listener that does not ask for captured stdout/err
+ *
+ * Running the binary with this listener, and asking for multiple _capturing_
+ * reporters (one would be sufficient, but that would also mean depending on
+ * implementation details inside Catch2's handling of listeners), we check that
+ * nothing is written to stdout, because listeners should not be considered in
+ * whether the stdout should be passed-through or not.
+ */
+
+#include <catch2/catch_test_macros.hpp>
+#include <catch2/reporters/catch_reporter_event_listener.hpp>
+#include <catch2/reporters/catch_reporter_registrars.hpp>
+
+#include <iostream>
+
+namespace {
+	class NonCapturingListener : public Catch::EventListenerBase {
+    public:
+        NonCapturingListener( Catch::ReporterConfig const& config ):
+            EventListenerBase( config ) {
+            m_preferences.shouldRedirectStdOut = false;
+            std::cerr << "X24 - NonCapturingListener initialized.\n";
+		}
+	};
+}
+
+CATCH_REGISTER_LISTENER( NonCapturingListener )
+
+TEST_CASE( "Writes to stdout" ) {
+	std::cout << "X24 - FooBarBaz\n";
+}
diff --git a/tests/ExtraTests/X25-ListenerCanAskForCapturedStdout.cpp b/tests/ExtraTests/X25-ListenerCanAskForCapturedStdout.cpp
new file mode 100644
index 00000000..e831abca
--- /dev/null
+++ b/tests/ExtraTests/X25-ListenerCanAskForCapturedStdout.cpp
@@ -0,0 +1,47 @@
+
+//              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
+ * Registers custom listener that asks for captured stdout/err
+ *
+ * Running the binary with this listener, and asking for multiple _noncapturing_
+ * reporters (one would be sufficient, but that would also mean depending on
+ * implementation details inside Catch2's handling of listeners), we check that
+ * the listener gets redirected stdout, even though the reporters didn't ask for
+ * it.
+ */
+
+#include <catch2/catch_test_macros.hpp>
+#include <catch2/reporters/catch_reporter_event_listener.hpp>
+#include <catch2/reporters/catch_reporter_registrars.hpp>
+
+#include <iostream>
+
+namespace {
+	class CapturingListener : public Catch::EventListenerBase {
+    public:
+        CapturingListener( Catch::ReporterConfig const& config ):
+            EventListenerBase( config ) {
+            m_preferences.shouldRedirectStdOut = true;
+            std::cerr << "CapturingListener initialized\n";
+		}
+
+		void
+			testCaseEnded(Catch::TestCaseStats const& testCaseStats) override {
+            if ( testCaseStats.stdOut.empty() ) {
+                std::cerr << "X25 - ERROR: empty stdout\n";
+            }
+		}
+	};
+}
+
+CATCH_REGISTER_LISTENER( CapturingListener )
+
+TEST_CASE( "Writes to stdout" ) {
+	std::cout << "X25 - FooBarBaz\n";
+}