[英]Is it possible for Ant to lists failed tests at the end of a build?
來自另一個線程的以下代碼段用於打印消息,並在運行所有單元測試后失敗:
<fail if="junit.failed" message="Oh no ! There were some failed unit tests :( "/>
但是---我沒有看到如何在junit / ant中記錄並打印失敗測試的NAMES,之后它們全部運行。 有什么想法嗎?
我相信其他人會發現這樣的功能非常重要,所以我假設存在一個簡單的解決方案:通過數百次失敗的測試來查看違規者是相當繁瑣的。
是的。 嘗試使用junitreport任務。
例如
在junit任務上嘗試此屬性:
在junit任務上printsummary="yes"
將格式化程序更改為:
<formatter type="xml" />
然后使用調用此目標的目標創建報告:
<junitreport>
<fileset dir="${testReport.dir}/tmp">
<include name="*.xml" />
</fileset>
<report format="frames" styledir="${testReportXslt.dir}" todir="${finalReport.dir}/html" />
</junitreport>
輸出:
<concat>
<fileset dir="${finalReport.dir}/html" includes="*.html"/>
<filterchain>
<linecontainsregexp>
<regexp pattern='some pattern' />
</linecontainsregexp>
</filterchain>
</concat>
我相信你們許多人都不想建立自定義格式化程序或其他任何東西。 我也沒有。 我發現,使用以下配置,測試成功將打印到stdout,同時將故障打印到stderr:
<target name="test"
depends="compile-tests"
description="runs the unit tests">
<junit failureproperty="hasFailingTests"
printsummary="on"
showoutput="true">
<formatter type="plain" usefile="false" />
<batchtest>
<fileset dir="${test.dir}">
<include name="**/*Test.java" />
<exclude name="**/Abstract*Test.java" />
</fileset>
</batchtest>
<classpath refid="tests.classpath"></classpath>
</junit>
<fail if="hasFailingTests" />
</target>
這意味着運行以下命令:
ant test > /dev/null
只會向控制台顯示測試失敗的stderr,這樣可以更容易地看到實際失敗的內容。
我想我終於對這個問題有了完整的答案:感謝FailedDev的見解。
首先,確保$ {reports.dir}變量,指定報告的目錄:
<property name="reports.dir" value="reports" />
然后,當我們開始編寫測試指令junit時:
<target name="test" depends="compile">
然后,為報告創建必要的目錄:
<mkdir dir="${reports.dir}" />
<mkdir dir="${reports.dir}/tmp" />
<mkdir dir="${reports.dir}/style" />
<mkdir dir="${reports.dir}/final" />
由於我們有一個報告掃描程序,我們可以將haltonfailure設置為no,並在(向下滾動)之后失敗。
<junit printsummary="yes" failureproperty="junit.failed" haltonfailure="no" fork="yes" forkmode="once">
<jvmarg value="-Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl" />
<classpath refid="common.classpath" />
<classpath>
<pathelement location="${build.dir}" />
<pathelement location="${src.dir}" />
</classpath>
<formatter type="xml" />
<batchtest todir="${reports.dir}">
<fileset dir="${test.dir}">
<include name="**/Test*.java" />
<exclude name="**/AllTests.java" />
<exclude name="**/*.properties" />
<exclude name="**/*.xml" />
</fileset>
</batchtest>
</junit>
現在,這里是其他問題的建議:運行junit報告。
<!-- Capture all failures, simple debugging statements. -->
<junitreport>
<fileset dir="${reports.dir}/tmp">
<include name="*.xml" />
</fileset>
<report todir="${reports.dir}/final" />
</junitreport>
最后,我們可以直接grep xml文件以查找錯誤:
<!-- This could be its own task, i.e., a java class which directly processed junit test data. -->
<echo message="Now checking xml test results for errors" />
<exec executable="grep" error="/dev/null">
<arg value="-r" />
<arg value="-m" />
<arg value="1" />
<arg value="-rl" />
<arg value="errors=\"[1-9]\"" />
<arg value="${reports.dir}" />
</exec>
現在,既然我們沒有提前失敗(而是我們正在運行整個構建,所以我們可以看到哪些測試失敗了,如果有的話)我們仍然必須通知構建器我們已經失敗...這是通過失敗完成的 - 如果語法:
<fail if="junit.failed" message="FAILING - unit tests failed." />
<!-- Now, we check if there were failures, and output the results -->
</target>
刪除我的注釋,如果將它粘貼到您的構建中,此代碼塊應該可以正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.