2014-09-06 37 views
0

螞蟻打印JUnit的報告,下面是我的build.xml如何強制色彩

<target name="tdd" description="Run unittest" depends="jar"> 
    <mkdir dir="${build.test.dir}"/> 
    <javac srcdir="${test.src.dir}" destdir="${build.test.dir}" 
     includeAntRuntime="false"> 
     <classpath refid="lib.junit"/> 
     <classpath refid="lib.dataquery"/> 
    </javac> 
    <jar destfile="${build.jar.dir}/dataqueryTDD.jar"> 
     <fileset dir="${build.test.dir}"> 
      <include name="**/*.class"/> 
     </fileset> 
    </jar> 
    <mkdir dir="${build.test.report.dir}"/> 

    <junit printsummary="yes"> 
     <classpath refid="lib.junit"/> 
     <classpath refid="lib.dataquery"/> 
     <classpath refid="lib.dataquery.tdd"/> 
     <formatter type="xml"/> 
     <batchtest todir="${build.test.report.dir}"> 
      <fileset dir="${test.src.dir}" includes="**/*Test.java"/> 
     </batchtest> 
    </junit> 

    <junitreport todir="${build.test.report.dir}" tofile="TEST-Summary.xml"> 
     <fileset dir="${build.test.report.dir}" includes="*.xml"/> 
    </junitreport> 
</target> 

我可以運行他們,並沒有錯誤打印報告。

但是,螞蟻只會告訴我是否有失敗的測試。我需要通過命令cat手動檢出測試報告。我想知道如何強制使用顏色將stdout上的失敗測試報告傾倒出來。

感謝

回答

1

嘗試以下方法:使用

<!-- new testwithcat target --> 
<target name="testwithcat" depends="tdd, catreport"/> 

<target name="tdd" description="Run unittest" depends="jar"> 
    <mkdir dir="${build.test.dir}"/> 
    <javac srcdir="${test.src.dir}" destdir="${build.test.dir}" 
     includeAntRuntime="false"> 
     <classpath refid="lib.junit"/> 
     <classpath refid="lib.dataquery"/> 
    </javac> 
    <jar destfile="${build.jar.dir}/dataqueryTDD.jar"> 
     <fileset dir="${build.test.dir}"> 
      <include name="**/*.class"/> 
     </fileset> 
    </jar> 
    <mkdir dir="${build.test.report.dir}"/> 

<!-- add errorProperty, failureProperty to junit task --> 
<junit printsummary="yes" errorProperty="test.failed" failureProperty="test.failed"> 
     <classpath refid="lib.junit"/> 
     <classpath refid="lib.dataquery"/> 
     <classpath refid="lib.dataquery.tdd"/> 
     <formatter type="xml"/> 
     <batchtest todir="${build.test.report.dir}"> 
      <fileset dir="${test.src.dir}" includes="**/*Test.java"/> 
     </batchtest> 
    </junit> 

    <junitreport todir="${build.test.report.dir}" tofile="TEST-Summary.xml"> 
     <fileset dir="${build.test.report.dir}" includes="*.xml"/> 
    </junitreport> 
</target> 

<!-- add cat report target that runs if test.failed has been set in junit task --> 
    <target name="catreport" depends="test" if="test.failed"> 
    <echo message="Test failed - cat the test report"/> 
    <!-- customise the following to cat your report in colour --> 
    <exec executable="cat"> 
     <arg value="${build.test.report.dir}/TEST-Summary.xml"/> 
    </exec> 
    </target> 

運行:

ant testwithcat 

輸出:

Test failed - cat the test report 
[content of ${build.test.report.dir}/TEST-Summary.xml] 

您需要定製

<exec executable="cat"> 
    <arg value="${build.test.report.dir}/TEST-Summary.xml"/> 
</exec> 

用你自己的命令「用你的顏色來報告你的報告」。

要獲得純文本輸出,請查看Ant JUnit Nice Output Formatter

您可以使用

<report format="frames" todir="${build.test.report.dir}"/> 

<report format="noframes" todir="${build.test.report.dir}"/> 

在junitreport任務生成HTML輸出。

您可以用調用替換

<exec executable="cat"> 
    <arg value="${build.test.report.dir}/TEST-Summary.xml"/> 
</exec> 

顯示在瀏覽器的HTML。示例代碼請參閱Exec來完成此操作。

+0

謝謝。如果測試失敗,我可以抓取報告。然而,'''TEST-Summary.xml''的格式不是人性化的...有沒有解析XML的命令?或者,是否有可能獲得失敗的測試,以便我可以用'''printsummary'''運行另一個'''junit''來轉儲這些錯誤? – 2014-09-06 12:36:40

+0

完美。非常感謝! – 2014-09-06 13:38:39

+0

這兩個建議(網頁和純文本)都非常好。就個人而言,我選擇了一個網頁,因爲它需要較少的代碼:D。再次感謝。 – 2014-09-06 13:44:21