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

summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorMartin Hořeňovský2021-05-22 23:41:58 +0200
committerMartin Hořeňovský2021-10-10 22:10:48 +0200
commitde67278e145bf0d5d4ceb3e2bdcf42fda5b4876d (patch)
tree0f5179887180c0a3f05c4e5cf63c5926faba6501 /src
parent1d9696d22d4a33b106dee1e3f69cb310d855b637 (diff)
downloadcatch3-de67278e145bf0d5d4ceb3e2bdcf42fda5b4876d.tar.gz
JUnit reporter uses only 3 decimal places when reporting durations
We used to use whatever precision we ended up having from C++'s stdlib. However, some relatively popular tools, like Jenkins, use Maven SureFire XML schema to validate JUnit test reports, and Maven SureFire schema requires the duration to have at most 3 decimal places. For compatibility, the JUnit reporter will now respect this limitation. Closes #2221
Diffstat (limited to 'src')
-rw-r--r--src/catch2/reporters/catch_reporter_junit.cpp16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/catch2/reporters/catch_reporter_junit.cpp b/src/catch2/reporters/catch_reporter_junit.cpp
index 8e8da3ca..71a93051 100644
--- a/src/catch2/reporters/catch_reporter_junit.cpp
+++ b/src/catch2/reporters/catch_reporter_junit.cpp
@@ -17,6 +17,7 @@
#include <cassert>
#include <ctime>
#include <algorithm>
+#include <iomanip>
namespace Catch {
@@ -54,6 +55,17 @@ namespace Catch {
}
return std::string();
}
+
+ // Formats the duration in seconds to 3 decimal places.
+ // This is done because some genius defined Maven Surefire schema
+ // in a way that only accepts 3 decimal places, and tools like
+ // Jenkins use that schema for validation JUnit reporter output.
+ std::string formatDuration( double seconds ) {
+ ReusableStringStream rss;
+ rss << std::fixed << std::setprecision( 3 ) << seconds;
+ return rss.str();
+ }
+
} // anonymous namespace
JunitReporter::JunitReporter( ReporterConfig const& _config )
@@ -111,7 +123,7 @@ namespace Catch {
if( m_config->showDurations() == ShowDurations::Never )
xml.writeAttribute( "time"_sr, ""_sr );
else
- xml.writeAttribute( "time"_sr, suiteTime );
+ xml.writeAttribute( "time"_sr, formatDuration( suiteTime ) );
xml.writeAttribute( "timestamp"_sr, getCurrentTimestamp() );
// Write properties
@@ -178,7 +190,7 @@ namespace Catch {
xml.writeAttribute( "classname"_sr, className );
xml.writeAttribute( "name"_sr, name );
}
- xml.writeAttribute( "time"_sr, ::Catch::Detail::stringify( sectionNode.stats.durationInSeconds ) );
+ xml.writeAttribute( "time"_sr, formatDuration( sectionNode.stats.durationInSeconds ) );
// This is not ideal, but it should be enough to mimic gtest's
// junit output.
// Ideally the JUnit reporter would also handle `skipTest`