0
点赞
收藏
分享

微信扫一扫

CppUnit框架下XML结果转化为JUnit格式

陬者 2022-02-11 阅读 56

最近开发过程中遇到需要将CppUnit框架下的单元测试XML结果转化为JUnit格式,查阅资料后找到解决方法,记录如下。

参考资料:

  1. CppUnit疑似官方网站 CppUnit - C++ port of JUnit download | SourceForge.net
  2. CppUnit示例 GitHub - skazik/cppunit
  3. XML处理器-xsltproc zlatkovic.com - Libxml
  4. 一些讨论 #58 (Provide CppUnit output parsing, perhaps as xUnit XML) – Bitten
  5. 一些讨论2 What XSLT converts JUnit Xml format to JUnit Plain format - Stack Overflow

前期准备

根据网站[1]中CppUnit发布说明,在1.11.0中增加了可以将XML输出转换为JUnit格式的功能。

通过上述关键词的搜索,找到了关于这个xslt的信息,最终在GitHub中找到了关于这个xslt的文件以及用法。

cpp2junit.xslt

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
	<testsuite>
	    <xsl:attribute name="name">cppunit-name</xsl:attribute>
	    <xsl:attribute name="errors"><xsl:value-of select="TestRun/Statistics/Errors"/></xsl:attribute>
	    <xsl:attribute name="tests"><xsl:value-of select="TestRun/Statistics/Tests"/></xsl:attribute>
	    <xsl:attribute name="failures"><xsl:value-of select="TestRun/Statistics/Failures"/></xsl:attribute>
	    <xsl:attribute name="time"><xsl:value-of select="TestRun/Statistics/Time"/></xsl:attribute>
	    <xsl:attribute name="timestamp"><xsl:value-of select="TestRun/Statistics/Timestamp"/></xsl:attribute>
	    <xsl:apply-templates/>
	</testsuite>
    </xsl:template>
    <xsl:template match="/TestRun/SuccessfulTests/Test">
	<testcase>
	    <xsl:attribute name="classname" ><xsl:value-of select="substring-before(Name, '::')"/></xsl:attribute>
	    <xsl:attribute name="name"><xsl:value-of select="substring-after(Name, '::')"/></xsl:attribute>
	    <xsl:attribute name="time"><xsl:value-of select="Time"/></xsl:attribute>
	</testcase>
    </xsl:template>
    <xsl:template match="/TestRun/FailedTests/FailedTest">
	<testcase>
	    <xsl:attribute name="classname" ><xsl:value-of select="substring-before(Name, '::')"/></xsl:attribute>
	    <xsl:attribute name="name"><xsl:value-of select="substring-after(Name, '::')"/></xsl:attribute>
	    <xsl:attribute name="time"><xsl:value-of select="Time"/></xsl:attribute>
	    <error>
		<xsl:attribute name="message">
		    <xsl:value-of select=" normalize-space(Message)"/>
		</xsl:attribute>
		<xsl:attribute name="type">
		    <xsl:value-of select="FailureType"/>
		</xsl:attribute>
		<xsl:value-of select="Message"/>
		File:<xsl:value-of select="Location/File"/>
		Line:<xsl:value-of select="Location/Line"/>
	    </error>
	</testcase>
    </xsl:template>
    <xsl:template match="text()|@*"/>
</xsl:stylesheet>

cpp2junit.sh

xsltproc -o junitResults.xml cpp2junit.xslt cppunitResults.xml

xsltproc

下载路径:https://www.zlatkovic.com/pub/libxml/libxmlsec-nounicode-1.2.18.win32.zip

调用过程

所需的dll库大致如下,再将cpp2junit.xslt也放入同路径, 通过上述命令行的调用就可以将CppUnit格式的XML转化为JUnit格式。

CppUnitResults.xml

<?xml version="1.0" encoding='utf-8' ?>
<TestRun>
  <FailedTests></FailedTests>
  <SuccessfulTests>
    <Test id="1">
      <Name>HelloTest::firstTest</Name>
    </Test>
  </SuccessfulTests>
  <Statistics>
    <Tests>1</Tests>
    <FailuresTotal>0</FailuresTotal>
    <Errors>0</Errors>
    <Failures>0</Failures>
  </Statistics>
</TestRun>

junitResults.xml

<?xml version="1.0"?>
<testsuite name="cppunit-name" errors="0" tests="1" failures="0" time="" timestamp="">
  <testcase classname="HelloTest" name="firstTest" time=""/>
</testsuite>

至此,转化完成。

举报

相关推荐

0 条评论