在 Eclipse 中使用 Ant 运行 JUnit 测试时出现问题。初学者问题
这些天我正在学习如何使用 ant 来按照这个 教程 运行自动化测试。
我的项目的类路径中有 JUnit。一切似乎都工作正常,我可以将它包含在我的类中:
import junit.framework.TestCase; //line20
public class SimpleLattice1DTest extends TestCase{
...
}
我的 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>
当我将它运行到 Eclipse 中时,出现以下错误:
[javac] C:\Documents 和 设置 \noname\Documenti\JLife_git\JLife_git\JLife\src\jlife\tests\SimpleLattice1DTest.java:20: 包 junit.framework 不存在 [javac]导入junit.framework.TestCase;
我想它有问题,但我不知道。有人能让我走上正确的方向吗?
I'm learning these days how to use ant to run automated test folowing this tutorial.
I have JUnit in the classpath of my project. All seem to work fine and I can include it in my classes:
import junit.framework.TestCase; //line20
public class SimpleLattice1DTest extends TestCase{
...
}
My build.xml is:
<?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:
[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;
I suppose there's something wrong with it, but I have no idea. Could someone put me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的 javac 目标除了源目录和目标目录之外没有指定任何内容 - 它不会添加任何类路径条目;您需要为相应的 JUnit jar 文件添加一个条目。有关更多详细信息,请参阅
javac
任务文档。您可能希望将 JUnit 的路径指定为类路径属性、嵌套元素或对其他地方声明的路径的引用。Your
javac
target 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 thejavac
task documentation for 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.eclipse 类路径与您的 ant 环境是分开的。在构建文件中,当您调用 javac 时,您需要提供类路径属性。
您可以使用其余属性在文件顶部定义类路径,如下所示:
然后通过设置
classpathref
属性在每次调用 javac 时使用它,如下所示:The eclipse classpath is separate from your ant environment. In your build file, when you call
javac
you need to supply a classpath attribute.You can define the classpath at the top of the file with the rest of your properties, like this:
and then use it in each call to javac by setting the
classpathref
attribute, like this:您需要指定包含 .class 文件和外部 jar(如 junit)的目录。
例如,
这是我摘录的完整文件,以防您需要有关如何设置其他常见内容(emma、javadoc 等)的想法
You need to specify the directory that contains your .class files and your external jars (like junit).
e.g.
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)
如果观察错误堆栈,您会发现以下行,就在您提到的错误行上方...
[javac] [类文件的搜索路径:C:\Program Files\Java\jre6\lib\resource...
此行显示了类路径中可用于此 ant 目标执行的所有 jar。
你肯定不会在这里找到想要的jar,即junit-xxxjar (junit-4.8.2.jar)
现在转到eclipse ->;窗口->偏好->蚂蚁->运行时->全局条目 -> 4.8.2jar (您可以在项目 lib 目录中找到它)
添加 Jars 添加 junit - 运行时->类路径和错误堆栈中与类路径相关的错误行,您就会明白这个问题。
希望这能解决您的问题。
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...
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)
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)
If you play around the Ant -> Runtime -> classpath and the classpath related error line in the error stack, you will understand the issue.
Hope this solves your problem.