24

I'm using CTest (part of CMake) for my automated tests.

How do I get CTest results in the Jenkins dashboard ? Or, phrased differently, how do I get CTest to output in JUnit-like XML ?

Calvin1602
  • 9,413
  • 2
  • 44
  • 55

2 Answers2

20

In Jenkins, after the CMake part (probably made through the CMake plugin), add the following batch script, or adapt for builds on Linux :

del build_32\JUnitTestResults.xml
pushd build_32\Tests
"C:\Program Files\CMake 2.8\bin\ctest.exe" -T Test -C RelWithDebInfo --output-on-failure
popd
verify >nul
C:\Python27\python.exe external/tool/CTest2JUnit.py build_32/Tests external/tool/CTest2JUnit.xsl > build_32/JUnitTestResults.xml
  • build_32 is the Build Directory in the CMake plugin
  • Tests is the subdirectory where all my tests live
  • -T Test makes CTest output in XML (?!)
  • verify >nul resets errorlevel to 0, because CTest returns >0 if any test fails, which Jenkins interprets as "the whole build failed", which we don't want
  • The last line converts CTest's XML into a minimal JUnit xml. The Python script and the xslt live in the source directory, you may want to change that.

The python script looks like this (hacked together in 10 min, beware) :

from lxml import etree
import StringIO
import sys

TAGfile = open(sys.argv[1]+"/Testing/TAG", 'r')
dirname = TAGfile.readline().strip()

xmlfile = open(sys.argv[1]+"/Testing/"+dirname+"/Test.xml", 'r')
xslfile = open(sys.argv[2], 'r')

xmlcontent = xmlfile.read()
xslcontent = xslfile.read()

xmldoc = etree.parse(StringIO.StringIO(xmlcontent))
xslt_root = etree.XML(xslcontent)
transform = etree.XSLT(xslt_root)

result_tree = transform(xmldoc)
print(result_tree)
  • It needs lxml, direct link
  • It takes two arguments, the directory in which the tests live (in the build directory), and a xsl file
  • It simply reads the last xml tests results, transforms it with the xsl, and outputs it to stdout
  • The "last xml tests" are present in the first line of the Testing/TAG file, hence the additional fopen

The xsl looks like this. It's pretty minimal but gets the job done : [EDIT] see MOnsDaR 's improved version : http://pastebin.com/3mQ2ZQfa

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>

    <xsl:template match="/Site/Testing">    
        <testsuite>
            <xsl:apply-templates select="Test"/>
        </testsuite>
    </xsl:template>

    <xsl:template match="Test">
        <xsl:variable name="testcasename"><xsl:value-of select= "Name"/></xsl:variable>
        <xsl:variable name="testcaseclassname"><xsl:value-of select= "FullName"/></xsl:variable>
        <testcase name="{$testcasename}" classname="{$testcaseclassname}">
            <xsl:if test="@Status = 'passed'">
            </xsl:if>
            <xsl:if test="@Status = 'failed'">
                <error type="error"><xsl:value-of select="Results/Measurement/Value/text()" /></error>
            </xsl:if>
            <xsl:if test="@Status = 'notrun'">
                <skipped><xsl:value-of select="Results/Measurement/Value/text()" /></skipped>
            </xsl:if>
        </testcase>
    </xsl:template>

</xsl:stylesheet>

Finally, check "Publish JUnit tests results" (or similar, my version is in French) and set the xml path to build_32/JUnitTestResults.xml

Well, that was ugly. But still, hope this helps someone. And improvements are welcome ( running ctest from python maybe ? Using the path of the Python plugin instead of C:... ? )

Calvin1602
  • 9,413
  • 2
  • 44
  • 55
  • Thanks a lot for this solution. I'm currently playing around with it and it basically works perfect for me. – MOnsDaR Jul 01 '11 at 22:50
  • 6
    I enhanced your XSL-Code to support more features of the CTest XML-Output: http://pastebin.com/GAj7Fzqd It now adds the execution time of the tests and in case of an error it adds the errortype and errorcode as a message. Please comment if there is some other thing which could be added to the generated JUnit-File. – MOnsDaR Jul 02 '11 at 10:46
  • 1
    The new version supports the -Tags of JUnit and additionally stores the properties as system-out (so they could be seen in Jenkins): http://pastebin.com/3mQ2ZQfa – MOnsDaR Jul 02 '11 at 12:13
  • @MOnsDaR : Thanks for the improvements ! Awesome. I'll test it this week when I find some time, and I'll update my answer with your code. – Calvin1602 Jul 04 '11 at 13:25
  • Updated answer ; I let the link to pastebin, the xsl is a bit too big. Thanks again, works well. – Calvin1602 Aug 04 '11 at 14:08
  • 4
    on Linux you can use xsltproc + shell to achieve the same results: copy XSL file to some location, and then use following command: xsltproc ctest2junix.xsl Testing/`head -n 1 < Testing/TAG`/Test.xml > JUnitTestResults.xml `head -n ...` command should be enclosed in backquotes... – Alex Ott Mar 20 '12 at 08:47
  • 2
    I further improved xslt file from @MOnsDaR xslt as: [CTest2JUnit.xsl](https://github.com/zanata/zanata-tests/blob/master/scripts/CTest2JUnit.xsl) It fills testclassname as 'this/Path', tests and failures. – Ding-Yi Chen Jun 22 '12 at 04:39
  • I wrote a improved version of that script which is located [here](https://bitbucket.org/shackra/ctest-jenkins/downloads) – shackra May 15 '14 at 22:02
0

This seems to be integrated in jenkins-ci nowadays:

malat
  • 12,152
  • 13
  • 89
  • 158