2012-02-27 70 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