I have JUnit tests on my project that run correctly with Eclipse.
So, now I try to integrate these tests with an ant task. To make that I make the following ant script :
<path id="classpath-test">
<pathelement path="." />
<pathelement path="${classes.home}" />
<fileset dir="${lib.home}" includes="*.jar" />
<fileset dir="${libtest.home}" includes="*.jar" />
</path>
<target name="compile" ... > // compiles src code of the project
<target name="compile-tests" depends="compile">
<javac srcdir="${test.home}"
destdir="${testclasses.home}"
target="1.5"
source="1.5"
debug="true"
>
<classpath refid="classpath-test" />
</javac>
<copy todir="${testclasses.home}">
<fileset dir="${test.home}">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="unit-test" depends="compile-tests">
<junit printsummary="false" fork="off" haltonfailure="true">
<classpath refid="classpath-test" />
<formatter type="brief" usefile="false" />
<test name="com.test.MyTest" />
<!--<batchtest todir="${reports.dir}" >
<fileset dir="${testclasses.home}" >
<exclude name="**/AllTests*"/>
<include name="**/*Test.class" />
</fileset>
</batchtest>-->
</junit>
</target>
The directory ${libtest.hom} contains junit-4.8.1.jar and hamcrest-core-1.1.jar.
When I launch the following command : ant unit-test, the execution of the MyTest fails with the following output :
unit-test:
[junit] Testsuite: com.test.MyTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit]
[junit] Null Test: Caused an ERROR
[junit] com.test.MyTest
[junit] java.lang.ClassNotFoundException: com.test.MyTest
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
[junit] at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:247)
[junit]
[junit]
It's very strange because com.test.MyTest is well in the classpath pointed for my task junit in ant script. Someone would have and idea to solve this problem ?
Thanks for your help.
Sylvain.
The ${testclasses.home} directory is nowhere on a classpath for <junit> task.
I think this is where class file for com.test.MyTest lives.
Here is modified unit-test target:
<target name="unit-test" depends="compile-tests">
<junit printsummary="false" fork="off" haltonfailure="true">
<classpath>
<path refid="classpath-test"/>
<fileset dir="${testclasses.home}"/>
</classpath>
<formatter type="brief" usefile="false" />
<test name="com.test.MyTest" />
<!--<batchtest todir="${reports.dir}" >
<fileset dir="${testclasses.home}" >
<exclude name="**/AllTests*"/>
<include name="**/*Test.class" />
</fileset>
</batchtest>-->
</junit>
</target>
Related
Here is my code:
<project name="SampleProject" default="test-html" basedir=".">
<property name="ws.home" value="${basedir}" />
<property name="ws.jars" value="E:/jars" />
<property name="classes" value="${ws.home}/build" />
<property name="test.src" value="${ws.home}/src" />
<property name="test.reportsDir" value="c:/report" />
<!-- classpath -->
<path id="test.classpath">
<fileset dir="${ws.jars}" includes="*.jar" />
</path>
<!-- clean -->
<target name="clean" description="clean up">
<delete dir="${classes}" />
<echo message="Directory deleted..." />
</target>
<!-- create -->
<target name="create" description="create">
<mkdir dir="${classes}" />
<echo message="Directory created..." />
</target>
<!-- compile -->
<target name="compile" depends="clean,create">
<echo message="compiling..." />
<javac debug="true" srcdir="${test.src}" destdir="${classes}"
includeantruntime="false" classpathref="test.classpath" />
<echo message="compilation completed..." />
</target>
<!-- generating HTML-Report -->
<target name="test-html" depends="compile">
<junit fork="yes" printsummary="yes" haltonfailure="no">
<classpath refid="test.classpath" />
<batchtest fork="yes" todir="${test.reportsDir}">
<fileset dir="${classes}">
<include name="testcases/FirstTestCase.class" />
</fileset>
</batchtest>
<formatter type="xml" />
<classpath refid="test.classpath" />
</junit>
<junitreport todir="${test.reportsDir}">
<fileset dir="${test.reportsDir}">
<include name="TEST-*.xml" />
</fileset>
<report todir="${test.reportsDir}" />
</junitreport>
</target>
</project>
I am getting following problem in report generated with command prompt:
testcases.FirstTestCase
java.lang.ClassNotFoundException: testcases.FirstTestCase
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
And I am getting following error when the run build.xml in Eclipse:
Buildfile: E:\JLC REVISION\ANT WITH JUNIT\SampleProject\build.xml
clean:
[delete] Deleting directory E:\JLC REVISION\ANT WITH JUNIT\SampleProject\build
[echo] Directory deleted...
create:
[mkdir] Created dir: E:\JLC REVISION\ANT WITH JUNIT\SampleProject\build
[echo] Directory created...
compile:
[echo] compiling...
[javac] Compiling 1 source file to E:\JLC REVISION\ANT WITH JUNIT\SampleProject\build
[echo] compilation completed...
test-html:
[junit] Running testcases.FirstTestCase
[junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
[junit] Test testcases.FirstTestCase FAILED
[junitreport] Processing c:\report\TESTS-TestSuites.xml to C:\DOCUME~1\MUKESH~1\LOCALS~1\Temp\null1997459403
[junitreport] Loading stylesheet jar:file:/C:/Student%20Dvd/Student%20DVD/ANT/apache-ant-1.9.4-bin/apache-ant-1.9.4/lib/ant-junit.jar!/org/apache/tools/ant/taskdefs/optional/junit/xsl/junit-frames.xsl
[junitreport] Transform time: 2172ms
[junitreport] Deleting: C:\DOCUME~1\MUKESH~1\LOCALS~1\Temp\null1997459403
BUILD SUCCESSFUL
Total time: 5 seconds
I tried a lot to get out from here, but I didn't get solution. Help me.
It looks like the classes you compile are not on your test classpath.
The javac compiles to ${classes}, but that dir is not included in your definition of test.classpath.
Try adding the dir as a pathelement:
<path id="test.classpath">
<fileset dir="${ws.jars}" includes="*.jar" />
<pathelement path="${classes}" />
</path>
I'm using the junit ant target for running the junits. When I run the Junit file separately the junit ran properly. When I tried with the ant script the junit test case was not executed. It showed me the error as,
Running x.SimpleTest
[junit] Testsuite: x.SimpleTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit] Caused an ERROR
[junit] x.SimpleTest
[junit] java.lang.ClassNotFoundException: x.SimpleTest
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
[junit] at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:247)
[junit] at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
[junit] at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:424)
[junit] at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:138)
[junit] Test x.SimpleTest FAILED
But the class is in the classpath. Please help me in debugging in this.
My build file is,
<property file="build.properties" />
<path id="libirary">
<fileset dir="${lib}">
<include name="**.*jar" />
</fileset>
</path>
<path id="applicationClassPath">
<path id="classpath1" refid="libirary" />
<pathelement id="classpath2" location="${build.classes.dir}" />
</path>
<path id="application.classpath">
<pathelement path="${java.class.path}" />
</path>
<path id="class.path">
<fileset dir="${build.classes.dir}" />
</path>
<property name="classPath" refid="applicationClassPath" />
<target name="clean">
<delete dir="${build.classes.dir}" />
<delete dir="${build.dir}" />
<delete dir="${dist.dir}" />
</target>
<target name="init" depends="clean">
<mkdir dir="${build.classes.dir}" />
<mkdir dir="${dist.dir}" />
</target>
<target name="compile" depends="init">
<javac encoding="ISO-8859-1" destdir="${build.classes.dir}" srcdir="${src.dir}" debug="true" verbose="false">
<classpath>
<path refid="libirary" />
</classpath>
</javac>
</target>
<target name="jar" depends="compile">
<jar destfile="${dist.dir}/${jar.filename}.jar">
<fileset dir="${build.classes.dir}" />
<zipfileset dir="${lib}" includes="**/*.jar" />
<manifest>
<attribute name="Main-Class" value="${main.file.name}" />
</manifest>
</jar>
</target>
<target name="junit" depends="compile">
<echo message="classpath= ${classPath}" />
<junit printsummary="yes" description="true" filtertrace="yes" showoutput="yes">
<formatter type="plain" usefile="false" />
<classpath>
<path refid="applicationClassPath" />
</classpath>
<test name="x.SimpleTest" filtertrace="true">
</test>
</junit>
</target>
It is resolved now. It was due to the classpath errors. Thank you all for the response.
There is part of ant script with junit task:
...
<target name="test">
<mkdir dir="path_to_report_dir">
<junit fork="true" printsummary="true" showoutput="true" maxmemory="1024M">
<classpath ... />
<batchtest todir="path_to_report_dir">
<formatter type="xml" />
<fileset ... />
</batchtest>
</junit>
</target>
...
This script works from Eclipse and from command line. But it doesn't work in TeamCity. The last informative message in TeamCity is:
[mkdir] Created dir: path_to_report_dir
Process exit code: 0
It looks like junit task doesn't work and also it stops performting aff all script. Where is trouble in?
The cause was in <fileset> file list. The TeamCity version of Ant doesn't work with strings like "/test/" (this mean select all files recursively); it only works with strings like "**/test/*.class". The local version of Ant supports both variants.
Thanks.
Don't know if this helps.... but here's my standard test target:
<target name="test" depends="compile-tests">
<junit printsummary="yes" haltonfailure="yes">
<classpath>
<path refid="test.path"/>
<pathelement path="${classes.dir}"/>
<pathelement path="${test.classes.dir}"/>
</classpath>
<formatter type="xml"/>
<batchtest fork="yes" todir="${test.reports.dir}">
<fileset dir="${test.src.dir}">
<include name="**/*Test*.java"/>
<exclude name="**/AllTests.java"/>
</fileset>
</batchtest>
</junit>
</target>
Build output:
test:
[junit] Running org.demo.AppTest
[junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.056 sec
Notes
Using Junit 4.10.
I'm getting troubles trying to use
<dirsets>
in my junit ant.
This is the snippet of the classpath.
<target name="myTests" >
<junit haltonerror="true" haltonfailure="true" fork="true">
<classpath>
<dirset dir="/my/absolute/root/path/where/I/keep/compiled/classes">
<include name="com/mycompany/mytests"/>
</dirset>
<pathelement location="my/path/to/jars/myjar1.jar" />
<pathelement location="my/path/to/jars/myjar2.jar" />
<!-- and so on -->
</classpath>
<test name="com.mycompany.mytests.MyFirstTest"
outfile="${dir.report.test}/report_MyFirstTest">
<formatter type="xml" />
</test>
</junit>
</target>
when I launch the test, after having successfully compiled all the code, ant complains:
java.lang.ClassNotFoundException: com.mycompany.mytests.MyFirstTest
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
I tried with absolute, relative paths and it never works. My classpath consists on many jars specified with many and that that is never recognized.
Where is my fault?
thanks
I former times when I used ant I used the nested <classpath> element and specified the classpath with the path-like structure - like this:
<path id="project.test.classpath">
<pathelement location="/my/absolute/root/path/where/I/keep/compiled/classes" />
<fileset dir="/my/path/to/jars">
<include name="**/*.jar" />
</fileset>
</path>
<target name="myTests">
<junit haltonerror="true" haltonfailure="true" fork="true">
<classpath refid="project.test.classpath" />
<test name="com.mycompany.mytests.MyFirstTest" outfile="${dir.report.test}/report_MyFirstTest">
<formatter type="xml" />
</test>
</junit>
</target>
Maybe that fit's also for you.
I am trying to use ant to run junit tests and generate reports.
I am able to successfully run the tests but the report files are empty.
What am I doing wrong ?
This is my build.xml :
<project name="JunitTest" default="test" basedir=".">
<property name="testdir" location="." />
<property name="srcdir" location="." />
<property name="full-compile" value="true" />
<property name="test.reports" value="./reports" />
<path id="classpath.base"/>
<path id="classpath.test">
<pathelement location="${testdir}" />
<pathelement location="${srcdir}" />
<path refid="classpath.base" />
</path>
<target name="clean" >
<delete verbose="${full-compile}">
<fileset dir="${testdir}" includes="**/*.class" />
</delete> `
</target>
<target name="compile" depends="clean">
<javac srcdir="${srcdir}" destdir="${testdir}" verbose="${full-compile}" >
<classpath refid="classpath.test"/>
</javac>
</target>
<target name="test" depends="compile">
<junit>
<classpath refid="classpath.test" />
<formatter type="brief" usefile="false" />
<test name="com.tests.nav1" />
</junit>
<junitreport todir="${test.reports}">
<fileset dir="${test.reports}">
<include name="TEST-*.xml" />
</fileset>
<report todir="${test.reports}" />
</junitreport>
</target>
</project>
and this is the output on the console :
[junit] Using CLASSPATH C:\eclipse\eclipse-java-helios-SR1-win32\eclipse\JunitWS\SeleniumTraining\src;C:\jars\junit.jar;C:\ant\lib\ant-launcher.jar;C:\ant\lib\ant.jar;C:\ant\lib\ant-junit.jar;C:\ant\lib\ant-junit4.jar
[junit] Testsuite: com.tests.nav1
[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 48.187 sec
[junit] ------------- Standard Output ---------------
[junit] testnav2
[junit] ------------- ---------------- ---------------
[junitreport] Using class org.apache.tools.ant.taskdefs.optional.TraXLiaison
[junitreport] Processing C:\eclipse\eclipse-java-helios-SR1-win32\eclipse\JunitWS\SeleniumTraining\src\reports\TESTS-TestSuites.xml to C:\Users\pmahajan\AppData\Local\Temp\null236099757
[junitreport] Loading stylesheet jar:file:/C:/ant/lib/ant-junit.jar!/org/apache/tools/ant/taskdefs/optional/junit/xsl/junit-frames.xsl
[junitreport] Transform time: 330ms
[junitreport] Deleting: C:\Users\pmahajan\AppData\Local\Temp\null236099757
BUILD SUCCESSFUL
Total time: 49 seconds
If you look at the ant snippet, there are a few issues:
You have set usefile=false, which means no output file is created
You have set formatter type=brief, which will print detailed information only for failed tests
You need to also specify the todir - the folder where the report has to go in the <test> tag - the default is current folder. This should match the folder you are using in <junitreport> task.
You can try with the following updated <junit> section...
<junit>
<classpath refid="classpath.test" />
<formatter type="xml"/>
<test name="com.tests.nav1" todir="${test.reports}"/>
</junit>
1 <target name="test" depends="compile">
2 <junit>
3 <classpath refid="classpath.test" />
4 <formatter type="brief" usefile="false" />
5 <test name="com.tests.nav1" />
6 </junit>
7 <junitreport todir="${test.reports}">
8 <fileset dir="${test.reports}">
9 <include name="TEST-*.xml" />
10 </fileset>
11 <report todir="${test.reports}" />
12 </junitreport>
13 </target>
The above segment of your coded needs following changes.
You have to specify the to directory option in the 5th line( for example todir = ${data.reports} )
In the 8th line the directory specified must me data.reports.
The 11th line must contain the option format with the value frames (format="frames").
The ant JUnit Task doc gives this example that might help you (as it apparently does exactly what you're trying to achieve):
<junit printsummary="yes" haltonfailure="yes">
<classpath>
<pathelement location="${build.tests}"/>
<pathelement path="${java.class.path}"/>
</classpath>
<formatter type="plain"/>
<test name="my.test.TestCase" haltonfailure="no" outfile="result">
<formatter type="xml"/>
</test>
<batchtest fork="yes" todir="${reports.tests}">
<fileset dir="${src.tests}">
<include name="**/*Test*.java"/>
<exclude name="**/AllTests.java"/>
</fileset>
</batchtest>
</junit>
Runs my.test.TestCase in the same VM, ignoring the given CLASSPATH; only a warning is printed if this test fails. In addition to the plain text test results, for this test a XML result will be output to result.xml. Then, for each matching file in the directory defined for ${src.tests} a test is run in a separate VM. If a test fails, the build process is aborted. Results are collected in files named TEST-name.txt and written to ${reports.tests}.
It is specified in the doc that printsummary can take values on and off, but they're using yes in the example which is on the same page, so I guess it's accepted too.
Please try formatter with "xml"
<formatter type="${junit.format}"/>
where junit.format is property with appropriate value.