2012-02-27 69 views
2

我正在将我的构建从Ant移动到Gradle。 Ant允许JUnit任务创建具有不同格式的多个报告。 Gradle更具限制性 - 它生成一个HTML报告和一个XML报告。 XML报告是JUnit文本报告的超集,因此它可以从一个转换到另一个。 XSLT会将XML转换为文本?下面是一个例子XML:XSLT将JUnit Xml格式转换为JUnit Plain格式

<?xml version="1.0" encoding="UTF-8"?> 
<testsuite errors="0" failures="0" hostname="spina.stsci.edu" name="edu.stsci.CoSI.test.DependencySupressingConstraintJUnitTest" tests="6" time="0.14" timestamp="2012-02-27T18:08:03"> 
    <properties /> 
    <testcase classname="edu.stsci.CoSI.test.DependencySupressingConstraintJUnitTest" name="testSupressionWorks" time="0.01" /> 
    <testcase classname="edu.stsci.CoSI.test.DependencySupressingConstraintJUnitTest" name="testSupressionWorksWithDependenciesDisabled" time="0.0020" /> 
    <testcase classname="edu.stsci.CoSI.test.DependencySupressingConstraintJUnitTest" name="testPropagationIsDeferred" time="0.0010" /> 
    <testcase classname="edu.stsci.CoSI.test.DependencySupressingConstraintJUnitTest" name="testPropagationIsDeferredWhenDependenciesAreSuppressed" time="0.0010" /> 
    <testcase classname="edu.stsci.CoSI.test.DependencySupressingConstraintJUnitTest" name="testPropagationIsDeferredWhenDependenciesAreSuppressed2" time="0.0010" /> 
    <testcase classname="edu.stsci.CoSI.test.DependencySupressingConstraintJUnitTest" name="testGarbageCollection" time="0.066" /> 
    <system-out><![CDATA[Running Supressing Constraint 
    Running Supressing Constraint 
    Running Supressing Constraint 
    Change Me is: 2 
    Running Supressing Constraint 
    Change Me is: 5 
    ]]></system-out> 
    <system-err><![CDATA[]]></system-err> 
</testsuite> 

这里是我想它产生于文:

Testsuite: edu.stsci.CoSI.test.DependencySupressingConstraintJUnitTest 
Tests run: 6, Failures: 0, Errors: 0, Time elapsed: 0.363 sec 
------------- Standard Output --------------- 
Running Supressing Constraint 
Running Supressing Constraint 
Running Supressing Constraint 
Change Me is: 2 
Running Supressing Constraint 
Change Me is: 5 
------------- ---------------- --------------- 

Testcase: testSupressionWorks took 0.001 sec 
Testcase: testSupressionWorksWithDependenciesDisabled took 0.001 sec 
Testcase: testPropagationIsDeferred took 0 sec 
Testcase: testPropagationIsDeferredWhenDependenciesAreSuppressed took 0.001 sec 
Testcase: testPropagationIsDeferredWhenDependenciesAreSuppressed2 took 0 sec 
Testcase: testGarbageCollection took 0.038 sec 

某些细节并不重要(如秒的格式)。

+0

那么,这是什么问题?我没看到一个。 – 2012-02-27 18:33:48

+0

@DimitreNovatchev,谢谢你指出我的遗漏。我在这里从这个问题上领先:http://stackoverflow.com/questions/3671613/junit-report-single-page-xslt-for-email。如果这真的是一个不恰当的问题,我很乐意收回它。我认为这可能是一般用途,因为它与从一个构建系统迁移到另一个构建系统有关。 – Spina 2012-02-27 19:30:34

+0

脊柱,这不是不恰当的 - 看起来你希望有人为你做整个工作* - 并不是说​​你已经尝试过自己,写了一个改造,并且遇到了问题。这里的人们愿意*帮助*做这项工作,而不是做整个工作。更重要的是你学到了一些东西。当人们为你完成整个工作时,你学习/理解的机会是微乎其微的。 – 2012-02-27 20:41:27

回答

7

在这里你走了。

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

    <xsl:template match="/testsuite"> 
    Testsuite: <xsl:value-of select="@name" /> 
    <xsl:text> 
    Tests run: </xsl:text> 
    <xsl:value-of select="@tests" /> 
    <xsl:text>, Failures: </xsl:text> 
    <xsl:value-of select="@failures" /> 
    <xsl:text>, Errors: </xsl:text> 
    <xsl:value-of select="@errors" /> 
    <xsl:text>, Time elapsed: </xsl:text> 
    <xsl:value-of select="@time" /> 
    <xsl:text> sec</xsl:text> 
    <xsl:apply-templates select="system-out" /> 
    <xsl:apply-templates select="system-err" /> 
    <xsl:text> 
    --------- ----------- --------- 
    </xsl:text> 
    <xsl:apply-templates select="testcase" /> 
    </xsl:template> 

    <xsl:template match="testcase"> 
    <xsl:text> 
    Testcase: </xsl:text> 
    <xsl:value-of select="@name" /> 
    <xsl:text> took </xsl:text> 
    <xsl:value-of select="@time" /> 
    </xsl:template> 

    <xsl:template match="system-out"> 
    <xsl:text> 
    ------ Standard output ------ 
    </xsl:text> 
    <xsl:value-of select="." /> 
    </xsl:template> 

    <xsl:template match="system-err"> 
    <xsl:text> 
    ------ Error output ------ 
    </xsl:text> 
    <xsl:value-of select="." /> 
    </xsl:template> 

</xsl:stylesheet> 

虽然你可能想玩格式化。

+0

Thanks @Mr Happy。这正是我想要的(通过一些调整)。 – Spina 2012-04-06 14:19:44

+0

谢谢。我用这个在Gradle构建中输出测试错误为纯文本到Travis CI控制台:https://github.com/lhotari/travis-gradle-test-failures-to-console/blob/master/travis/junit- XML的格式errors.xsl – 2014-11-29 08:54:24