【问题标题】:Ant+Junit works on first run and never againAnt+Junit 在第一次运行时工作,以后不再运行
【发布时间】:2009-11-25 04:43:52
【问题描述】:

大家好,我整天都在尝试获取一个 ant 文件来自动构建我的项目。该文件(附在下面)在我找到的网页上,非常详尽。

我的问题是,只要我不运行“干净”目标,它就可以工作。一旦我运行“干净”目标,“测试”目标就会停止工作。我在所有测试类上都收到 NoClassFound 错误。即使它在早些时候起作用。

我正在使用最新版本的 Eclipse 开发 Macbook (10.5.8)。从 Eclipse 和使用 Ant 1.7.1 版的终端运行文件时会发生此错误

我稍微修改了文件以使其适应我的文件结构,如下所示:

src/
   packageA.packageB/
                     ClassA.java
                     ...
                     ClassN.java

unittests/
          packageA.packageB/
                            AllClassTests.java
                            ClassATest.java
                            ...
                            ClassNTest.java

lib/
    junit-4.7.jar

ant文件是:

<project name="SampleJUnitTests" default="dist" basedir=".">
<description>
    DataTypes Build File
</description>
<!-- set global properties for this build -->

<property name="project_name" value="DataTypes"/>
<property name="src" location="src"/>
<property name="build" location="bin"/>
<property name="dist"  location="dist"/>
<property name="lib"  location="lib"/>
<property name="reports" location="reports"/>
<property name="tests" location="unittests"/>
<property name="tmp" location="tmp_file"/>

<!-- 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}"/>
    <delete dir="${tmp}"/>
</target>

<target name="run-tests" depends="compile" description="run your test suite" >
    <junit printsummary="yes" haltonfailure="no" showoutput="yes" tempdir="${tmp}">
        <classpath>
            <pathelement path="${build}"/>
            <fileset dir="lib">
                <include name="**/*.jar"/>
            </fileset>
        </classpath>
      <batchtest fork="yes" todir="${reports}/raw/">
        <formatter type="xml"/>
        <fileset dir="${tests}">
          <include name="**/*.java"/>
          <exclude name="**/All*Tests.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/"/>
    <mkdir dir="${tmp}"/>
</target>

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

我放弃了,希望有人能解决我的问题。 非常感谢!

【问题讨论】:

    标签: java ant junit


    【解决方案1】:

    您没有任何构建测试类的 Ant 目标。如果您愿意,可以将其作为 compile 目标的一部分:

    <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>
      <javac srcdir="${tests}" destdir="${build}">
        <classpath>
          <pathelement path="${build}"/>
          <fileset dir="lib">
            <include name="**/*.jar"/>
          </fileset>
        </classpath>
      </javac>
    </target>
    

    【讨论】:

    • 谢谢!就是这样。我没有注意到当我修改脚本时,他们的测试与源代码混合在一起。 :)
    猜你喜欢
    • 2016-06-23
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多