I want to define in command line what unit tests are to be run. Tests are defined in a logical structure based on Java packages. From the command line I want to define what Java packages are to be included in the test run (i.e. what tests to include).
In this way I want to loop through a tokenized list of path values, and include Java tests from these paths. However the batchtest element does not support nested for loops. How can I modify this to do what I want? So the amount of includes are as many as there are defined path values.
Updated:
I have defined target like this:
<target name="test">
<property name="path" value="**"/>
<junit fork="yes" failureproperty="true" forkmode="once">
<formatter type="xml" />
<classpath>
<pathelement path="bin.path"/>
</classpath>
<batchtest haltonerror="false" todir="${test.dir}">
<fileset dir="/src/test/">
<for list="${path}" param="path">
<sequential>
<var name="path" value="#{path}"/>
<include name="**/${path}/**/*.java" />
</sequential>
</for>
</fileset>
</batchtest>
</junit>
You could use reguler expression.
For example;
<batchtest haltonfailure="yes" todir="/test">
<fileset dir="/build/classes/"
includes="**/TestBlaBla.class" excludes="**/BlaBla.class" />
</batchtest>
I hope it helps to you.
Related
I'm using Jacoco code coverage in my Ant build and build is success after using instrument for my junit classes as they have powermockito in it.
I'm getting errors in the junit classes when i ran through the ant and tests failed but build success.
I'm comipiling the src and test classes using WAS server jars from another build.xml and placed the class files in the dir.build folder. This folder is given as input for the instrument task to give me the instrumented classes. When the junit task is started it is saying that errors occured in the test classes and test failed. I used the printsummary ="on" and tried to generate logs using verbose.
I want to know what are the errors occuring while running the junit. Can somebody please tell me how can i see the errors in the Test classes.
<?xml version="1.0" encoding="UTF-8"?>
<project name = "JunitIntegration" default = "report" xmlns:jacoco="antlib:org.jacoco.ant">
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
<classpath path="./ant/lib/jacocoant.jar"/>
</taskdef>
<property name= "project.name" value = "Services"/>
<property name= "source.dir" value = "C:\\Thejesh\\Workspaces\\firstSetup\\Services"/>
<property name= "dir.build" value = "C:\\Thejesh\\Workspaces\\edpm_firstSetup\\build\\Services-classes"/>
<!-- Code coverage report -->
<property name= "result.dir" value = "C:\\Thejesh\\Workspaces\\edpm_firstSetup\\junitResults"/>
<property name= "result.classes.instr.dir" value = "${result.dir}/classes-instr"/>
<property name= "result.report.dir" value = "${result.dir}/site/jacoco"/>
<property name= "result.exec.file" value = "${result.dir}/jacoco.exec"/>
<import file="build-utils.xml"/>
<target name ="cleanL">
<delete dir ="${result.dir}"/>
</target>
<target name ="createL">
<mkdir dir ="${result.dir}"/>
<mkdir dir ="${result.classes.instr.dir}"/>
</target>
<target name ="instrument" depends = "cleanL, createL">
<jacoco:instrument destdir="${result.classes.instr.dir}">
<fileset dir="${dir.build}"/>
</jacoco:instrument>
</target>
<target name ="test" depends = "instrument">
<record name="${result.dir}/loggerinfo.log" loglevel="verbose" action="start"/>
<jacoco:coverage destfile="${result.exec.file}" xmlns:jacoco="antlib:org.jacoco.ant"
exclclassloader = "sun.reflect.DelegatingClassLoader">
<junit fork="true" forkmode="once" printsummary="on">
<formatter type= "xml"/>
<classpath>
<pathelement path="${dir.build}" />
<pathelement path="./ant/lib/jacocoagent.jar" />
<fileset dir ="C:\\Thejesh\\PIF\\Jars\\Common">
<include name ="*.jar*" />
</fileset>
</classpath>
<batchtest todir ="${result.dir}">
<fileset dir= "${result.classes.instr.dir}">
<include name ="**/*Test.class*"/>
</fileset>
</batchtest>
</junit>
</jacoco:coverage>
<record name="${result.dir}/loggerinfo.log" loglevel="verbose" action="stop"/>
</target>
<!-- Generating code coverage reports -->
<target name ="report" depends ="test">
<jacoco:report>
<executiondata>
<file file="${result.exec.file}"/>
</executiondata>
<structure name="JUnit intergration report">
<classfiles>
<fileset dir="${dir.build}"/>
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${source.dir}"/>
</sourcefiles>
</structure>
<html destdir="${result.report.dir}"/>
<xml destfile ="${result.report.dir}/report.xml"/>
</jacoco:report>
</target>
<target name ="compiler" >
<junit.compile
projectdir="${source.dir}"
destfile="${dir.build}"
/>
</target>
</project>
One way that may help you. Hopefully someone can suggest an easier way - this is snippet from build.xml I used a few years ago before upgrade to JUNIT5.
<junit printsummary="on" showoutput="off" fork="true"
haltonerror="false" haltonfailure="false"
errorproperty="junit.errors" failureproperty="junit.failures" >
<formatter type="plain"/>
<formatter type="xml"/>
You'd need to check what outout files you get from JUNIT4 in your ${result.dir} and add ant action to grab and print out part of the junit output files to the console so that the final error lines were visible
<loadfile srcfile="${junit.stdout}" property="junit.summary">
<filterchain><tailfilter lines="30"/></filterchain>
</loadfile>
<echo>${junit.summary}</echo>
and then bail out of the build if there are errors:
<fail if="junit.failures">JUNIT failed - see ${result.dir}</fail>
<fail if="junit.errors" >JUNIT error - see ${result.dir}</fail>
I have a simple Junit test that keeps throwing a java.lang.ClassNotFoundException. Below is the folder structure
project:
|__build.xml
|__hamcrest-core-1.3.jar
|__junit.jar
|__src:
| |__Simple.java
|__bin:
|__Simple.class
Simple.java
import org.junit.Test;
public class Simple {
#Test
public void check() {
assert (true);
}
}
build.xml
<?xml version="1.0"?>
<project name="Ant-Test" default="main">
<target name="main">
<junit printsummary="yes" fork="yes" haltonfailure="no">
<classpath path="hamcrest-core-1.3.jar" />
<classpath path="junit.jar" />
<formatter type="plain" />
<batchtest fork="yes" todir="docs">
<fileset dir="bin">
<include name="**.class" />
</fileset>
</batchtest>
</junit>
</target>
</project>
Error:
Testsuite: Simple
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
Caused an ERROR
Simple
java.lang.ClassNotFoundException: Simple
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:292)
Please help me find the reason for why this error occurs. The script is able to find the test case but at the end an error is thrown.
Add the required resources to the test classpath:
<pathelement path="${classpath}"/>
that in your case will be:
<classpath>
<pathelement path="bin" />
<pathelement location="hamcrest-core-1.3.jar" />
<pathelement location="junit.jar" />
</classpath>
As explained here
To make things a bit more flexible, we add one extra feature, which makes it possible to match multiple directory levels. This can be used to match a complete directory tree, or a file anywhere in the directory tree. To do this, "" must be used as the name of a directory. When ** is used as the name of a directory in the pattern, it matches zero or more directories. For example: /test/ matches all files/directories under /test/, such as /test/x.java, or /test/foo/bar/xyz.html, but not /xyz.xml.
'**' must be used to match folders, and not as wildcard to select partial file names.
Change your ant with:
<fileset dir="bin">
<include name="*.class" />
</fileset>
If you want to select all '*.class' files under every directory under bin/ then use:
<fileset dir="bin">
<include name="**/*.class" />
</fileset>
You will have now an error cause by the test report writer. You need to create a docs folder and everything will work.
The final build.xml will be:
<?xml version="1.0"?>
<project name="Ant-Test" default="main">
<target name="main">
<mkdir dir="docs"/>
<junit printsummary="yes" fork="yes" haltonfailure="no">
<classpath location="hamcrest-core-1.3.jar" />
<classpath location="junit-4.12.jar" />
<classpath path="bin/" />
<formatter type="plain" />
<batchtest fork="yes" todir="docs">
<fileset dir="bin">
<include name="**/*.class" />
</fileset>
</batchtest>
</junit>
</target>
</project>
Please try with following fileset tag:
<fileset dir="bin">
<include name="**.java" />
</fileset>
I'm not sure if junit task admits more than one classpath node, but anyway, it is a good practice to have just one. Moreover, your classpath is missing the classes directory. The right declaration would be like this:
<junit ...>
<classpath>
<pathelement path="bin"/>
<pathelement location="hamcrest-core-1.3.jar"/>
<pathelement location="junit.jar"/>
</classpath>
...
</junit>
I am trying to migrate my test suite from Junit4 to Junit5. Have a bunch of System Properties given as parameters in the older targets which run tests on Junit4 but now as i am migrating to Junit5, JunitLauncher doesn't support this parameter .
Older Target which runs tests on Junit4:
<target name="test">
<mkdir dir="${junit_reports.dir}" />
<junit printsummary="${junit.printsummary}" haltonfailure="${junit.haltonfailure}" haltonerror="${junit.haltonerror}" showoutput="${junit.showoutput}" fork="true" forkmode="once" failureProperty="failed">
<sysproperty key="clover.initstring" value="${clover.dbdir}/${clover.dbfile}" />
<sysproperty key="rules.location" value="${classes.dir}/rules/impl" />
<classpath>
<path refid="classes.classpath" />
<path refid="test.classpath" />
<pathelement path="${basedir}/../../.." />
<pathelement path="${test.classes.dir}" />
<path location="${basedir}/../common/target/test_classes" />
<pathelement location="${3rdparty.dir}/prime-server-framework/framework-core-mock.jar" />
</classpath>
<formatter type="${unittest.output.type}" />
<batchtest fork="true" todir="${junit_reports.dir}">
<fileset dir="${test.classes.dir}" includes="${tests.patternset}" />
</batchtest>
</junit>
</target>
New Target which runs tests on Junit5:
<target name = "sampletest">
<mkdir dir="${junit_reports.dir}" />
<junitlauncher>
<classpath>
<path refid="classes.classpath" />
<path refid="test.classpath" />
<pathelement path="${basedir}/../../.." />
<pathelement path="${test.classes.dir}" />
<path location="${basedir}/../common/target/test_classes" />
</classpath>
<!--<testclasses outputdir="${junit_reports.dir}">
<fileset dir="${test.classes.dir}">
<include name = "**/*Test.class"/>
</fileset>
</testclasses>-->
<test name = "impl.RulesEngineValidationTest"/>
</junitlauncher>
</target>
How do i give system properties in new target?
Ant 1.10.4 does support JUnit 5. However, it does not support all the features that Ant integration JUnit 4 does. In particular, it does not support forking the junit process and therefore passing system properties.
I found this question because I was trying to do the same thing. I found a workaround though. You can set the system properties in code before calling junitlauncher.
This code is what I used to set a single system property for file encoding. You could do something similar for your properties.
<script language="javascript">
<![CDATA[
var imports = new JavaImporter(java.lang.System);
imports.System.setProperty('file.encoding', 'ISO8859_1')
]]>
</script>
Yours is a little more complicated since your properties use others. You can read an Ant variable from inside the code. (I don't know how to read one with a dot in the name so I got rid of the dot in this example)
<property name="cloverdbdir" value="clover-dir-property-value" />
<property name="cloverdbfile" value="clover-db-file-property-value" />
<script language="javascript">
<![CDATA[
var imports = new JavaImporter(java.lang.System);
imports.System.setProperty('clover.initstring', cloverdbdir + '/' + cloverdbfile);
print(imports.System.getProperty('clover.initstring'));
]]>
</script>
There are a few things to be aware of if you use this technique:
Nashorn is deprecated for removal. It is definitely in Java 11. However, it isn't guaranteed to all future versions. It seems likely that Ant will add the system property functionality natively by then so I'm not worried about it.
The system property remains set for the remainder of the build. This doesn't look like a problem for you. If it is, you'd need another script block after calling JUnit to null it out.
I have a java project that has just been imported into TFS and I've been trying to get TFS to output the results of the unit tests.
At the end of the TFSBuild.proj file I have the following:
<ItemGroup>
<!-- Ant Call Configuration.
The build file called should be included in the workspace of the build definition.
-->
<AntBuildFile Include="$/PROJECT_NAME/Main/tfsbuild.xml">
<Targets>build,test</Targets>
<Properties>BinariesRoot=$(BinariesRoot);BuildDefinitionName=$(BuildDefinitionName);BuildDefinitionUri=$(BuildDefinitionUri);BuildDirectory=$(BuildDirectory);BuildNumber=$(BuildNumber);DropLocation=$(DropLocation);LogLocation=$(LogLocation);SourceGetVersion=$(SourceGetVersion);TestResultsRoot=$(TestResultsRoot);TeamProject=$(TeamProject);WorkspaceName=$(WorkspaceName);WorkspaceOwner=$(WorkspaceOwner)</Properties>
<Lib></Lib>
</AntBuildFile>
<!-- JUnit XML Results files should be created using the XML formatter
and be located in the following path
-->
<JUnitLogFiles Include="$(BinariesRoot)\**\TEST-*.xml" />
</ItemGroup>
This kicks off the build and tells TFS where to find the junit test results.
The issue is, TFS is not finding the unit test results even though I can see through the logs that the tests were run.
I almost gave up on this, and changed my ant file to produce a junit report and store it with the build artifacts. I changed my ant task to be:
<target name="test" depends="compile-tests">
<echo>Running unit tests, output should be in ${junit.output}</echo>
<junit printsummary="yes">
<classpath>
<pathelement path="${compile.classpath}" />
<pathelement path="${lib.dir}/junit-4.0.jar" />
<pathelement path="${build}" />
<pathelement path="${dist-classes}" />
</classpath>
<formatter type="xml" />
<batchtest fork="yes" todir="${junit.output}">
<fileset dir="${src.test}">
<include name="**/*Test.java" />
</fileset>
</batchtest>
</junit>
<mkdir dir="${DropLocation}/${BuildNumber}/test_results" />
<junitreport todir="${junit.output}">
<fileset dir="${junit.output}">
<include name="TEST-*.xml" />
</fileset>
<report todir="${DropLocation}/${BuildNumber}/test_results" />
</junitreport>
</target>
But after checking the output from the next build, I realised that the JUnit output was saved as TESTS-TestSuites.xml, not TEST-*.xml. I changed my TFSBuild.proj file accordingly, and now have the build results appearing in TFS.
Somehow the junitreport task still picks up the output though.
I'm trying to make an ant target that only runs the JUnit tests on a project without any other prior actions (no depends). I'm using Emma to instrument these in another target, and then I have another target that does a bytecode mutation on these instrumented classes. All that is working and I can get JUnit to run in that target after I've performed the instrumentation/mutation steps but I need to have the ability to just run the JUnit tests separately from this compile-instrument-mutate chain.
I've build a target that looks like this:
<target name="mutant-test-run" description="Run tests with mutation applied">
<path id="run.classpath">
<pathelement location="${out.dir}" />
</path>
<mkdir dir="${reports}" />
<junit printsummary="yes" fork="true">
<classpath>
<pathelement location="${out.instr.dir}" />
<path refid="junit.classpath" />
<path refid="famaLib.classpath" />
<path refid="run.classpath" />
<path refid="emma.lib" />
</classpath>
<jvmarg value="-Demma.coverage.out.file=${coverage.dir}/coverage.emma" />
<jvmarg value="-Demma.coverage.out.merge=true" />
<formatter type="plain" />
<formatter type="xml" />
<batchtest todir="${reports}">
<fileset dir="${src.dir}">
<include name="test1.java" />
<include name="test2.java" />
<include name="test3.java" />
<include name="test4.java" />
<include name="test5.java" />
</fileset>
</batchtest>
</junit>
<emma enabled="${emma.enabled}">
<report sourcepath="${src.dir}" sort="+block,+name,+method,+class" metrics="method:70,block:80,line:80,class:100">
<fileset dir="${coverage.dir}">
<include name="*.emma" />
</fileset>
<!-- for every type of report desired, configure a nested
element; various report parameters can be inherited from the parent <report>
and individually overridden for each report type:-->
<txt outfile="${coverage.dir}/coverage.txt" depth="package" columns="class,method,block,line,name" />
<xml outfile="${coverage.dir}/coverage.xml" depth="package" />
<html outfile="${coverage.dir}/coverage.html" depth="method" columns="name,class,method,block,line" />
</report>
</emma>
</target>
The jUnit task within the target doesn't get executed however, all I get is the output from the mkdir task and the emma task.
m1> ${ANT_HOME}/bin/ant -f nnbuild.xml -verbose mutant-test-run
Apache Ant(TM) version 1.8.2 compiled on July 5 2011
Buildfile: (Elided path here)/nnbuild.xml
Detected Java version: 1.6 in: /usr/lib64/jvm/java-1.6.0-sun-1.6.0/jre
Detected OS: Linux
parsing buildfile (Elided path)/nnbuild.xml with URI = file:(Elided path)/nnbuild.xml
Project base dir set to: (Elided path)
parsing buildfile jar:file:(Elided ANT_HOME path)/ant/lib/ant.jar!(Elided ANT_HOME path)/ant/antlib.xml with URI = jar:file:(Elided ANT_HOME path)/ant/lib/ant.jar!/org/apache/tools/ant/antlib.xml from a zip file
Build sequence for target(s) `mutant-test-run' is [mutant-test-run]
Complete build sequence is [mutant-test-run, emma, clean, init, compile, run, emma-run, merge, all, sofya, ]
mutant-test-run:
[mkdir] Skipping (Elided path)/reports because it already exists.
[emma] [EMMA v2.0, build 5312 (2005/06/12 19:32:43)]
[report] processing input files ...
[report] 1 file(s) read and merged in 48 ms
[report] nothing to do: no runtime coverage data found in any of the data files
BUILD SUCCESSFUL
Total time: 0 seconds
m1>
How do you setup an ant target to only do JUnit tests?
It looks like your fileset is wrong for the batchtest element, I think maybe you need to include a **:
<batchtest todir="${reports}">
<fileset dir="${src.dir}">
<include name="test1.java" />
<include name="test2.java" />
<include name="test3.java" />
<include name="test4.java" />
<include name="test5.java" />
</fileset>
</batchtest>
From the examples on the page JUnit Task, you can use:
<batchtest fork="yes" todir="${reports.tests}">
<fileset dir="${src.tests}">
<include name="**/*Test*.java"/>
<exclude name="**/AllTests.java"/>
</fileset>
</batchtest>
Where the ** means in any sub-directory.
Another example:
<batchtest todir="${collector.dir}" unless="hasFailingTests">
<fileset dir="${collector.dir}" includes="**/*.java" excludes="**/${collector.class}.*"/>
</batchtest>