Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/tests/ExtraTests
diff options
context:
space:
mode:
authorMartin Hořeňovský2022-01-01 16:19:31 +0100
committerGitHub2022-01-01 16:19:31 +0100
commitf2f585b4788b61ed202e78b73b105f099d9cce7b (patch)
tree174f72ba09e22e564a571f96c21a175a68d85103 /tests/ExtraTests
parent6b55f5d780c5d8e935720f9d7d8ed28a3ba8617f (diff)
parentb5547f2ef6be18f70d2e9638d8f1eb0d8d42d2ba (diff)
downloadcatch3-f2f585b4788b61ed202e78b73b105f099d9cce7b.tar.gz
Merge pull request #2183 from mjerabek/dev-multiple-reporters
Add support for multiple reporters
Diffstat (limited to 'tests/ExtraTests')
-rw-r--r--tests/ExtraTests/CMakeLists.txt71
-rw-r--r--tests/ExtraTests/X24-ListenerStdoutCaptureInMultireporter.cpp40
-rw-r--r--tests/ExtraTests/X25-ListenerCanAskForCapturedStdout.cpp47
-rw-r--r--tests/ExtraTests/X26-ReporterPreferencesForPassingAssertionsIsRespected.cpp51
4 files changed, 209 insertions, 0 deletions
diff --git a/tests/ExtraTests/CMakeLists.txt b/tests/ExtraTests/CMakeLists.txt
index 3244e10f..1ac08445 100644
--- a/tests/ExtraTests/CMakeLists.txt
+++ b/tests/ExtraTests/CMakeLists.txt
@@ -256,6 +256,77 @@ 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(ReporterPreferencesForPassingAssertionsIsRespected ${TESTS_DIR}/X26-ReporterPreferencesForPassingAssertionsIsRespected.cpp)
+target_link_libraries(ReporterPreferencesForPassingAssertionsIsRespected PRIVATE Catch2::Catch2WithMain)
+add_test(
+ NAME Reporters::PreferencesForPassingAssertionsIsRespected
+ COMMAND ReporterPreferencesForPassingAssertionsIsRespected
+ --reporter test-reporter
+)
+set_tests_properties(
+ Reporters::PreferencesForPassingAssertionsIsRespected
+ PROPERTIES
+ PASS_REGULAR_EXPRESSION "X26 - TestReporter constructed"
+ FAIL_REGULAR_EXPRESSION "X26 - assertionEnded"
+)
+add_test(
+ NAME MultiReporter::PreferencesForPassingAssertionsIsRespected
+ COMMAND ReporterPreferencesForPassingAssertionsIsRespected
+ --reporter test-reporter
+ --reporter console::${_NullFile}
+)
+set_tests_properties(
+ MultiReporter::PreferencesForPassingAssertionsIsRespected
+ PROPERTIES
+ PASS_REGULAR_EXPRESSION "X26 - TestReporter constructed"
+ FAIL_REGULAR_EXPRESSION "X26 - assertionEnded"
+)
+
+
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..b36a307a
--- /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";
+}
diff --git a/tests/ExtraTests/X26-ReporterPreferencesForPassingAssertionsIsRespected.cpp b/tests/ExtraTests/X26-ReporterPreferencesForPassingAssertionsIsRespected.cpp
new file mode 100644
index 00000000..af3a3202
--- /dev/null
+++ b/tests/ExtraTests/X26-ReporterPreferencesForPassingAssertionsIsRespected.cpp
@@ -0,0 +1,51 @@
+
+// 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 reporter is not passed passing assertions when it
+ * doesn't ask for it.
+ */
+
+#include <catch2/catch_test_macros.hpp>
+#include <catch2/reporters/catch_reporter_streaming_base.hpp>
+#include <catch2/reporters/catch_reporter_registrars.hpp>
+
+#include <iostream>
+
+namespace {
+
+ class TestReporter : public Catch::StreamingReporterBase {
+ public:
+ TestReporter(Catch::ReporterConfig const& _config):
+ StreamingReporterBase(_config) {
+ m_preferences.shouldReportAllAssertions = false;
+ std::cout << "X26 - TestReporter constructed\n";
+ }
+
+ static std::string getDescription() {
+ return "X26 - test reporter that opts out of passing assertions";
+ }
+
+ void
+ assertionEnded( Catch::AssertionStats const& ) override {
+ std::cerr << "X26 - assertionEnded\n";
+ }
+
+ ~TestReporter() override;
+ };
+
+ TestReporter::~TestReporter() = default;
+
+}
+
+CATCH_REGISTER_REPORTER("test-reporter", TestReporter)
+
+TEST_CASE( "Test with only passing assertions" ) {
+ REQUIRE( 1 == 1 );
+ REQUIRE( 2 == 2 );
+}