[sysadmin/ci-utilities] /: Let ctest output junit results directly
David Redondo <[email protected]>
| Newsgroups | gmane.comp.kde.cvs |
|---|---|
| Message-ID | <[email protected]> |
Git commit 6929faea092e09701a2617486b7b7eab9546d8e2 by David Redondo.
Committed on 01/08/2026 at 11:12.
Pushed by bshah into branch 'master'.
Let ctest output junit results directly
Since version 3.21 ctest can output junit results directly. Frameworks currently requires 3.29 so all platforms should have it.
M +4 -32 components/TestHandler.py
D +0 -102 resources/ctesttojunit.xsl
https://invent.kde.org/sysadmin/ci-utilities/-/commit/6929faea092e09701a2617486b7b7eab9546d8e2
diff --git a/components/TestHandler.py b/components/TestHandler.py
index 715dddf..4b7e30d 100644
--- a/components/TestHandler.py
+++ b/components/TestHandler.py
@@ -8,7 +8,6 @@ import hashlib
import subprocess
import multiprocessing
from typing import Dict, MutableMapping, List
-from lxml import etree
from components import CommonUtils
def run( projectConfig: Dict[str, Dict[str, bool]], sourcesPath: str, buildPath: str, installPath: str, buildEnvironment: MutableMapping[str, str] ) -> bool:
@@ -165,9 +164,12 @@ def run( projectConfig: Dict[str, Dict[str, bool]], sourcesPath: str, buildPath:
if projectConfig['Options']['tests-run-in-parallel']:
cpuCount = int(multiprocessing.cpu_count())
+ junitFilename = os.path.join( sourcesPath, 'JUnitTestResults.xml' )
+
# Now it's time to invoke CTest! Build up the command...
- commandToRun = "ctest -T Test --output-on-failure --no-compress-output --test-output-size-passed 1048576 --test-output-size-failed 1048576 -j {cpuCount} --timeout {timeLimit} {additionalCTestArguments}"
+ commandToRun = "ctest -T Test --output-junit {junitFilename} --output-on-failure --no-compress-output --test-output-size-passed 1048576 --test-output-size-failed 1048576 -j {cpuCount} --timeout {timeLimit} {additionalCTestArguments}"
commandToRun = commandToRun.format(
+ junitFilename=junitFilename,
cpuCount=cpuCount,
timeLimit=projectConfig['Options']['per-test-timeout'],
additionalCTestArguments=projectConfig['Options']['ctest-arguments']
@@ -178,12 +180,6 @@ def run( projectConfig: Dict[str, Dict[str, bool]], sourcesPath: str, buildPath:
ctestProcess = subprocess.Popen( commandToRun, stdout=sys.stdout, stderr=sys.stderr, shell=True, cwd=buildPath, env=buildEnvironment )
ctestProcess.wait()
- # Now that CTest is done, we convert it's output to JUnit format
- junitOutput = convertCTestResultsToJUnit( buildPath )
- junitFilename = os.path.join( sourcesPath, 'JUnitTestResults.xml' )
- with open(junitFilename, 'w', encoding='UTF-8') as junitFile:
- junitFile.write( str(junitOutput) )
-
# To ensure we don't hang, cleanup the Window Manager and X server if needed
if projectConfig['Options']['setup-x-environment'] and ( sys.platform != 'win32' and sys.platform != 'darwin' ):
wmProcess.terminate()
@@ -202,30 +198,6 @@ def run( projectConfig: Dict[str, Dict[str, bool]], sourcesPath: str, buildPath:
# All done!
return ctestProcess.returncode == 0
-def convertCTestResultsToJUnit( buildDirectory: str ) -> etree._XSLTResultTree:
- # Where is the base prefix for all test data for this project located?
- testDataDirectory = os.path.join( buildDirectory, 'Testing' )
-
- # Determine where we will find the test run data for the latest run
- filename = os.path.join( testDataDirectory, 'TAG' )
- with open(filename, 'r') as tagFile:
- testDirectoryName = tagFile.readline().strip()
-
- # Open the test result XML and load it
- filename = os.path.join( testDataDirectory, testDirectoryName, 'Test.xml' )
- with open(filename , 'r', encoding='UTF-8') as xmlFile:
- xmlDocument = etree.parse( xmlFile )
-
- # Load the XSLT file
- filename = os.path.join( CommonUtils.scriptsBaseDirectory(), 'resources', 'ctesttojunit.xsl' )
- with open(filename, 'r') as xslFile:
- xslContent = xslFile.read()
- xsltRoot = etree.XML(xslContent)
-
- # Transform the CTest XML into JUnit XML
- transform = etree.XSLT(xsltRoot)
- return transform(xmlDocument)
-
def qmllintToCodeClimate(input_files: List[str], sourcesPath: str) -> None:
warnings = []
diff --git a/resources/ctesttojunit.xsl b/resources/ctesttojunit.xsl
deleted file mode 100644
index a28f039..0000000
--- a/resources/ctesttojunit.xsl
+++ /dev/null
@@ -1,102 +0,0 @@
-<?xml version="1.0"?>
-<!--
-The MIT License (MIT)
-
-Copyright (c) 2014, Gregory Boissinot, Falco Nikolas
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in
-all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-THE SOFTWARE.
--->
-<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xunit="http://www.xunit.org">
- <xsl:output method="xml" indent="yes" cdata-section-elements="system-out system-err failure"/>
- <xsl:decimal-format decimal-separator="." grouping-separator=","/>
-
- <xsl:function name="xunit:junit-time" as="xs:string">
- <xsl:param name="value" as="xs:anyAtomicType?" />
-
- <xsl:variable name="time" as="xs:double">
- <xsl:choose>
- <xsl:when test="$value instance of xs:double">
- <xsl:value-of select="$value" />
- </xsl:when>
- <xsl:otherwise>
- <xsl:value-of select="translate(string(xunit:if-empty($value, 0)), ',', '.')" />
- </xsl:otherwise>
- </xsl:choose>
- </xsl:variable>
- <xsl:value-of select="format-number($time, '0.000')" />
- </xsl:function>
-
- <xsl:function name="xunit:if-empty" as="xs:string">
- <xsl:param name="value" as="xs:anyAtomicType?" />
- <xsl:param name="default" as="xs:anyAtomicType" />
- <xsl:value-of select="if (string($value) != '') then string($value) else $default" />
- </xsl:function>
-
- <xsl:function name="xunit:is-empty" as="xs:boolean">
- <xsl:param name="value" as="xs:string?" />
- <xsl:value-of select="string($value) != ''" />
- </xsl:function>
-
- <xsl:template match="/">
- <testsuites>
- <xsl:variable name="buildName" select="//Site/@BuildName"/>
- <xsl:variable name="buildTime" select="(//Site/Testing/EndTestTime - //Site/Testing/StartTestTime)"/>
- <xsl:variable name="numberOfTests" select="count(//Site/Testing/Test)"/>
- <xsl:variable name="numberOfFailures" select="count(//Site/Testing/Test[@Status='failed'])"/>
- <xsl:variable name="numberOfSkipped" select="count(//Site/Testing/Test[@Status='notrun'])"/>
- <xsl:variable name="numberOfSkippedDueNotFound" select="count(//Site/Testing/Test[@Status='notrun']/Results/NamedMeasurement[@name='Completion Status'][Value='Unable to find executable'])"/>
- <testsuite name="CTest"
- tests="{$numberOfTests}"
- failures="{$numberOfFailures + $numberOfSkippedDueNotFound}"
- errors="0"
- skipped="{$numberOfSkipped - $numberOfSkippedDueNotFound}">
- <xsl:for-each select="//Site/Testing/Test">
- <xsl:variable name="testName" select="translate(Name, '-', '_')"/>
- <xsl:variable name="duration" select="Results/NamedMeasurement[@name='Execution Time']/Value"/>
- <xsl:variable name="status" select="@Status"/>
- <xsl:variable name="completionStatus" select="Results/NamedMeasurement[@name='Completion Status']/Value"/>
- <xsl:variable name="output" select="Results/Measurement/Value"/>
- <xsl:variable name="className" select="translate(Path, '/.', '.')"/>
- <testcase classname="projectroot{$className}"
- name="{$testName}">
- <xsl:choose>
- <xsl:when test="@Status='passed'"/>
- <xsl:when test="@Status='notrun' and $completionStatus='Unable to find executable'">
- <failure>
- <xsl:value-of select="$output"/>
- </failure>
- </xsl:when>
- <xsl:when test="@Status='notrun'">
- <skipped/>
- </xsl:when>
- <xsl:otherwise>
- <failure>
- <xsl:value-of select="$output"/>
- </failure>
- </xsl:otherwise>
- </xsl:choose>
- <system-out>
- <xsl:value-of select="$output"/>
- </system-out>
- </testcase>
- </xsl:for-each>
- </testsuite>
- </testsuites>
- </xsl:template>
-</xsl:stylesheet>