2010-01-31 45 views
2

當我運行JUnit測試失敗時會導致非常大的堆棧跟蹤,而這些跟蹤並不能提供很多信息。我希望能夠看到失敗的assert語句,以便每個故障最多佔用2或3行,而不是20個。如何讓JUnit安靜?

我不使用ANT來調用JUnit,而是從一個非常大批量的命令行。額外的輸出僅僅使得解析數據變得困難。

編輯:此測試是針對編程課程中的學生作業,所以會出現錯誤,並且有相當多的程序需要測試。

回答

3

鑑於這是一個比較少見的情況(從大批量的命令行運行JUnit,而不是使用IDE或像Ant這樣的工具),如果結果不是一種方式,我不會感到驚訝在JUnit本身內部進行工作。

爲什麼不把結果寫入文件,然後通過一個小的解析器運行它,該解析器可以識別JUnit失敗的開始,打印接下來的幾行,然後跳到下一個的開始?

2

我會說你的JUnit測試會很好,很安靜,一旦你修復它們。留下「嘈雜」堆棧痕跡作爲消除它們的誘因並返回綠色欄。

「...我不使用ANT調用JUnit ...」 - 爲什麼不呢?什麼是批量購買你的產品?如果你使用Ant,你不僅會得到測試,還會有HTML報告任務。這將「安靜」的輸出,很好地呈現它,而不會丟棄堆棧跟蹤細節。

聽起來就像您在您編寫的JUnit測試工具中使用提交的作業作爲第三方JAR。好的方法。應該是向上或向下,紅色或綠色。你現在是QA部門 - 堆棧跟蹤是學生的責任。

你可以給他們測試類,並告訴他們在運行時讓他們的東西通過。把責任放在它們所屬的地方。

學生應該早點學習單元測試的價值。我會向他們提供JUnit和Ant ,並使它們提供運行的JUnit測試作爲傳遞任務的先決條件。

下面是一個示例Ant build.xml,您可以根據需要自由修改該示例。參加測試的任務特別說明:

<?xml version="1.0" encoding="UTF-8"?> 
    <project name="spring-finance" basedir="." default="package"> 

     <property name="version" value="1.6"/> 
     <property name="haltonfailure" value="no"/> 

     <property name="out" value="out"/> 

     <property name="production.src" value="src/main/java"/> 
     <property name="production.lib" value="src/main/webapp/WEB-INF/lib"/> 
     <property name="production.resources" value="src/main/resources"/> 
     <property name="production.classes" value="${out}/production/${ant.project.name}"/> 

     <property name="test.src" value="src/test/java"/> 
     <property name="test.lib" value="src/test/lib"/> 
     <property name="test.resources" value="src/test/resources"/> 
     <property name="test.classes" value="${out}/test/${ant.project.name}"/> 

     <property name="exploded" value="out/exploded/${ant.project.name}"/> 
     <property name="exploded.classes" value="${exploded}/WEB-INF/classes"/> 
     <property name="exploded.lib" value="${exploded}/WEB-INF/lib"/> 

     <property name="reports.out" value="${out}/reports"/> 
     <property name="junit.out" value="${reports.out}/junit"/> 

     <property name="web.src" value="src/main/webapp"/> 
     <property name="web.lib" value="${web.src}/WEB-INF/lib"/> 
     <property name="web.classes" value="${web.src}/WEB-INF/classes"/> 

     <path id="production.class.path"> 
      <pathelement location="${production.classes}"/> 
      <pathelement location="${production.resources}"/> 
      <fileset dir="${production.lib}"> 
       <include name="**/*.jar"/> 
       <exclude name="**/junit*.jar"/> 
       <exclude name="**/*test*.jar"/> 
      </fileset> 
     </path> 

     <path id="test.class.path"> 
      <path refid="production.class.path"/> 
      <pathelement location="${test.classes}"/> 
      <pathelement location="${test.resources}"/> 
      <fileset dir="${test.lib}"> 
       <include name="**/junit*.jar"/> 
       <include name="**/*test*.jar"/> 
      </fileset> 
     </path> 

     <available file="${out}" property="outputExists"/> 

     <target name="clean" description="remove all generated artifacts" if="outputExists"> 
      <delete dir="${out}" includeEmptyDirs="true"/> 
     </target> 

     <target name="create" description="create the output directories" unless="outputExists"> 
      <mkdir dir="${production.classes}"/> 
      <mkdir dir="${test.classes}"/> 
      <mkdir dir="${junit.out}"/> 
      <mkdir dir="${exploded.classes}"/> 
      <mkdir dir="${exploded.lib}"/> 
     </target> 

     <target name="compile" description="compile all .java source files" depends="create"> 
    <!-- Debug output 
      <property name="production.class.path" refid="production.class.path"/> 
      <echo message="${production.class.path}"/> 
    --> 
      <javac srcdir="src" destdir="${out}/production/${ant.project.name}" debug="on" source="${version}"> 
       <classpath refid="production.class.path"/> 
       <include name="**/*.java"/> 
       <exclude name="**/*Test.java"/> 
      </javac> 
      <javac srcdir="${test.src}" destdir="${out}/test/${ant.project.name}" debug="on" source="${version}"> 
       <classpath refid="test.class.path"/> 
       <include name="**/*Test.java"/> 
      </javac> 
     </target> 

     <target name="test" description="run all unit tests" depends="compile"> 
    <!-- Debug output 
      <property name="test.class.path" refid="test.class.path"/> 
      <echo message="${test.class.path}"/> 
    --> 
      <junit printsummary="yes" haltonfailure="${haltonfailure}"> 
       <classpath refid="test.class.path"/> 
       <formatter type="xml"/> 
       <batchtest fork="yes" todir="${junit.out}"> 
        <fileset dir="${test.src}"> 
         <include name="**/*Test.java"/> 
        </fileset> 
       </batchtest> 
      </junit> 
      <junitreport todir="${junit.out}"> 
       <fileset dir="${junit.out}"> 
        <include name="TEST-*.xml"/> 
       </fileset> 
       <report todir="${junit.out}" format="frames"/> 
      </junitreport> 
     </target> 

     <target name="exploded" description="create exploded deployment" depends="test"> 
      <copy todir="${exploded}"> 
       <fileset dir="${web.src}"/> 
      </copy> 
      <copy todir="${exploded}/WEB-INF"> 
       <fileset dir="${web.src}/WEB-INF"/> 
      </copy> 
      <copy todir="${exploded.classes}"> 
       <fileset dir="${production.classes}"/> 
      </copy> 
      <copy todir="${exploded.lib}"> 
       <fileset dir="${production.lib}"/> 
      </copy> 
     </target> 

     <target name="jar" description="create jar file" depends="test"> 
      <jar destfile="${out}/${ant.project.name}.jar" basedir="${production.classes}" includes="**/*.class"/> 
     </target> 

     <target name="war" description="create war file" depends="exploded"> 
      <war basedir="${exploded}" webxml="${exploded}/WEB-INF/web.xml" destfile="${out}/${ant.project.name}.war"/> 
     </target> 

     <target name="package" description="create package for deployment" depends="test"> 
      <antcall target="war"/> 
     </target> 

    </project> 
+0

雖然這樣會很好,但是測試的代碼實際上是由學生提交的任務,所以它會失敗。我們不使用ANT的唯一原因是因爲我們還沒有投入時間來改變它。儘管可能需要進行一些調查。 – 2010-02-01 00:30:07

1

forum post似乎描述的方式來過濾什麼是JUnit的報道,當你運行命令行測試。該方法似乎是創建一個替代入口點類(即「主要」方法),該類使用另一個RunListener來記錄錯誤的方式。

警告:這將涉及到一些Java編碼,通過JUnit javadocs進行拖網,以及(可能)查看JUnit源代碼的想法。

0

失敗的JUnit測試用例應該是嘈雜的。我喜歡他們向我尖叫;)

如果你的單元測試用例不斷地惹怒和破壞,雖然一切都很好(誤報),而是想到改變測試用例。而是包裝JUnit測試運行器,而不是自行運行測試用例。