testlib: Reduce JUnit test duration reporting to millisecond precision

The original Ant JUnit reporter produced test durations via Double.toString(),
supporting arbitrary precisions, and the de-facto schema declared them
as xs:decimal.

Sadly, the now popular Maven Surefire reporter limited the duration to
millisecond precision, and hard-coded this into its schema as SUREFIRE_TIME:

  https://issues.apache.org/jira/browse/SUREFIRE-1533

Unfortunately this definition spread into tools such as the Jenkins xUnit
plugin, which relies on the schema provided by Maven Surefire:

  https://issues.jenkins.io/browse/JENKINS-52152

As a result, anything that produces higher precision results will not
validate in the Jenkins xUnit plugin.

Other test frameworks have bitten the bullet and reduced their precision
correspondingly, e.g.:

 https://github.com/catchorg/Catch2/issues/2221
 https://github.com/catchorg/Catch2/commit/581c46249acf8389e9

We follow suit, and our JUnit XML output now validates against both
the Jenkins JUnit and xUnit plugins, as well as the original Apache
Ant de-facto schema.

Pick-to: 6.2
Task-number: QTBUG-95424
Change-Id: I3097d10c03c2a29709960372301b29055d224e10
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
bb10
Tor Arne Vestbø 2021-07-29 15:33:20 +02:00
parent 973e74399e
commit bb74e72aa9
1 changed files with 7 additions and 2 deletions

View File

@ -77,6 +77,11 @@ static qreal elapsedTestCaseSeconds()
return elapsedTestcaseTime.nsecsElapsed() / 1e9;
}
static QByteArray toSecondsFormat(qreal ms)
{
return QByteArray::number(ms / 1000, 'f', 3);
}
void QJUnitTestLogger::startLogging()
{
QAbstractTestLogger::startLogging();
@ -135,7 +140,7 @@ void QJUnitTestLogger::stopLogging()
currentTestSuite->addAttribute(QTest::AI_Errors, buf);
currentTestSuite->addAttribute(QTest::AI_Time,
QByteArray::number(QTestLog::msecsTotalTime() / 1000, 'f').constData());
toSecondsFormat(QTestLog::msecsTotalTime()).constData());
currentTestSuite->addLogElement(listOfTestcases);
@ -205,7 +210,7 @@ void QJUnitTestLogger::leaveTestFunction()
void QJUnitTestLogger::leaveTestCase()
{
currentLogElement->addAttribute(QTest::AI_Time,
QByteArray::number(elapsedTestCaseSeconds(), 'f').constData());
toSecondsFormat(elapsedTestCaseSeconds()).constData());
}
void QJUnitTestLogger::addIncident(IncidentTypes type, const char *description,