Ant 告诉我我的 Junit 测试成功了,但实际上却没有成功

发布于 2024-09-15 09:16:47 字数 2872 浏览 5 评论 0原文

我有以下 build.xml 文件

<property file="build.properties"/>
  <property name="src.dir" value="src"/>
  <property name="build.dir" value="classes"/>
  <property name="web.dir" value="war"/>
  <property name="test.dir" value="test"/>

  <path id="build.classpath">
      <fileset dir="lib">
       <include name="*.jar"/>
      </fileset>
      <!-- servlet API classes: -->
      <fileset dir="${appserver.lib}">
          <include name="servlet*.jar"/>
      </fileset>
      <pathelement path="${build.dir}"/>
      <pathelement path="${test.dir}"/>
  </path>

 <path id="classpath.base"/>
 <path id="classpath.test">
     <pathelement location="c:/ant/lib/junit.jar" />
     <pathelement location="${build.dir}"/>
     <pathelement location="${src.dir}"/>
     <pathelement location="${test.dir}" />
     <pathelement location="classes"/>
    <path refid="classpath.base" />
</path>

<target name="build">
      <!-- Following two lines creat src and test folders in WEB/INF folders -->
      <mkdir dir="${build.dir}"/>
  <mkdir dir="${test.dir}"/>
     <javac destdir="${build.dir}" source="1.5" target="1.5" debug="true" deprecation="false" optimize="false" failonerror="true">
      <src path="${src.dir}"/>
      <classpath refid="build.classpath"/>
      </javac>
  <javac destdir="${build.dir}" source="1.5" target="1.5" debug="true" deprecation="false" optimize="false" failonerror="true">
          <src path="${test.dir}"/>
          <classpath refid="build.classpath"/>
      </javac>
</target>

    <target name="test">
    <junit haltonfailure="true" printsummary="yes">
      <classpath refid="classpath.test" />
      <classpath refid="build.classpath"/>
      <formatter type="brief" usefile="false" />
      <batchtest fork="yes">
            <fileset dir="${test.dir}">
                <include name="**/*Test.java"/>
            </fileset>
      </batchtest>
    </junit>

,并且有以下测试类只是为了测试构建

package com.mmz.mvc.test;

import junit.framework.*;

public class MemberDAOTest extends TestCase
{   
    public void test1() {
        assertTrue("Test didn't work",false);
    }
}   

显然这个测试应该会失败,但事实并非如此。谁能告诉我为什么我会得到以下输出。另外,如果您发现有问题,请告诉我是否可以改进我的构建脚本,我是编写任何构建文件的新手。

test:
    [junit] Running com.mmz.mvc.test.MemberDAOTest
    [junit] Testsuite: com.mmz.mvc.test.MemberDAOTest
    [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.018 sec
    [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.018 sec
BUILD SUCCESSFUL
Total time: 1 second

I have the following build.xml file

<property file="build.properties"/>
  <property name="src.dir" value="src"/>
  <property name="build.dir" value="classes"/>
  <property name="web.dir" value="war"/>
  <property name="test.dir" value="test"/>

  <path id="build.classpath">
      <fileset dir="lib">
       <include name="*.jar"/>
      </fileset>
      <!-- servlet API classes: -->
      <fileset dir="${appserver.lib}">
          <include name="servlet*.jar"/>
      </fileset>
      <pathelement path="${build.dir}"/>
      <pathelement path="${test.dir}"/>
  </path>

 <path id="classpath.base"/>
 <path id="classpath.test">
     <pathelement location="c:/ant/lib/junit.jar" />
     <pathelement location="${build.dir}"/>
     <pathelement location="${src.dir}"/>
     <pathelement location="${test.dir}" />
     <pathelement location="classes"/>
    <path refid="classpath.base" />
</path>

<target name="build">
      <!-- Following two lines creat src and test folders in WEB/INF folders -->
      <mkdir dir="${build.dir}"/>
  <mkdir dir="${test.dir}"/>
     <javac destdir="${build.dir}" source="1.5" target="1.5" debug="true" deprecation="false" optimize="false" failonerror="true">
      <src path="${src.dir}"/>
      <classpath refid="build.classpath"/>
      </javac>
  <javac destdir="${build.dir}" source="1.5" target="1.5" debug="true" deprecation="false" optimize="false" failonerror="true">
          <src path="${test.dir}"/>
          <classpath refid="build.classpath"/>
      </javac>
</target>

    <target name="test">
    <junit haltonfailure="true" printsummary="yes">
      <classpath refid="classpath.test" />
      <classpath refid="build.classpath"/>
      <formatter type="brief" usefile="false" />
      <batchtest fork="yes">
            <fileset dir="${test.dir}">
                <include name="**/*Test.java"/>
            </fileset>
      </batchtest>
    </junit>

and I have the following test class just to test out the build

package com.mmz.mvc.test;

import junit.framework.*;

public class MemberDAOTest extends TestCase
{   
    public void test1() {
        assertTrue("Test didn't work",false);
    }
}   

Obviously this test is supposed to fail, but its not. Can anybody tell me why I am getting the following output. Also, if there is something you see wrong, then please let me know if I can improve my build script, I am new to writing any build files.

test:
    [junit] Running com.mmz.mvc.test.MemberDAOTest
    [junit] Testsuite: com.mmz.mvc.test.MemberDAOTest
    [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.018 sec
    [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.018 sec
BUILD SUCCESSFUL
Total time: 1 second

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妖妓 2024-09-22 09:16:47

确认 Ant 运行最新版本的 MemberDAOTest 的简单方法:
添加方法 test2(),以查看 Ant 是否运行 test2()

Easy way to confirm Ant runs your latest version of MemberDAOTest:
Add method test2(), to see if Ant runs test2() or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文