在 Eclipse 中使用 Ant 运行 JUnit 测试时出现问题。初学者问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7547439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 17:02:51  来源:igfitidea点击:

problem running JUnit tests with Ant in Eclipse. Beginner question

javaeclipseantjunit

提问by Heisenbug

I'm learning these days how to use ant to run automated test folowing this tutorial.

这些天我正在学习如何按照本教程使用 ant 运行自动化测试。

I have JUnit in the classpath of my project. All seem to work fine and I can include it in my classes:

我的项目的类路径中有 JUnit。一切似乎都很好,我可以将它包含在我的课程中:

import junit.framework.TestCase; //line20

public class SimpleLattice1DTest extends TestCase{
 ...
}

My build.xml is:

我的 build.xml 是:

<?xml version="1.0"?>
<project name="Ant-Test" default="compile" basedir=".">
    <!-- Sets variables which can later be used. -->
    <!-- The value of a property is accessed via ${} -->
    <property name="src.dir" location="." />
    <property name="build.dir" location="build" />
    <property name="dist.dir" location="dist" />
    <property name="docs.dir" location="docs" />
    <property name="test.dir" location="jlife/tests" />
    <property name="test.report.dir" location="test/report" />  

    <!-- Deletes the existing build, docs and dist directory-->
    <target name="clean">
        <delete dir="${build.dir}" />
        <delete dir="${docs.dir}" />
        <delete dir="${dist.dir}" />
    </target>

    <!-- Creates the  build, docs and dist directory-->
    <target name="makedir">
        <mkdir dir="${build.dir}" />
        <mkdir dir="${docs.dir}" />
        <mkdir dir="${dist.dir}" />

        <mkdir dir="${test.report.dir}" />


    </target>

    <!-- Compiles the java code (including the usage of library for JUnit -->
    <target name="compile" depends="clean, makedir">
        <javac srcdir="${src.dir}" destdir="${build.dir}">
        </javac>

    </target>

    <!-- Creates Javadoc -->
    <target name="docs" depends="compile">
        <javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
            <!-- Define which files / directory should get included, we include all -->
            <fileset dir="${src.dir}">
                <include name="**" />
            </fileset>
        </javadoc>
    </target>

    <!--Creates the deployable jar file  -->
    <target name="jar" depends="compile">
        <jar destfile="${dist.dir}\CoreTest.jar" basedir="${build.dir}">
            <manifest>
                <attribute name="Main-Test" value="test.CoreTest" />
            </manifest>
        </jar>
    </target>


    <!-- Run the JUnit Tests -->
        <!-- Output is XML, could also be plain-->
        <target name="junit" depends="compile">
            <junit printsummary="on" fork="true" haltonfailure="yes">

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

</project>

When i run it into eclipse I get the following error:

当我将它运行到 eclipse 时,出现以下错误:

[javac] C:\Documents and Settings\noname\Documenti\JLife_git\JLife_git\JLife\src\jlife\tests\SimpleLattice1DTest.java:20: package junit.framework does not exist [javac] import junit.framework.TestCase;

[javac] C:\Documents and Settings\noname\Documenti\JLife_git\JLife_git\JLife\src\jlife\tests\SimpleLattice1DTest.java:20: package junit.framework 不存在 [javac] import junit.framework.TestCase;

I suppose there's something wrong with it, but I have no idea. Could someone put me in the right direction?

我想它有什么问题,但我不知道。有人能把我放在正确的方向吗?

回答by Jon Skeet

Your javactarget doesn't specify anything apart from the source and target directory - it doesn't add any classpath entries; you'll need to add an entry for the appropriate JUnit jar file. See the javactask documentationfor more details. You may want to specify the path to JUnit as a classpath attribute, a nested element, or a reference to a path declared elsewhere.

javac除了源和目标目录之外,您的目标没有指定任何内容 - 它没有添加任何类路径条目;您需要为相应的 JUnit jar 文件添加一个条目。有关更多详细信息,请参阅javac任务文档。您可能希望将 JUnit 的路径指定为类路径属性、嵌套元素或对其他地方声明的路径的引用。

回答by JohnKlehm

You need to specify the directory that contains your .class files and your external jars (like junit).

您需要指定包含 .class 文件和外部 jar(如 junit)的目录。

e.g.

例如

<!-- Populates a class path containing our classes and jars -->
<path id="dist.classpath">
    <fileset dir="${lib}"/>
    <pathelement path="${build}"/>
</path>
<!-- Compile the java code place into ${build} -->
<target name="compile" depends="-dirty" description="Compile the source.">
    <javac srcdir="${source}" destdir="${build}" includeantruntime="false">
        <classpath refid="dist.classpath"/>
        <exclude name="${test.relative}/**/*"/>
    </javac>
</target>

Here's the complete file I took that excerpt from in case you need ideas for how to setup other common things (emma, javadoc, etc)

这是我摘录的完整文件,以防您需要有关如何设置其他常见内容(emma、javadoc 等)的想法

<project name="imp" default="dist" basedir="..">

<description>Buildscript for IMP</description>

<property name="source" location="src"/>
<property name="lib"  location="lib"/>
<property name="history" location="test_history"/>
<property name="web-tests" location="/var/www/tests"/>
<property name="web-files" location="/var/www/files"/>
<property name="web-javadoc" location="/var/www/javadoc"/>
<property name="web-emma" location="/var/www/emma"/>
<property name="emma.dir" value="${lib}"/>
<property name="test" location="${source}/imp/unittest"/>
<property name="test.relative" value="imp/unittest"/>
<property name="javadoc-theme" value="tools/javadoc-theme"/>

<!-- directories for generated files -->
<property name="build" location="build"/>
<property name="build-debug" location="debug"/>
<property name="build-coverage" location="coverage"/>
<property name="dist"  location="dist"/>
<property name="reports" location="reports"/>
<property name="coverage-emma" location="${reports}/coverage/emma"/>

<!-- Populates a class path containing our classes and jars -->
<path id="dist.classpath">
    <fileset dir="${lib}"/>
    <pathelement path="${build}"/>
</path>
<path id="debug.classpath">
    <fileset dir="${lib}"/>
    <pathelement path="${build-debug}"/>
</path>

<!-- import emma. This classpath limits the coverage to just our classes -->
<path id="debug.imp.classpath">
    <pathelement path="${build-debug}"/>
</path>
<taskdef resource="emma_ant.properties" classpathref="debug.classpath"/>

<!-- 
    Shouldn't ever need to use this from the command line. IRC saith that the "private"
    internal use only sort of targets are prefixed with '-'. 
    dirty because it's the opposite of the 'clean' target.
-->
<target name="-dirty">
    <tstamp/>
    <mkdir dir="${build}"/>
    <mkdir dir="${build-debug}"/>
    <mkdir dir="${build-coverage}"/>
    <mkdir dir="${dist}"/>
    <mkdir dir="${reports}"/>
    <mkdir dir="${coverage-emma}"/>
</target>

<!-- clean up all the generated files and direcories -->
<target name="clean" description="Deletes all files and directories created by this script.">
    <delete dir="${build}"/>
    <delete dir="${build-debug}"/>
    <delete dir="${build-coverage}"/>
    <delete dir="${dist}"/>
    <delete dir="${reports}"/>
    <delete dir="${coverage-emma}"/>
</target>

<!-- Compile the java code place into ${build} -->
<target name="compile" depends="-dirty" description="Compile the source.">
    <javac srcdir="${source}" destdir="${build}" includeantruntime="false">
        <classpath refid="dist.classpath"/>
        <exclude name="${test.relative}/**/*"/>
    </javac>
</target>

<!-- Compile the java code with debug info place into ${build} -->
<target name="compile-debug" depends="-dirty" description="Compile the source with debug information.">
    <javac
        srcdir="${source}"
        destdir="${build-debug}"
        includeantruntime="false"
        debug="true"
        debuglevel="lines,vars,source"
    >
        <classpath refid="debug.classpath"/>
    </javac>
</target>

<!-- roll up everyting into a single jar file -->
<target name="dist" depends="clean, compile" description="Generate the distribution file for IMP.">
    <!-- Copy the library .jars to the directory where the IMP distribution will be located -->
    <copy todir="${dist}">
        <fileset dir="${lib}"/>
    </copy>

    <!-- TODO: Generate the MANIFEST.MF file on the fly -->
    <jar jarfile="${dist}/imp.jar" basedir="${build}" manifest="tools/MANIFEST.MF"/>

    <!-- dump to web server -->
    <copy todir="${web-files}">
        <fileset dir="${dist}"/>
    </copy>
</target>

<!-- build and run the tests then report the results in HTML -->
<target name="test" depends="compile-debug" description="Run all the JUnit tests and outputs the results as HTML.">
    <!-- run the tests -->
    <junit printsummary="true" haltonerror="false" haltonfailure="false">
        <classpath refid="debug.classpath"/>
        <formatter type="xml"/>
        <batchtest fork="true" todir="${reports}">
            <fileset dir="${source}">
                <include name="${test.relative}/**/*Test*.java"/>
                <exclude name="${test.relative}/**/AllTests.java"/>
            </fileset>
        </batchtest>
    </junit>

    <!-- report the results -->
    <junitreport todir="${reports}">
        <fileset dir="${reports}" includes="TEST-*.xml"/>
        <report todir="${reports}"/>
    </junitreport>

    <!-- update the latest results file to be commited -->
    <copy file="${reports}/TESTS-TestSuites.xml" tofile="${history}/test-results-latest.xml"/>

    <!-- dump to webserver -->
    <copy todir="${web-tests}">
        <fileset dir="${reports}"/>
    </copy>
</target>

<!-- run emma code coverage tool and publish results in HTML -->
<target name="emma" depends="compile-debug"  description="Checks code coverage with Emma.">
    <!-- put the magic emma juice into the classes -->
    <emma>
        <instr
            instrpathref="debug.imp.classpath"
            destdir="${coverage-emma}/instr"
            metadatafile="${coverage-emma}/metadata.emma"
            merge="true"
        />
    </emma>

    <!-- run the tests -->
    <junit fork="true" printsummary="true" haltonerror="false" haltonfailure="false">
        <classpath>
            <pathelement location="${coverage-emma}/instr"/>
            <path refid="debug.classpath"/>
        </classpath>
        <batchtest fork="true" todir="${reports}">
            <fileset dir="${source}">
                <include name="${test.relative}/**/*Test*.java"/>
                <exclude name="${test.relative}/**/AllTests.java"/>
            </fileset>
        </batchtest>
        <jvmarg value="-Demma.coverage.out.file=${coverage-emma}/coverage.emma"/>
        <jvmarg value="-Demma.coverage.out.merge=true"/>
    </junit>

    <!-- publish the coverage report -->
    <emma>
        <report sourcepath="${source}" verbosity="verbose">
            <fileset dir="${coverage-emma}">
                <include name="*.emma"/>
            </fileset>

            <html outfile="${web-emma}/index.html"/>
        </report>
    </emma>
</target>

<!-- publish javadoc -->
<target name="javadoc" description="Creates javadoc for IMP.">
    <delete dir="${web-javadoc}"/>
    <javadoc
            sourcepath="${source}"
            defaultexcludes="no"
            destdir="${web-javadoc}"
            author="true"
            version="true"
            use="true"
            windowtitle="IMP: Integrated Mechanisms Program"
            overview="${source}/overview.html"
            classpathref="debug.classpath"
            stylesheetfile="${javadoc-theme}/stylesheet.css"
    />
    <copy file="${javadoc-theme}/javadoc.jpg" tofile="${web-javadoc}/javadoc.jpg"/>
</target>

<target name="all" description="Runs test, emma, javadoc, and dist targets.">
    <antcall target="test"/>
    <antcall target="emma"/>
    <antcall target="javadoc"/>
    <antcall target="dist"/>
</target>

</project>

回答by GenericJon

The eclipse classpath is separate from your ant environment. In your build file, when you call javacyou need to supply a classpath attribute.

eclipse 类路径与您的 ant 环境是分开的。在您的构建文件中,当您调用时,javac您需要提供一个类路径属性。

You can define the classpath at the top of the file with the rest of your properties, like this:

您可以使用其余属性在文件顶部定义类路径,如下所示:

<path id="classpath">
    <fileset dir="[path to libraries]" includes="**/*.jar" />
</path>

and then use it in each call to javac by setting the classpathrefattribute, like this:

然后通过设置classpathref属性在每次调用 javac 时使用它,如下所示:

<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath" />

回答by Aniruddha Jagtap

If you observe the error stack, you will find the following line, just above the error line you mentioned...

如果您观察错误堆栈,您会发现以下行,就在您提到的错误行上方......

[javac] [search path for class files: C:\Program Files\Java\jre6\lib\resource...

[javac] [class文件的搜索路径:C:\Program Files\Java\jre6\lib\resource...

This line shows all the jars available in the class path for this ant target execution. You will definitely not find the desired jar over here i.e. junit-x.x.x.jar (junit-4.8.2.jar)

这一行显示了在这个 ant 目标执行的类路径中可用的所有 jar。你绝对不会在这里找到你想要的 jar,即 junit-xxxjar (junit-4.8.2.jar)

Now go to eclipse -> Window -> preferences -> Ant -> Runtime -> Global Entries -> Add Jars add junit-4.8.2jar (which you will find in your project lib directory)

现在转到 eclipse -> Window -> 首选项 -> Ant -> Runtime -> Global Entries -> Add Jars add junit-4.8.2jar(你会在你的项目 lib 目录中找到它)

If you play around the Ant -> Runtime -> classpath and the classpath related error line in the error stack, you will understand the issue.

如果您在错误堆栈中处理 Ant -> Runtime -> classpath 和与类路径相关的错误行,您就会理解这个问题。

Hope this solves your problem.

希望这能解决您的问题。