Junit 报告在存在断言错误时无法生成报告

5
Junit报告如果存在断言错误,则不会生成报告 待测试的类
public class Math {

    static public int add(int a, int b) {

        return a + b;
    }

    static public int multiply ( int a, int b) {
        if(a==0&&b==0)
            return 0;;
        return a * b;
    }
}

Junit测试类

import junit.framework.*;

public class TestMath extends TestCase { 

  protected void setUp() { 

    // put common setup code in here
   }

  protected void tearDown() {

    // put common cleanup code in here
  }

  public void testAdd() {
    int num1 = 3;
    int num2 = 2;
    int total = 5;
    int sum = 0;
    sum = Math.add(num1, num2);
    assertEquals(sum, total);
  }

  public void testMulitply() {

    int num1 = 3; 
    int num2 = 7; 
    int total = 21;
    int sum = 0;
    sum = Math.multiply(num1, num2);
    assertEquals("Problem with multiply", sum, total);


    num1 = 5;
    num2 = 4;
    total = 20;
    sum = Math.multiply(num1, num2);
    assertEquals("Problem with multiply", sum, total);

  }
  public void testv(){
     Assert.assertEquals(1, Math.multiply(0, 0));
  }

}

我正在使用JUnit报告生成器来生成报告。

问题在于,如果断言失败,则无法生成报告,Ant构建失败。我的假设是,如果断言失败,它应该列在失败列表中,而不是出现在构建结果的错误中。

Ant XML

<project name="SampleJUnitTests" default="dist" basedir=".">
    <description>
        Sample JUnit Tests
    </description>
  <!-- set global properties for this build -->

  <property name="project_name" value="junitSamples"/>
  <property name="src" location="src"/>
  <property name="build" location="bin"/>
  <property name="dist"  location="dist"/>
  <property name="lib"  location="lib"/>
  <property name="res"  location="res"/>
  <property name="reports" location="reports"/>

  <!-- the names of various distributable files -->
  <property name="jar_name" value="${project_name}.jar"/>
  <property name="war_name" value="${project_name}.war"/>

    <!-- top level targets -->

  <target name="compile" depends="init" description="compile the source code " >
        <javac srcdir="${src}" destdir="${build}">  
            <classpath>
                <fileset dir="lib">
                    <include name="**/*.jar"/>
                </fileset>
            </classpath>
        </javac>

  </target>

  <target name="dist" depends="compile" description="generate the distributable files " >

    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/${jar_name}" basedir="${build}"/>

  </target>

  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
    <delete dir="${reports}"/>
  </target>

    <target name="run-tests" depends="compile" description="run your test suite" >
        <junit printsummary="yes" haltonfailure="yes" showoutput="yes" >
            <classpath>
                <pathelement path="${build}"/>
                <fileset dir="lib">
                    <include name="**/*.jar"/>
                </fileset>
            </classpath>            

          <batchtest fork="yes" todir="${reports}/raw/">
            <formatter type="xml"/>
            <fileset dir="${src}">
              <include name="**/*Test*.java"/>
            </fileset>
          </batchtest>
        </junit>    
    </target>

  <target name ="test" depends="run-tests">
        <junitreport todir="${reports}">
          <fileset dir="${reports}/raw/">
            <include name="TEST-*.xml"/>
          </fileset>
          <report format="frames" todir="${reports}/html/"/>
        </junitreport>
  </target>

  <target name ="run" depends="" description="if this project can be run, run it" >

  </target>

    <!-- supporting targets -->

     <target name="init" description="initialize the build environment" >
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create directory structures -->
    <mkdir dir="${build}"/>
    <mkdir dir="${lib}"/>
    <mkdir dir="${dist}/lib"/>
    <mkdir dir="${reports}"/>
    <mkdir dir="${reports}/raw/"/>
    <mkdir dir="${reports}/html/"/>
  </target>

  <target name="all" depends="clean, test">

  </target>

</project>

我的需求是,如果断言失败了,它应该在失败中显示?我需要做什么?

我的要求是,如果断言失败了,它应该在失败信息中展示。我需要怎么做?

这是一个Eclipse还是Ant的问题?请相应地打标签。 - oers
1
你是否正在使用 Ant JUnit 任务?如果是,请在你的问题中添加一个显示你正在使用的确切 XML 的部分。 - Chad Nouis
@ChadNouis,我已经添加了Ant构建XML文件。感谢您的回复。 - tharani dharan
找到了解决方案 将haltonfailure的值更改为“no”,它将按预期工作 <junit haltonfailure="no" failureproperty="test.failed" ... > ... </junit> - tharani dharan
@tharanidharan,所以你应该关闭这个问题。 - gontard
1个回答

2

这个问题已经在评论中由Tharani回答了... 我在回答它,以免它成为未回答的问题...

将haltonfailure="no"的值更改为"yes",它将按预期工作...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接