Add test for handling of Listeners requesting stdout capture

This commit is contained in:
Martin Hořeňovský 2021-12-30 15:49:15 +01:00
parent 22547a3c5f
commit b4efa4751a
3 changed files with 132 additions and 0 deletions

View File

@ -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(

View File

@ -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";
}

View File

@ -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";
}