如何在Hudson / Jenkins获得CTest结果 [英] How to get CTest results in Hudson / Jenkins

查看:210
本文介绍了如何在Hudson / Jenkins获得CTest结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CTest(CMake的一部分)进行自动测试。



如何在Jenkins仪表板中获取CTest结果?

解决方案

在Jenkins中,在CMake部分之后(可能是通过CMake插件创建的),添加以下批处理脚本或适应Linux上的构建:

  del build_32 \\ JUnitTestResults.xml 
pushd build_32\Tests
C:\Program Files\CMake 2.8\bin\ctest.exe-T Test -C RelWithDebInfo - 输出失败
popd
verify> nul
C:\Python27\python.exe external / tool / CTest2JUnit.py build_32 / Tests external / tool / CTest2JUnit.xsl> build_32 / JUnitTestResults.xml




  • code>是CMake插件中的Build目录

  • 测试
  • -T Test 使用XML(?!)输出CTest

  • > nul 将errorlevel重置为0,因为如果任何测试失败,CTest返回> 0,Jenkins将其解释为整个构建失败,我们不想这样。

  • 最后一行将CTest的XML转换为最小 JUnit xml。



python脚本看起来像这样(hacked一起在10分钟,谨防):

 从lxml import etree 
import StringIO
import sys

TAGfile = open(sys.argv [1] +/ Testing / TAG,'r')
dirname = TAGfile.readline()。strip()
$ b b 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)




  • 需要lxml,直接链接

  • 它有两个参数,测试目录live(在build目录中)和一个xsl文件

  • 只需读取最后一个xml测试结果,用xsl转换它,并将其输出到stdout

  • 最后一个xml测试存在于 Testing / TAG 文件的第一行,因此附加的fopen



    • xsl看起来像这样。这是很少,但完成工作:看到MOnsDaR的改进版本: http://pastebin.com/3mQ2ZQfa p>

       <?xml version =1.0encoding =UTF-8?& 
      < xsl:stylesheet xmlns:xsl =http://www.w3.org/1999/XSL/Transformversion =1.0>
      < xsl:output method =xmlindent =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>最后,选中发布JUnit测试结果(或类似的,我的版本是法语)并设置为发布JUnit测试结果 build_32 / JUnitTestResults.xml的



      好吧,这很丑陋。但仍然,希望这有助于某人。并且改进是受欢迎的(从python运行ctest也许?使用Python插件的路径,而不是C:...?)


      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 ?

      解决方案

      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:... ? )

      这篇关于如何在Hudson / Jenkins获得CTest结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆