1
0
mirror of https://github.com/catchorg/Catch2.git synced 2025-01-15 22:58:02 +00:00

Compare commits

...

5 Commits

Author SHA1 Message Date
Martin Hořeňovský
0b2874b6b1
Move SonarQube reporter implementation to a cpp file 2020-02-07 11:26:33 +01:00
Martin Hořeňovský
e6ea53ab49
Move Automake reporter to a cpp file 2020-02-07 11:26:30 +01:00
Martin Hořeňovský
338572a4f7
Move TAP reporter implementation to a cpp file 2020-02-07 11:26:28 +01:00
Martin Hořeňovský
70836d49ba
Move include of catch_enforce out of the reporter bases 2020-02-07 11:26:24 +01:00
Martin Hořeňovský
db148c42d7
Move TeamCity implementation to a cpp file 2020-02-07 11:26:21 +01:00
9 changed files with 539 additions and 505 deletions

View File

@ -9,4 +9,22 @@ namespace Catch {
AutomakeReporter::~AutomakeReporter() {}
void AutomakeReporter::testCaseEnded(TestCaseStats const& _testCaseStats) {
// Possible values to emit are PASS, XFAIL, SKIP, FAIL, XPASS and ERROR.
stream << ":test-result: ";
if (_testCaseStats.totals.assertions.allPassed()) {
stream << "PASS";
} else if (_testCaseStats.totals.assertions.allOk()) {
stream << "XFAIL";
} else {
stream << "FAIL";
}
stream << ' ' << _testCaseStats.testInfo->name << '\n';
StreamingReporterBase::testCaseEnded(_testCaseStats);
}
void AutomakeReporter::skipTest(TestCaseInfo const& testInfo) {
stream << ":test-result: SKIP " << testInfo.name << '\n';
}
} // end namespace Catch

View File

@ -1,7 +1,4 @@
/*
* Created by Justin R. Wilson on 2/19/2017.
* Copyright 2017 Justin R. Wilson. All rights reserved.
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
@ -20,30 +17,17 @@ namespace Catch {
~AutomakeReporter() override;
static std::string getDescription() {
return "Reports test results in the format of Automake .trs files";
using namespace std::string_literals;
return "Reports test results in the format of Automake .trs files"s;
}
void assertionStarting( AssertionInfo const& ) override {}
bool assertionEnded( AssertionStats const& /*_assertionStats*/ ) override { return true; }
void testCaseEnded( TestCaseStats const& _testCaseStats ) override {
// Possible values to emit are PASS, XFAIL, SKIP, FAIL, XPASS and ERROR.
stream << ":test-result: ";
if (_testCaseStats.totals.assertions.allPassed()) {
stream << "PASS";
} else if (_testCaseStats.totals.assertions.allOk()) {
stream << "XFAIL";
} else {
stream << "FAIL";
}
stream << ' ' << _testCaseStats.testInfo->name << '\n';
StreamingReporterBase::testCaseEnded( _testCaseStats );
}
void testCaseEnded(TestCaseStats const& _testCaseStats) override;
void skipTest( TestCaseInfo const& testInfo ) override {
stream << ":test-result: SKIP " << testInfo.name << '\n';
}
void skipTest(TestCaseInfo const& testInfo) override;
};

View File

@ -8,7 +8,6 @@
#ifndef TWOBLUECUBES_CATCH_REPORTER_BASES_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_REPORTER_BASES_HPP_INCLUDED
#include <catch2/catch_enforce.h>
#include <catch2/catch_interfaces_reporter.h>
#include <algorithm>

View File

@ -5,8 +5,131 @@
#include <catch2/reporters/catch_reporter_sonarqube.hpp>
#include <map>
namespace Catch {
SonarQubeReporter::~SonarQubeReporter() {}
void SonarQubeReporter::testRunStarting(TestRunInfo const& testRunInfo) {
CumulativeReporterBase::testRunStarting(testRunInfo);
xml.startElement("testExecutions");
xml.writeAttribute("version", '1');
}
void SonarQubeReporter::testGroupEnded(TestGroupStats const& testGroupStats) {
CumulativeReporterBase::testGroupEnded(testGroupStats);
writeGroup(*m_testGroups.back());
}
void SonarQubeReporter::writeGroup(TestGroupNode const& groupNode) {
std::map<std::string, TestGroupNode::ChildNodes> testsPerFile;
for (auto const& child : groupNode.children)
testsPerFile[child->value.testInfo->lineInfo.file].push_back(child);
for (auto const& kv : testsPerFile)
writeTestFile(kv.first, kv.second);
}
void SonarQubeReporter::writeTestFile(std::string const& filename, TestGroupNode::ChildNodes const& testCaseNodes) {
XmlWriter::ScopedElement e = xml.scopedElement("file");
xml.writeAttribute("path", filename);
for (auto const& child : testCaseNodes)
writeTestCase(*child);
}
void SonarQubeReporter::writeTestCase(TestCaseNode const& testCaseNode) {
// All test cases have exactly one section - which represents the
// test case itself. That section may have 0-n nested sections
assert(testCaseNode.children.size() == 1);
SectionNode const& rootSection = *testCaseNode.children.front();
writeSection("", rootSection, testCaseNode.value.testInfo->okToFail());
}
void SonarQubeReporter::writeSection(std::string const& rootName, SectionNode const& sectionNode, bool okToFail) {
std::string name = trim(sectionNode.stats.sectionInfo.name);
if (!rootName.empty())
name = rootName + '/' + name;
if (!sectionNode.assertions.empty() || !sectionNode.stdOut.empty() || !sectionNode.stdErr.empty()) {
XmlWriter::ScopedElement e = xml.scopedElement("testCase");
xml.writeAttribute("name", name);
xml.writeAttribute("duration", static_cast<long>(sectionNode.stats.durationInSeconds * 1000));
writeAssertions(sectionNode, okToFail);
}
for (auto const& childNode : sectionNode.childSections)
writeSection(name, *childNode, okToFail);
}
void SonarQubeReporter::writeAssertions(SectionNode const& sectionNode, bool okToFail) {
for (auto const& assertion : sectionNode.assertions)
writeAssertion(assertion, okToFail);
}
void SonarQubeReporter::writeAssertion(AssertionStats const& stats, bool okToFail) {
AssertionResult const& result = stats.assertionResult;
if (!result.isOk()) {
std::string elementName;
if (okToFail) {
elementName = "skipped";
} else {
switch (result.getResultType()) {
case ResultWas::ThrewException:
case ResultWas::FatalErrorCondition:
elementName = "error";
break;
case ResultWas::ExplicitFailure:
elementName = "failure";
break;
case ResultWas::ExpressionFailed:
elementName = "failure";
break;
case ResultWas::DidntThrowException:
elementName = "failure";
break;
// We should never see these here:
case ResultWas::Info:
case ResultWas::Warning:
case ResultWas::Ok:
case ResultWas::Unknown:
case ResultWas::FailureBit:
case ResultWas::Exception:
elementName = "internalError";
break;
}
}
XmlWriter::ScopedElement e = xml.scopedElement(elementName);
ReusableStringStream messageRss;
messageRss << result.getTestMacroName() << "(" << result.getExpression() << ")";
xml.writeAttribute("message", messageRss.str());
ReusableStringStream textRss;
if (stats.totals.assertions.total() > 0) {
textRss << "FAILED:\n";
if (result.hasExpression()) {
textRss << "\t" << result.getExpressionInMacro() << "\n";
}
if (result.hasExpandedExpression()) {
textRss << "with expansion:\n\t" << result.getExpandedExpression() << "\n";
}
}
if (!result.getMessage().empty())
textRss << result.getMessage() << "\n";
for (auto const& msg : stats.infoMessages)
if (msg.type == ResultWas::Info)
textRss << msg.message << "\n";
textRss << "at " << result.getSourceInfo();
xml.writeText(textRss.str(), XmlFormatting::Newline);
}
}
} // end namespace Catch

View File

@ -1,17 +1,13 @@
/*
* Created by Daniel Garcia on 2018-12-04.
* Copyright Social Point SL. All rights reserved.
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef CATCH_REPORTER_SONARQUBE_HPP_INCLUDED
#define CATCH_REPORTER_SONARQUBE_HPP_INCLUDED
#include <catch2/catch_xmlwriter.h>
#include <catch2/reporters/catch_reporter_bases.hpp>
#include <map>
#include <catch2/catch_xmlwriter.h>
namespace Catch {
@ -27,140 +23,31 @@ namespace Catch {
~SonarQubeReporter() override;
static std::string getDescription() {
return "Reports test results in the Generic Test Data SonarQube XML format";
}
static std::set<Verbosity> getSupportedVerbosities() {
return { Verbosity::Normal };
using namespace std::string_literals;
return "Reports test results in the Generic Test Data SonarQube XML format"s;
}
void noMatchingTestCases(std::string const& /*spec*/) override {}
void testRunStarting(TestRunInfo const& testRunInfo) override {
CumulativeReporterBase::testRunStarting(testRunInfo);
xml.startElement("testExecutions");
xml.writeAttribute("version", "1");
}
void testRunStarting(TestRunInfo const& testRunInfo) override;
void testGroupEnded(TestGroupStats const& testGroupStats) override {
CumulativeReporterBase::testGroupEnded(testGroupStats);
writeGroup(*m_testGroups.back());
}
void testGroupEnded(TestGroupStats const& testGroupStats) override;
void testRunEndedCumulative() override {
xml.endElement();
}
void writeGroup(TestGroupNode const& groupNode) {
std::map<std::string, TestGroupNode::ChildNodes> testsPerFile;
for(auto const& child : groupNode.children)
testsPerFile[child->value.testInfo->lineInfo.file].push_back(child);
void writeGroup(TestGroupNode const& groupNode);
for(auto const& kv : testsPerFile)
writeTestFile(kv.first.c_str(), kv.second);
}
void writeTestFile(std::string const& filename, TestGroupNode::ChildNodes const& testCaseNodes);
void writeTestFile(const char* filename, TestGroupNode::ChildNodes const& testCaseNodes) {
XmlWriter::ScopedElement e = xml.scopedElement("file");
xml.writeAttribute("path", filename);
void writeTestCase(TestCaseNode const& testCaseNode);
for(auto const& child : testCaseNodes)
writeTestCase(*child);
}
void writeSection(std::string const& rootName, SectionNode const& sectionNode, bool okToFail);
void writeTestCase(TestCaseNode const& testCaseNode) {
// All test cases have exactly one section - which represents the
// test case itself. That section may have 0-n nested sections
assert(testCaseNode.children.size() == 1);
SectionNode const& rootSection = *testCaseNode.children.front();
writeSection("", rootSection, testCaseNode.value.testInfo->okToFail());
}
void writeAssertions(SectionNode const& sectionNode, bool okToFail);
void writeSection(std::string const& rootName, SectionNode const& sectionNode, bool okToFail) {
std::string name = trim(sectionNode.stats.sectionInfo.name);
if(!rootName.empty())
name = rootName + '/' + name;
if(!sectionNode.assertions.empty() || !sectionNode.stdOut.empty() || !sectionNode.stdErr.empty()) {
XmlWriter::ScopedElement e = xml.scopedElement("testCase");
xml.writeAttribute("name", name);
xml.writeAttribute("duration", static_cast<long>(sectionNode.stats.durationInSeconds * 1000));
writeAssertions(sectionNode, okToFail);
}
for(auto const& childNode : sectionNode.childSections)
writeSection(name, *childNode, okToFail);
}
void writeAssertions(SectionNode const& sectionNode, bool okToFail) {
for(auto const& assertion : sectionNode.assertions)
writeAssertion( assertion, okToFail);
}
void writeAssertion(AssertionStats const& stats, bool okToFail) {
AssertionResult const& result = stats.assertionResult;
if(!result.isOk()) {
std::string elementName;
if(okToFail) {
elementName = "skipped";
}
else {
switch(result.getResultType()) {
case ResultWas::ThrewException:
case ResultWas::FatalErrorCondition:
elementName = "error";
break;
case ResultWas::ExplicitFailure:
elementName = "failure";
break;
case ResultWas::ExpressionFailed:
elementName = "failure";
break;
case ResultWas::DidntThrowException:
elementName = "failure";
break;
// We should never see these here:
case ResultWas::Info:
case ResultWas::Warning:
case ResultWas::Ok:
case ResultWas::Unknown:
case ResultWas::FailureBit:
case ResultWas::Exception:
elementName = "internalError";
break;
}
}
XmlWriter::ScopedElement e = xml.scopedElement(elementName);
ReusableStringStream messageRss;
messageRss << result.getTestMacroName() << "(" << result.getExpression() << ")";
xml.writeAttribute("message", messageRss.str());
ReusableStringStream textRss;
if (stats.totals.assertions.total() > 0) {
textRss << "FAILED:\n";
if (result.hasExpression()) {
textRss << "\t" << result.getExpressionInMacro() << "\n";
}
if (result.hasExpandedExpression()) {
textRss << "with expansion:\n\t" << result.getExpandedExpression() << "\n";
}
}
if(!result.getMessage().empty())
textRss << result.getMessage() << "\n";
for(auto const& msg : stats.infoMessages)
if(msg.type == ResultWas::Info)
textRss << msg.message << "\n";
textRss << "at " << result.getSourceInfo();
xml.writeText(textRss.str(), XmlFormatting::Newline);
}
}
void writeAssertion(AssertionStats const& stats, bool okToFail);
private:
XmlWriter xml;

View File

@ -4,9 +4,219 @@
*/
#include <catch2/reporters/catch_reporter_tap.hpp>
#include <catch2/catch_console_colour.h>
#include <algorithm>
namespace Catch {
namespace {
class TapAssertionPrinter {
static constexpr StringRef failedString = "not ok"_sr;
static constexpr StringRef passedString = "ok"_sr;
public:
TapAssertionPrinter& operator= (TapAssertionPrinter const&) = delete;
TapAssertionPrinter(TapAssertionPrinter const&) = delete;
TapAssertionPrinter(std::ostream& _stream, AssertionStats const& _stats, std::size_t _counter)
: stream(_stream)
, result(_stats.assertionResult)
, messages(_stats.infoMessages)
, itMessage(_stats.infoMessages.begin())
, printInfoMessages(true)
, counter(_counter) {}
void print() {
itMessage = messages.begin();
switch (result.getResultType()) {
case ResultWas::Ok:
printResultType(passedString);
printOriginalExpression();
printReconstructedExpression();
if (!result.hasExpression())
printRemainingMessages(Colour::None);
else
printRemainingMessages();
break;
case ResultWas::ExpressionFailed:
if (result.isOk()) {
printResultType(passedString);
} else {
printResultType(failedString);
}
printOriginalExpression();
printReconstructedExpression();
if (result.isOk()) {
printIssue(" # TODO");
}
printRemainingMessages();
break;
case ResultWas::ThrewException:
printResultType(failedString);
printIssue("unexpected exception with message:"_sr);
printMessage();
printExpressionWas();
printRemainingMessages();
break;
case ResultWas::FatalErrorCondition:
printResultType(failedString);
printIssue("fatal error condition with message:"_sr);
printMessage();
printExpressionWas();
printRemainingMessages();
break;
case ResultWas::DidntThrowException:
printResultType(failedString);
printIssue("expected exception, got none"_sr);
printExpressionWas();
printRemainingMessages();
break;
case ResultWas::Info:
printResultType("info"_sr);
printMessage();
printRemainingMessages();
break;
case ResultWas::Warning:
printResultType("warning"_sr);
printMessage();
printRemainingMessages();
break;
case ResultWas::ExplicitFailure:
printResultType(failedString);
printIssue("explicitly"_sr);
printRemainingMessages(Colour::None);
break;
// These cases are here to prevent compiler warnings
case ResultWas::Unknown:
case ResultWas::FailureBit:
case ResultWas::Exception:
printResultType("** internal error **"_sr);
break;
}
}
private:
static Colour::Code dimColour() { return Colour::FileName; }
void printSourceInfo() const {
Colour colourGuard(dimColour());
stream << result.getSourceInfo() << ':';
}
void printResultType(StringRef passOrFail) const {
if (!passOrFail.empty()) {
stream << passOrFail << ' ' << counter << " -";
}
}
void printIssue(StringRef issue) const {
stream << ' ' << issue;
}
void printExpressionWas() {
if (result.hasExpression()) {
stream << ';';
{
Colour colour(dimColour());
stream << " expression was:";
}
printOriginalExpression();
}
}
void printOriginalExpression() const {
if (result.hasExpression()) {
stream << ' ' << result.getExpression();
}
}
void printReconstructedExpression() const {
if (result.hasExpandedExpression()) {
{
Colour colour(dimColour());
stream << " for: ";
}
std::string expr = result.getExpandedExpression();
std::replace(expr.begin(), expr.end(), '\n', ' ');
stream << expr;
}
}
void printMessage() {
if (itMessage != messages.end()) {
stream << " '" << itMessage->message << '\'';
++itMessage;
}
}
void printRemainingMessages(Colour::Code colour = dimColour()) {
if (itMessage == messages.end()) {
return;
}
// using messages.end() directly (or auto) yields compilation error:
std::vector<MessageInfo>::const_iterator itEnd = messages.end();
const std::size_t N = static_cast<std::size_t>(std::distance(itMessage, itEnd));
{
Colour colourGuard(colour);
stream << " with " << pluralise(N, "message") << ':';
}
for (; itMessage != itEnd; ) {
// If this assertion is a warning ignore any INFO messages
if (printInfoMessages || itMessage->type != ResultWas::Info) {
stream << " '" << itMessage->message << '\'';
if (++itMessage != itEnd) {
Colour colourGuard(dimColour());
stream << " and";
}
}
}
}
private:
std::ostream& stream;
AssertionResult const& result;
std::vector<MessageInfo> messages;
std::vector<MessageInfo>::const_iterator itMessage;
bool printInfoMessages;
std::size_t counter;
};
constexpr StringRef TapAssertionPrinter::failedString;
constexpr StringRef TapAssertionPrinter::passedString;
} // End anonymous namespace
TAPReporter::~TAPReporter() {}
void TAPReporter::noMatchingTestCases(std::string const& spec) {
stream << "# No test cases matched '" << spec << "'\n";
}
bool TAPReporter::assertionEnded(AssertionStats const& _assertionStats) {
++counter;
stream << "# " << currentTestCaseInfo->name << '\n';
TapAssertionPrinter printer(stream, _assertionStats, counter);
printer.print();
stream << '\n' << std::flush;
return true;
}
void TAPReporter::testRunEnded(TestRunStats const& _testRunStats) {
if (_testRunStats.totals.testCases.total() == 0) {
stream << "1..0 # Skipped: No tests ran.";
} else {
stream << "1.." << counter;
}
stream << "\n\n" << std::flush;
StreamingReporterBase::testRunEnded(_testRunStats);
}
} // end namespace Catch

View File

@ -1,18 +1,12 @@
/*
* Created by Colton Wolkins on 2015-08-15.
* Copyright 2015 Martin Moene. All rights reserved.
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef TWOBLUECUBES_CATCH_REPORTER_TAP_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_REPORTER_TAP_HPP_INCLUDED
#include <catch2/catch_console_colour.h>
#include <catch2/reporters/catch_reporter_bases.hpp>
#include <algorithm>
namespace Catch {
struct TAPReporter : StreamingReporterBase {
@ -22,220 +16,24 @@ namespace Catch {
~TAPReporter() override;
static std::string getDescription() {
return "Reports test results in TAP format, suitable for test harnesses";
using namespace std::string_literals;
return "Reports test results in TAP format, suitable for test harnesses"s;
}
ReporterPreferences getPreferences() const override {
return m_reporterPrefs;
}
void noMatchingTestCases( std::string const& spec ) override {
stream << "# No test cases matched '" << spec << "'" << std::endl;
}
void noMatchingTestCases(std::string const& spec) override;
void assertionStarting( AssertionInfo const& ) override {}
bool assertionEnded( AssertionStats const& _assertionStats ) override {
++counter;
bool assertionEnded(AssertionStats const& _assertionStats) override;
stream << "# " << currentTestCaseInfo->name << std::endl;
AssertionPrinter printer( stream, _assertionStats, counter );
printer.print();
stream << std::endl;
return true;
}
void testRunEnded( TestRunStats const& _testRunStats ) override {
printTotals( _testRunStats.totals );
stream << "\n" << std::endl;
StreamingReporterBase::testRunEnded( _testRunStats );
}
void testRunEnded(TestRunStats const& _testRunStats) override;
private:
std::size_t counter = 0;
class AssertionPrinter {
public:
AssertionPrinter& operator= ( AssertionPrinter const& ) = delete;
AssertionPrinter( AssertionPrinter const& ) = delete;
AssertionPrinter( std::ostream& _stream, AssertionStats const& _stats, std::size_t _counter )
: stream( _stream )
, result( _stats.assertionResult )
, messages( _stats.infoMessages )
, itMessage( _stats.infoMessages.begin() )
, printInfoMessages( true )
, counter(_counter)
{}
void print() {
itMessage = messages.begin();
switch( result.getResultType() ) {
case ResultWas::Ok:
printResultType( passedString() );
printOriginalExpression();
printReconstructedExpression();
if ( ! result.hasExpression() )
printRemainingMessages( Colour::None );
else
printRemainingMessages();
break;
case ResultWas::ExpressionFailed:
if (result.isOk()) {
printResultType(passedString());
} else {
printResultType(failedString());
}
printOriginalExpression();
printReconstructedExpression();
if (result.isOk()) {
printIssue(" # TODO");
}
printRemainingMessages();
break;
case ResultWas::ThrewException:
printResultType( failedString() );
printIssue( "unexpected exception with message:" );
printMessage();
printExpressionWas();
printRemainingMessages();
break;
case ResultWas::FatalErrorCondition:
printResultType( failedString() );
printIssue( "fatal error condition with message:" );
printMessage();
printExpressionWas();
printRemainingMessages();
break;
case ResultWas::DidntThrowException:
printResultType( failedString() );
printIssue( "expected exception, got none" );
printExpressionWas();
printRemainingMessages();
break;
case ResultWas::Info:
printResultType( "info" );
printMessage();
printRemainingMessages();
break;
case ResultWas::Warning:
printResultType( "warning" );
printMessage();
printRemainingMessages();
break;
case ResultWas::ExplicitFailure:
printResultType( failedString() );
printIssue( "explicitly" );
printRemainingMessages( Colour::None );
break;
// These cases are here to prevent compiler warnings
case ResultWas::Unknown:
case ResultWas::FailureBit:
case ResultWas::Exception:
printResultType( "** internal error **" );
break;
}
}
private:
static Colour::Code dimColour() { return Colour::FileName; }
static const char* failedString() { return "not ok"; }
static const char* passedString() { return "ok"; }
void printSourceInfo() const {
Colour colourGuard( dimColour() );
stream << result.getSourceInfo() << ":";
}
void printResultType( std::string const& passOrFail ) const {
if( !passOrFail.empty() ) {
stream << passOrFail << ' ' << counter << " -";
}
}
void printIssue( std::string const& issue ) const {
stream << " " << issue;
}
void printExpressionWas() {
if( result.hasExpression() ) {
stream << ";";
{
Colour colour( dimColour() );
stream << " expression was:";
}
printOriginalExpression();
}
}
void printOriginalExpression() const {
if( result.hasExpression() ) {
stream << " " << result.getExpression();
}
}
void printReconstructedExpression() const {
if( result.hasExpandedExpression() ) {
{
Colour colour( dimColour() );
stream << " for: ";
}
std::string expr = result.getExpandedExpression();
std::replace( expr.begin(), expr.end(), '\n', ' ');
stream << expr;
}
}
void printMessage() {
if ( itMessage != messages.end() ) {
stream << " '" << itMessage->message << "'";
++itMessage;
}
}
void printRemainingMessages( Colour::Code colour = dimColour() ) {
if (itMessage == messages.end()) {
return;
}
// using messages.end() directly (or auto) yields compilation error:
std::vector<MessageInfo>::const_iterator itEnd = messages.end();
const std::size_t N = static_cast<std::size_t>( std::distance( itMessage, itEnd ) );
{
Colour colourGuard( colour );
stream << " with " << pluralise( N, "message" ) << ":";
}
for(; itMessage != itEnd; ) {
// If this assertion is a warning ignore any INFO messages
if( printInfoMessages || itMessage->type != ResultWas::Info ) {
stream << " '" << itMessage->message << "'";
if ( ++itMessage != itEnd ) {
Colour colourGuard( dimColour() );
stream << " and";
}
}
}
}
private:
std::ostream& stream;
AssertionResult const& result;
std::vector<MessageInfo> messages;
std::vector<MessageInfo>::const_iterator itMessage;
bool printInfoMessages;
std::size_t counter;
};
void printTotals( const Totals& totals ) const {
if( totals.testCases.total() == 0 ) {
stream << "1..0 # Skipped: No tests ran.";
} else {
stream << "1.." << counter;
}
}
};
} // end namespace Catch

View File

@ -5,8 +5,166 @@
#include <catch2/reporters/catch_reporter_teamcity.hpp>
#include <catch2/catch_enforce.h>
#include <catch2/catch_text.h>
namespace Catch {
namespace {
// if string has a : in first line will set indent to follow it on
// subsequent lines
void printHeaderString(std::ostream& os, std::string const& _string, std::size_t indent = 0) {
std::size_t i = _string.find(": ");
if (i != std::string::npos)
i += 2;
else
i = 0;
os << Column(_string)
.indent(indent + i)
.initialIndent(indent) << "\n";
}
std::string escape(std::string const& str) {
std::string escaped = str;
replaceInPlace(escaped, "|", "||");
replaceInPlace(escaped, "'", "|'");
replaceInPlace(escaped, "\n", "|n");
replaceInPlace(escaped, "\r", "|r");
replaceInPlace(escaped, "[", "|[");
replaceInPlace(escaped, "]", "|]");
return escaped;
}
} // end anonymous namespace
TeamCityReporter::~TeamCityReporter() {}
void TeamCityReporter::testGroupStarting(GroupInfo const& groupInfo) {
StreamingReporterBase::testGroupStarting(groupInfo);
stream << "##teamcity[testSuiteStarted name='"
<< escape(groupInfo.name) << "']\n";
}
void TeamCityReporter::testGroupEnded(TestGroupStats const& testGroupStats) {
StreamingReporterBase::testGroupEnded(testGroupStats);
stream << "##teamcity[testSuiteFinished name='"
<< escape(testGroupStats.groupInfo.name) << "']\n";
}
bool TeamCityReporter::assertionEnded(AssertionStats const& assertionStats) {
AssertionResult const& result = assertionStats.assertionResult;
if (!result.isOk()) {
ReusableStringStream msg;
if (!m_headerPrintedForThisSection)
printSectionHeader(msg.get());
m_headerPrintedForThisSection = true;
msg << result.getSourceInfo() << '\n';
switch (result.getResultType()) {
case ResultWas::ExpressionFailed:
msg << "expression failed";
break;
case ResultWas::ThrewException:
msg << "unexpected exception";
break;
case ResultWas::FatalErrorCondition:
msg << "fatal error condition";
break;
case ResultWas::DidntThrowException:
msg << "no exception was thrown where one was expected";
break;
case ResultWas::ExplicitFailure:
msg << "explicit failure";
break;
// We shouldn't get here because of the isOk() test
case ResultWas::Ok:
case ResultWas::Info:
case ResultWas::Warning:
CATCH_ERROR("Internal error in TeamCity reporter");
// These cases are here to prevent compiler warnings
case ResultWas::Unknown:
case ResultWas::FailureBit:
case ResultWas::Exception:
CATCH_ERROR("Not implemented");
}
if (assertionStats.infoMessages.size() == 1)
msg << " with message:";
if (assertionStats.infoMessages.size() > 1)
msg << " with messages:";
for (auto const& messageInfo : assertionStats.infoMessages)
msg << "\n \"" << messageInfo.message << '"';
if (result.hasExpression()) {
msg <<
"\n " << result.getExpressionInMacro() << "\n"
"with expansion:\n"
" " << result.getExpandedExpression() << '\n';
}
if (currentTestCaseInfo->okToFail()) {
msg << "- failure ignore as test marked as 'ok to fail'\n";
stream << "##teamcity[testIgnored"
<< " name='" << escape(currentTestCaseInfo->name) << '\''
<< " message='" << escape(msg.str()) << '\''
<< "]\n";
} else {
stream << "##teamcity[testFailed"
<< " name='" << escape(currentTestCaseInfo->name) << '\''
<< " message='" << escape(msg.str()) << '\''
<< "]\n";
}
}
stream.flush();
return true;
}
void TeamCityReporter::testCaseStarting(TestCaseInfo const& testInfo) {
m_testTimer.start();
StreamingReporterBase::testCaseStarting(testInfo);
stream << "##teamcity[testStarted name='"
<< escape(testInfo.name) << "']\n";
stream.flush();
}
void TeamCityReporter::testCaseEnded(TestCaseStats const& testCaseStats) {
StreamingReporterBase::testCaseEnded(testCaseStats);
auto const& testCaseInfo = *testCaseStats.testInfo;
if (!testCaseStats.stdOut.empty())
stream << "##teamcity[testStdOut name='"
<< escape(testCaseInfo.name)
<< "' out='" << escape(testCaseStats.stdOut) << "']\n";
if (!testCaseStats.stdErr.empty())
stream << "##teamcity[testStdErr name='"
<< escape(testCaseInfo.name)
<< "' out='" << escape(testCaseStats.stdErr) << "']\n";
stream << "##teamcity[testFinished name='"
<< escape(testCaseInfo.name) << "' duration='"
<< m_testTimer.getElapsedMilliseconds() << "']\n";
stream.flush();
}
void TeamCityReporter::printSectionHeader(std::ostream& os) {
assert(!m_sectionStack.empty());
if (m_sectionStack.size() > 1) {
os << getLineOfChars<'-'>() << '\n';
std::vector<SectionInfo>::const_iterator
it = m_sectionStack.begin() + 1, // Skip first section (test case)
itEnd = m_sectionStack.end();
for (; it != itEnd; ++it)
printHeaderString(os, it->name);
os << getLineOfChars<'-'>() << '\n';
}
SourceLineInfo lineInfo = m_sectionStack.front().lineInfo;
os << lineInfo << '\n';
os << getLineOfChars<'.'>() << "\n\n";
}
} // end namespace Catch

View File

@ -1,15 +1,10 @@
/*
* Created by Phil Nash on 19th December 2014
* Copyright 2014 Two Blue Cubes Ltd. All rights reserved.
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef TWOBLUECUBES_CATCH_REPORTER_TEAMCITY_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_REPORTER_TEAMCITY_HPP_INCLUDED
#include <catch2/catch_config.hpp>
#include <catch2/catch_text.h>
#include <catch2/catch_timer.h>
#include <catch2/reporters/catch_reporter_bases.hpp>
@ -29,175 +24,37 @@ namespace Catch {
m_reporterPrefs.shouldRedirectStdOut = true;
}
static std::string escape( std::string const& str ) {
std::string escaped = str;
replaceInPlace( escaped, "|", "||" );
replaceInPlace( escaped, "'", "|'" );
replaceInPlace( escaped, "\n", "|n" );
replaceInPlace( escaped, "\r", "|r" );
replaceInPlace( escaped, "[", "|[" );
replaceInPlace( escaped, "]", "|]" );
return escaped;
}
~TeamCityReporter() override;
static std::string getDescription() {
return "Reports test results as TeamCity service messages";
using namespace std::string_literals;
return "Reports test results as TeamCity service messages"s;
}
void skipTest( TestCaseInfo const& /* testInfo */ ) override {}
void noMatchingTestCases( std::string const& /* spec */ ) override {}
void testGroupStarting( GroupInfo const& groupInfo ) override {
StreamingReporterBase::testGroupStarting( groupInfo );
stream << "##teamcity[testSuiteStarted name='"
<< escape( groupInfo.name ) << "']\n";
}
void testGroupEnded( TestGroupStats const& testGroupStats ) override {
StreamingReporterBase::testGroupEnded( testGroupStats );
stream << "##teamcity[testSuiteFinished name='"
<< escape( testGroupStats.groupInfo.name ) << "']\n";
}
void testGroupStarting(GroupInfo const& groupInfo) override;
void testGroupEnded(TestGroupStats const& testGroupStats) override;
void assertionStarting( AssertionInfo const& ) override {}
void assertionStarting(AssertionInfo const&) override {}
bool assertionEnded( AssertionStats const& assertionStats ) override {
AssertionResult const& result = assertionStats.assertionResult;
if( !result.isOk() ) {
bool assertionEnded(AssertionStats const& assertionStats) override;
ReusableStringStream msg;
if( !m_headerPrintedForThisSection )
printSectionHeader( msg.get() );
m_headerPrintedForThisSection = true;
msg << result.getSourceInfo() << "\n";
switch( result.getResultType() ) {
case ResultWas::ExpressionFailed:
msg << "expression failed";
break;
case ResultWas::ThrewException:
msg << "unexpected exception";
break;
case ResultWas::FatalErrorCondition:
msg << "fatal error condition";
break;
case ResultWas::DidntThrowException:
msg << "no exception was thrown where one was expected";
break;
case ResultWas::ExplicitFailure:
msg << "explicit failure";
break;
// We shouldn't get here because of the isOk() test
case ResultWas::Ok:
case ResultWas::Info:
case ResultWas::Warning:
CATCH_ERROR( "Internal error in TeamCity reporter" );
// These cases are here to prevent compiler warnings
case ResultWas::Unknown:
case ResultWas::FailureBit:
case ResultWas::Exception:
CATCH_ERROR( "Not implemented" );
}
if( assertionStats.infoMessages.size() == 1 )
msg << " with message:";
if( assertionStats.infoMessages.size() > 1 )
msg << " with messages:";
for( auto const& messageInfo : assertionStats.infoMessages )
msg << "\n \"" << messageInfo.message << "\"";
if( result.hasExpression() ) {
msg <<
"\n " << result.getExpressionInMacro() << "\n"
"with expansion:\n" <<
" " << result.getExpandedExpression() << "\n";
}
if( currentTestCaseInfo->okToFail() ) {
msg << "- failure ignore as test marked as 'ok to fail'\n";
stream << "##teamcity[testIgnored"
<< " name='" << escape( currentTestCaseInfo->name )<< "'"
<< " message='" << escape( msg.str() ) << "'"
<< "]\n";
}
else {
stream << "##teamcity[testFailed"
<< " name='" << escape( currentTestCaseInfo->name )<< "'"
<< " message='" << escape( msg.str() ) << "'"
<< "]\n";
}
}
stream.flush();
return true;
}
void sectionStarting( SectionInfo const& sectionInfo ) override {
void sectionStarting(SectionInfo const& sectionInfo) override {
m_headerPrintedForThisSection = false;
StreamingReporterBase::sectionStarting( sectionInfo );
}
void testCaseStarting( TestCaseInfo const& testInfo ) override {
m_testTimer.start();
StreamingReporterBase::testCaseStarting( testInfo );
stream << "##teamcity[testStarted name='"
<< escape( testInfo.name ) << "']\n";
stream.flush();
}
void testCaseStarting(TestCaseInfo const& testInfo) override;
void testCaseEnded( TestCaseStats const& testCaseStats ) override {
StreamingReporterBase::testCaseEnded( testCaseStats );
auto const& testCaseInfo = *testCaseStats.testInfo;
if( !testCaseStats.stdOut.empty() )
stream << "##teamcity[testStdOut name='"
<< escape( testCaseInfo.name )
<< "' out='" << escape( testCaseStats.stdOut ) << "']\n";
if( !testCaseStats.stdErr.empty() )
stream << "##teamcity[testStdErr name='"
<< escape(testCaseInfo.name )
<< "' out='" << escape( testCaseStats.stdErr ) << "']\n";
stream << "##teamcity[testFinished name='"
<< escape(testCaseInfo.name ) << "' duration='"
<< m_testTimer.getElapsedMilliseconds() << "']\n";
stream.flush();
}
void testCaseEnded(TestCaseStats const& testCaseStats) override;
private:
void printSectionHeader( std::ostream& os ) {
assert( !m_sectionStack.empty() );
void printSectionHeader(std::ostream& os);
if( m_sectionStack.size() > 1 ) {
os << getLineOfChars<'-'>() << "\n";
std::vector<SectionInfo>::const_iterator
it = m_sectionStack.begin()+1, // Skip first section (test case)
itEnd = m_sectionStack.end();
for( ; it != itEnd; ++it )
printHeaderString( os, it->name );
os << getLineOfChars<'-'>() << "\n";
}
SourceLineInfo lineInfo = m_sectionStack.front().lineInfo;
os << lineInfo << "\n";
os << getLineOfChars<'.'>() << "\n\n";
}
// if string has a : in first line will set indent to follow it on
// subsequent lines
static void printHeaderString( std::ostream& os, std::string const& _string, std::size_t indent = 0 ) {
std::size_t i = _string.find( ": " );
if( i != std::string::npos )
i+=2;
else
i = 0;
os << Column( _string )
.indent( indent+i)
.initialIndent( indent ) << "\n";
}
private:
bool m_headerPrintedForThisSection = false;
Timer m_testTimer;