簡體   English   中英

如何將JUnit Ant任務配置為僅在失敗時生成輸出?

[英]How do I configure JUnit Ant task to only produce output on failures?

我在Ant中配置JUnit,以便在每個構建上運行單元測試。 我希望失敗測試的輸出在運行時打印在Ant控制台輸出中。 我不需要看到后續測試的任何輸出。

這是我的build.xml文件的相關位:

<junit>
    <classpath>
        <pathelement path="${build}"/>
    </classpath>
    <formatter type="brief" usefile="false"/>
    <batchtest>
        <fileset dir="${src}" includes="my/tree/junit/"/>
    </batchtest>
</junit>

這幾乎產生了我想要的東西,Ant輸出中詳細說明了失敗的測試,除了成功的測試還寫了以下輸出:

[junit] Testsuite: my.tree.junit.ExampleTest
    [junit] Tests run: 7, Failures: 0, Errors: 0, Time elapsed: 0.002 sec

我相信我已經嘗試了JUnit任務文檔中列出的所有組合,包括:

  • printsummary屬性
  • showoutput屬性
  • formatter與每個種元素type

我的用例是從命令行運行ant 當我編寫更多測試時,我不希望后續測試的輸出如此之大,以至於失敗測試的輸出會滾出屏幕。 我只想讓ant保持安靜,除非有一個需要我注意的失敗測試。 如何配置Ant / JUnit來執行此操作?

我使用的是Ant 1.6.4和JUnit 4.6。

一種可能性是使用' classname '屬性定義自己的xml格式化程序(並擴展org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter ,可能在endTest()endTestsuite()方法endTest() )。
該格式化程序將忽略信息消息並僅顯示失敗消息。


注意: 此設置提到僅顯示失敗測試的可能性:

<junit showoutput="true"
       fork="true"
       failureproperty="tests.failed"
       errorproperty="tests.failed">
    <batchtest todir="${test.results.dir}">
        <fileset dir="test">
            <include name="**/*Test.java"/>
        </fileset>
    </batchtest>
    <classpath path="${classes.dir}:${junit.jar}:${test.classes.dir}"/>
    <formatter usefile="false" type="brief"/>
    <!-- <formatter type="xml"/> If missing, only displays failed tests -->
</junit>

你測試過了嗎?

注意:“ showoutput="true"<formatter type="brief" usefile="false"/> ”可能有點問題,如最近的(2012年2月)票證所示


另一個方法是定義你的ant Juint Test runner,支持ant JUnitResultFormatter並只顯示stderr消息。

來自eclipse的EclipseTestRunner就是一個很好的例子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM