We have a large project and executing all our unit tests takes a lot of time. We are also using Ant. What strategies could be used to execute only a subset of our tests using Ant? We have our tests in groups of logical java packages, so that could be of help...
You should use batchtest tag of the junit ant task. Check here : http://ant.apache.org/manual/Tasks/junit.html
A simplified example from that link is given below:
<junit printsummary="yes" haltonfailure="yes">
<classpath>
<pathelement location="${build.tests}"/>
<pathelement path="${java.class.path}"/>
</classpath>
<formatter type="plain"/>
<batchtest fork="yes" todir="${reports.tests}">
<fileset dir="${src.tests}">
<include name="**/*Test*.java"/>
<exclude name="**/AllTests.java"/>
</fileset>
</batchtest>
</junit>
Related
I have seen a few links which appears to imply that JUnit tests can be executed from the .java file instead of the .class
For example
<junit printsummary="yes" haltonfailure="yes" haltonerror="yes">
<classpath refid="ui.tests.classpath"/>
<formatter type="xml"/>
<batchtest todir="${env.WORKSPACE}/UITests/output">
<fileset dir="${ui.tests.classes}">
<include name="**/*Test.java"/>
</fileset>
</batchtest>
</junit>
Instead of
<junit printsummary="yes" haltonfailure="yes" haltonerror="yes">
<classpath refid="ui.tests.classpath"/>
<formatter type="xml"/>
<batchtest todir="${env.WORKSPACE}/UITests/output">
<fileset dir="${ui.tests.classes}">
<include name="**/*Test.class"/>
</fileset>
</batchtest>
</junit>
Is the first example a valid case? I could not get it working due to ClassNotFoundExceptions
I agree with the comment above, that it looks like the tag allows for either .java or .class.
I did a small test and when I ran a test with this setup - where the dir=somefolder, then using <include name="**/*Test.java"/> when the folder pointed at contains class files, then Ant will basically have an empty fileset to process for *Test.java, but when using <include name="**/*Test.class"/> then the fileset was not empty and the test cases will get run.
This was the result of my quick test. As far as I could tell, it looks like you need to specify the *Test.class to pickup the test cases.
I m using cobertura-1.9.4.1 to generate code coverage reports.First I set the classpath to cobertura.jar and to other jars in the lib folder. Then I execute cobertura-instrument.sh.
But on executing I get the error loaded information on 0 classes . I m giving the complete path to the compiled classes still it is unable to instrument the classes .
So, what am I missing or what could be the possible reasons for this.
Do you mean the error is during the instrumentation, or that after running your tests, the coverage still shows zero?
Here's an example of instrumentation (with Ant):
<target name="--coverage.instrument">
<delete file="cobertura.ser"/>
<mkdir dir="${coverage.instrumented.dir}"/>
<cobertura-instrument todir="${coverage.instrumented.dir}">
<fileset dir="${classes.main.dir}">
<include name="**/*.class"/>
<exclude name="**/*Test.class"/>
</fileset>
</cobertura-instrument>
</target>
Don't forget that you need this sysproperty when testing (eg in Ant Junit task):
<sysproperty key="net.sourceforge.cobertura.datafile" file="cobertura.ser"/>
Once Cobertura is set up an instrumentation has happened, an example of execution:
<target name="--test.unit">
<mkdir dir="${temp.dir}/unit-tests"/>
<junit forkmode="perBatch" printsummary="yes" haltonfailure="no" haltonerror="no"
failureproperty="unit.tests.failed">
<sysproperty key="net.sourceforge.cobertura.datafile" file="cobertura.ser"/>
<classpath refid="classpath.test.utest"/>
<formatter type="xml"/>
<batchtest fork="yes" todir="${temp.dir}/unit-tests">
<fileset dir="${java.src.utest.dir}" includes="**/*Test.java"/>
</batchtest>
</junit>
</target>
I believe that recent versions of Cobertura don't work well with JDK5. Strongly suggest upgrading the JDK.
I have a directory with a bunch of JUnit tests in my project. So far I have used separate target for each unit test. For example:
<target name="MyTest">
<mkdir dir="${junit.output.dir}"/>
<junit fork="yes" printsummary="withOutAndErr">
<formatter type="xml"/>
<test name="tests.MyTest" todir="${junit.output.dir}"/>
<classpath refid="MyProject.classpath"/>
</junit>
</target>
This method requires me to change build file every time I add a Unit test.
I want to able able to to run all unit tests in the project with a single Ant builder target. Is it possible to do?
Yep it is, you need to look at the fileset tag, e.g:
<junit printsummary="yes" haltonfailure="yes">
<classpath>
<pathelement location="${build.tests}"/>
<pathelement path="${MyProject.classpath}"/>
</classpath>
<formatter type="xml"/>
<batchtest fork="yes" todir="${reports.tests}">
<fileset dir="${src.tests}">
<include name="**/*Test*.java"/>
<exclude name="**/AllTests.java"/>
</fileset>
</batchtest>
</junit>
The important part is the use of fileset and a glob/wildcard pattern to match the names of the tests. Full docs on the junit task with examples here:
http://ant.apache.org/manual/Tasks/junit.html
Yep! We do it using an ant command batchtest. Looks like this:
<batchtest todir="${junit.report.dir}">
<fileset dir="${basedir}\test\unit">
<include name="**/*Test.java" />
</fileset>
</batchtest>
Google it, it should sort you out
I have a number of tests failing in the following JUnit Task.
<target name="test-main" depends="build.modules" description="Main Integration/Unit tests">
<junit fork="yes"
description="Main Integration/Unit Tests"
showoutput="true"
printsummary="true"
outputtoformatters="true">
<classpath refid="test-main.runtime.classpath"/>
<batchtest filtertrace="false" todir="${basedir}">
<fileset dir="${basedir}" includes="**/*Test.class" excludes="**/*MapSimulationTest.class"/>
</batchtest>
</junit>
</target>
How do I tell Junit to ouput the errors for each test so that I can look at the stack trace and debug the issues.
You'll need to add the formatter task as a child of the batchtest task (NOT as the immediate child of the junit task)
The syntax of formatter is:
<formatter type="plain" usefile="false"/>
type can be one of plain, brief, xml or failure.
usefile="false" asks Ant to send output to the console.
Scroll down to the h4 on "formatters" at http://ant.apache.org/manual/Tasks/junit.html for more details.
The answer was to add the tag within the tag.
<target name="test-main" depends="build.modules" description="Main Integration/Unit tests">
<junit fork="yes"
description="Main Integration/Unit Tests"
showoutput="true"
printsummary="true"
outputtoformatters="true">
<classpath refid="test-main.runtime.classpath"/>
<batchtest filtertrace="false">
<fileset dir="${basedir}/out/test/common" includes="**/*Test.class" excludes="**/*MapSimulationTest.class"/>
<fileset dir="${basedir}/out/test/test-simulation" includes="**/*Test.class" excludes="**/*MapSimulationTest.class"/>
</batchtest>
<formatter type="brief" usefile="false"/>
</junit>
</target>
I am writing a web application which should be tested with JUnit framework. So please suggest me how we can use JUnit in Jsp and servlet and also how to generate test case reports using Ant?? Thanks in advance
Why cant we use Cactus? I have heard about that and what it differs from other test cases?
For servlets I use the spring framework mock classes - there are mock request, response, servlet context, etc. You do not need to use the spring framework in your application to use them.
Regarding your second question, i think what you are looking for is the junitreport Ant task. Here is a sample (taken from here):
<target name="junit" description="Runs the unit tests" depends="jar">
<delete dir="${junit.out.dir.xml}"/>
<mkdir dir="${junit.out.dir.xml}"/>
<junit printsummary="yes" haltonfailure="no">
<classpath refid="classpath.test"/>
<formatter type="xml"/>
<batchtest fork="yes" todir="${junit.out.dir.xml}">
<fileset dir="${src.dir}" includes="**/*Test.java"/>
</batchtest>
</junit>
</target>
<target name="junitreport" description="Create a report for the rest result">
<mkdir dir="${junit.out.dir.html}"/>
<junitreport todir="${junit.out.dir.html}">
<fileset dir="${junit.out.dir.xml}">
<include name="*.xml"/>
</fileset>
<report format="frames" todir="${junit.out.dir.html}"/>
</junitreport>
</target>
Yes, cactus is an good option.. it is mainly for Integration testing and can also satisfy the Unit testing. For details u just try this site http://jakarta.apache.org/cactus