From 12195c836ac1f69dfaaa85a08d931c306530f9a3 Mon Sep 17 00:00:00 2001 From: ligazetom Date: Sun, 12 Jan 2025 23:51:36 +0100 Subject: [PATCH] Add test case stats functionality and test case name priority output --- src/catch2/catch_config.cpp | 2 ++ src/catch2/catch_config.hpp | 4 ++++ .../interfaces/catch_interfaces_config.hpp | 2 ++ src/catch2/internal/catch_commandline.cpp | 6 ++++++ .../reporters/catch_reporter_console.cpp | 20 +++++++++++++++++++ .../reporters/catch_reporter_console.hpp | 1 + 6 files changed, 35 insertions(+) diff --git a/src/catch2/catch_config.cpp b/src/catch2/catch_config.cpp index 352c1f42..36d3e3ea 100644 --- a/src/catch2/catch_config.cpp +++ b/src/catch2/catch_config.cpp @@ -205,6 +205,8 @@ namespace Catch { int Config::abortAfter() const { return m_data.abortAfter; } bool Config::showInvisibles() const { return m_data.showInvisibles; } Verbosity Config::verbosity() const { return m_data.verbosity; } + bool Config::outputTestCaseNameFirst() const { return m_data.outputTestCaseNameFirst; } + bool Config::outputTestCaseStats() const { return m_data.outputTestCaseStats; } bool Config::skipBenchmarks() const { return m_data.skipBenchmarks; } bool Config::benchmarkNoAnalysis() const { return m_data.benchmarkNoAnalysis; } diff --git a/src/catch2/catch_config.hpp b/src/catch2/catch_config.hpp index 17e983e5..dbabd3eb 100644 --- a/src/catch2/catch_config.hpp +++ b/src/catch2/catch_config.hpp @@ -58,6 +58,8 @@ namespace Catch { bool filenamesAsTags = false; bool libIdentify = false; bool allowZeroTests = false; + bool outputTestCaseNameFirst = false; + bool outputTestCaseStats = false; int abortAfter = -1; uint32_t rngSeed = generateRandomSeed(GenerateFrom::Default); @@ -132,6 +134,8 @@ namespace Catch { int abortAfter() const override; bool showInvisibles() const override; Verbosity verbosity() const override; + bool outputTestCaseNameFirst() const override; + bool outputTestCaseStats() const override; bool skipBenchmarks() const override; bool benchmarkNoAnalysis() const override; unsigned int benchmarkSamples() const override; diff --git a/src/catch2/interfaces/catch_interfaces_config.hpp b/src/catch2/interfaces/catch_interfaces_config.hpp index eb054805..4602f9f1 100644 --- a/src/catch2/interfaces/catch_interfaces_config.hpp +++ b/src/catch2/interfaces/catch_interfaces_config.hpp @@ -87,6 +87,8 @@ namespace Catch { virtual ColourMode defaultColourMode() const = 0; virtual std::vector const& getSectionsToRun() const = 0; virtual Verbosity verbosity() const = 0; + virtual bool outputTestCaseNameFirst() const = 0; + virtual bool outputTestCaseStats() const = 0; virtual bool skipBenchmarks() const = 0; virtual bool benchmarkNoAnalysis() const = 0; diff --git a/src/catch2/internal/catch_commandline.cpp b/src/catch2/internal/catch_commandline.cpp index 212f1774..c1973ac8 100644 --- a/src/catch2/internal/catch_commandline.cpp +++ b/src/catch2/internal/catch_commandline.cpp @@ -251,6 +251,12 @@ namespace Catch { | Opt( setVerbosity, "quiet|normal|high" ) ["-v"]["--verbosity"] ( "set output verbosity" ) + | Opt( config.outputTestCaseNameFirst) + ["--output-test-case-name-first"] + ( "test case name will be printed before the test is run" ) + | Opt( config.outputTestCaseStats) + ["--output-test-case-stats"] + ( "prints assertion stat data of the test case run" ) | Opt( config.listTests ) ["--list-tests"] ( "list all/matching test cases" ) diff --git a/src/catch2/reporters/catch_reporter_console.cpp b/src/catch2/reporters/catch_reporter_console.cpp index 9529f39a..c8c44853 100644 --- a/src/catch2/reporters/catch_reporter_console.cpp +++ b/src/catch2/reporters/catch_reporter_console.cpp @@ -512,11 +512,31 @@ void ConsoleReporter::benchmarkFailed( StringRef error ) { << ColumnBreak() << RowBreak(); } +void ConsoleReporter::testCaseStarting( TestCaseInfo const& testInfo ) { + StreamingReporterBase::testCaseStarting( testInfo ); + + if ( m_config->outputTestCaseNameFirst() ) { + m_stream << testInfo.name << ":\n"; + } +} + void ConsoleReporter::testCaseEnded(TestCaseStats const& _testCaseStats) { m_tablePrinter->close(); StreamingReporterBase::testCaseEnded(_testCaseStats); m_headerPrinted = false; + + if ( !_testCaseStats.aborting && m_config->outputTestCaseStats() ) { + if (!m_config->outputTestCaseNameFirst()) + { + m_stream << _testCaseStats.testInfo->name << ": "; + } + + m_stream << _testCaseStats.totals.assertions.passed << " passed " + << _testCaseStats.totals.assertions.failed << " failed " + << _testCaseStats.totals.assertions.skipped << " skipped\n"; + } } + void ConsoleReporter::testRunEnded(TestRunStats const& _testRunStats) { printTotalsDivider(_testRunStats.totals); printTestRunTotals( m_stream, *m_colour, _testRunStats.totals ); diff --git a/src/catch2/reporters/catch_reporter_console.hpp b/src/catch2/reporters/catch_reporter_console.hpp index 24377262..71da48bd 100644 --- a/src/catch2/reporters/catch_reporter_console.hpp +++ b/src/catch2/reporters/catch_reporter_console.hpp @@ -38,6 +38,7 @@ namespace Catch { void benchmarkEnded(BenchmarkStats<> const& stats) override; void benchmarkFailed( StringRef error ) override; + void testCaseStarting( TestCaseInfo const& testInfo ) override; void testCaseEnded(TestCaseStats const& _testCaseStats) override; void testRunEnded(TestRunStats const& _testRunStats) override; void testRunStarting(TestRunInfo const& _testRunInfo) override;