Junit Test Not Deploying from Ant Script - ant

I have an ant script that I am trying to run my junit test from. It runs all but the junit portion of the ant with no errors. Anyone know why it's not hitting this portion of my script?
<?xml version="1.0"?>
<property file="build.properties" />
<property name="test.class.name" value="edu.psu.ist.probability.TestProbability" />
<path id="test.classpath">
<pathelement location="${classes}" />
<pathelement location="/lib/junit.jar" />
<fileset dir="${lib}">
<include name="**/*.jar"/>
</fileset>
</path>
<taskdef resource="checkstyletask.properties" classpath="${checkstyle}" />
<taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask">
<classpath>
<pathelement path="${findbugs.jar}" />
</classpath>
</taskdef>
<!--
Encapsulates the process of starting up program.
-->
<target name="run" depends="build">
<echo>-----------RUN-----------</echo>
<java classname="${main}" fork="true">
<!-- fork needed for running on OS X -->
<classpath>
<pathelement location="${jar}" />
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
</classpath>
</java>
</target>
<!--
Encapsulates the process of packaging (jarring) program.
-->
<target name="build" depends="document">
<echo>---------BUILD--------</echo>
<jar destfile="${jar}" basedir="${classes}" />
</target>
<!--
Encapsulates the process of generating javadoc for program.
-->
<target name="document" depends="findbugs">
<echo>-------DOC--------</echo>
<javadoc sourcepath="${src}" defaultexcludes="yes" destdir="${docs}" executable="C:\Program Files (x86)\Java\jdk1.6.0_20\bin\javadoc.exe" author="true" version="true" use="true" windowtitle="Decisions API">
</javadoc>
</target>
<!--
Encapsulates the process of executing findbugs against program.
-->
<target name="findbugs" depends="style">
<echo>----------FINDBUGS----------</echo>
<findbugs home="${findbugs}" output="html" outputFile="${findbugs.output}" failOnError="true" stylesheet="fancy.xsl">
<auxClasspath>
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
</auxClasspath>
<sourcePath path="${src}"/>
<class location="${classes}" />
</findbugs>
</target>
<!--
Encapsulates the process of style-checking program.
-->
<target name="style" depends="compile">
<echo>---STYLE---</echo>
<checkstyle config="${checkstyleconfig}" classpath="${classes}">
<fileset dir="${src}" includes="**/*.java"
excludes="**/DecisionsProgram.java, **/*Test.java" />
</checkstyle>
</target>
<!--
Encapsulates the process of compiling program.
-->
<target name="compile" depends="clean">
<echo>------------COMPILE--------</echo>
<javac destdir="${classes}">
<src path="${src}" />
<classpath>
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
</classpath>
</javac>
</target>
<!--
Encapsulates the process of cleaning up program directory structure.
-->
<target name="clean">
<!-- make sure dirs exist first, to prevent error -->
<mkdir dir="${classes}" />
<mkdir dir="${dist}" />
<mkdir dir="${docs}" />
<mkdir dir="${reports}" />
<!-- now delete them -->
<delete dir="${classes}" />
<delete dir="${dist}" />
<delete dir="${docs}" />
<delete dir="${reports}" />
<!-- now recreate them so they can be used -->
<mkdir dir="${classes}" />
<mkdir dir="${dist}" />
<mkdir dir="${docs}" />
<mkdir dir="${reports}" />
</target>
<target name="test" depends="compile">
<echo>-------JUNIT----------</echo>
<junit fork="yes" haltonfailure="yes">
<test name="${test.class.name}" />
<formatter type="plain" usefile="false" />
<classpath refid="test.classpath" />
</junit>
</target>

There is no target which is invoking "test".
One possibility is to replace
<target name="style" depends="compile">
with
<target name="style" depends="test">

Related

failed to create task or type testng

This is my build.xml file. I am trying to call testNG.xml file to execute it from build.xml but i am getting below error.
<property name="bin.dir" value="${basedir}/bin" />
<property name="lib.dir" value="${basedir}/lib" />
<property name="src.dir" value="${basedir}/src" />
<property name="res.dir" value="${basedir}/resources" />
<property name="server.dir" value="${basedir}/server" />
<path id="seleniumautomation.classpath">
<!-- <pathelement path="${lib.dir}" /> -->
<fileset dir="${lib.dir}">
<include name="*.jar" />
<include name="**/*.jar" />
</fileset>
<!--<fileset dir="${basedir}/server"> <include name="*.jar" /> <include
name="**/*.jar" /> </fileset> -->
</path>
<target name="clean">
<echo>Clean data</echo>
<delete failonerror="false" dir="lib"/>
</target>
<target name="create" depends="clean">
<echo>Creating directory.</echo>
<mkdir dir="lib"/>
</target>
<target name="copy" depends="create">
<echo>Coping jars</echo>
<copy todir="lib" overwrite="true">
<fileset dir="C:\backup\ToolsQA\ProjectThree\JarFiles" includes="**/*.jar" id="id" >
</fileset>
</copy>
</target>
<target name="compile" depends="copy">
<javac classpathref="seleniumautomation.classpath" includeantruntime="true" srcdir="src" destdir="bin" includes="**/*.java" verbose="true" >
</javac>
<echo>Java file compiled Successfully.</echo>
</target>
<target name="runtests" depends="compile">
<echo>ABCFD</echo>
<testng classpathref="seleniumautomation.classpath" useDefaultListeners="true">
<echo>2431234ABCFD</echo>
<xmlfileset dir="${basedir}" includes="TestNG.xml" />
</testng>
</target>
BUILD FAILED C:\backup\ToolsQA\ProjectThree\Build.xml:65: Problem:
failed to create task or type testng Cause: The name is undefined.
Action: Check the spelling. Action: Check that any custom tasks/types
have been declared. Action: Check that any /
declarations have taken place.
Please suggest....
You are missing taskdef tag like:
<taskdef resource="testngtasks" classpath="<Path where testng jar is in >/testng.jar"/>

Execute selenium test suit jar using ant

I have a Selenium RC testing project that uses JUNIT test cases. I have made a JAR file of it and I am executing that JAR through ANT build file:
My ant file looks like this:
*
<project name="test" default="run-all" basedir=".">
<property name="src" value="./src" />
<property name="lib" value="./lib" />
<property name="bin" value="./bin" />
<property name="report" value="./report" />
<path id="test.classpath">
<pathelement location="${bin}" />
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
</path>
<target name="init">
<delete dir="${bin}" />
<mkdir dir="${bin}" />
</target>
<target name="exec" depends="init">
<delete dir="${report}" />
<mkdir dir="${report}" />
<mkdir dir="${report}/xml" />
<junit printsummary="yes" haltonfailure="no">
<batchtest fork="yes" todir="${report}/xml">
<resources>
<zipfileset src="BVTTest.jar" includes="**/*TestSuite.class"/>
</resources>
</batchtest>
<classpath>
<fileset dir="">
<include name="**/*.jar" />
</fileset>
</classpath>
</junit>
<junitreport todir="${report}">
<fileset dir="${report}/xml">
<include name="TEST*.xml" />
</fileset>
<report format="frames" todir="C:/eclipse/html" />
</junitreport>
</target>
<target name="start-selenium-server">
<java jar="selenium-server-standalone-2.25.0.jar" fork="true" spawn="true">
<arg line="-timeout 30" />
</java>
</target>
<target name="browse">
<exec executable="C:\Program Files\Internet Explorer\iexplore.exe">
<arg value="C:/eclipse/html/index.html"/>
</exec>
</target>
<target name="stop-selenium-server">
<get taskname="selenium-shutdown"
src="http://localhost:4444/selenium-server/driver/?cmd=shutDown"
dest="result.txt"
ignoreerrors="true" />
<echo message="selenium server stopped succesfully"/>
</target>
<target name="run-all">
<parallel>
<antcall target="start-selenium-server">
</antcall>
<sequential>
<echo taskname="waitfor" message="Wait for proxy server launch" />
<waitfor maxwait="1" maxwaitunit="minute" checkevery="100">
<http url="http://localhost:4444/selenium-server/
driver/?cmd=testComplete" />
</waitfor>
<antcall target="exec">
</antcall>
<antcall target="stop-selenium-server">
</antcall>
<antcall target="browse">
</antcall>
</sequential>
</parallel>
</target>
</project>
*
What it does is, it skips the <junit>; and after <mkdir> jumps directly to <junitreport>.
Please help.

HTML Report for JUNIT by ANT

How can I generate HTML reports from JUnit using Ant when there are test failures?
The reports are generated when there are no failures.
Also, how can we define our own XSLT for the report generation?
build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="Ant Example" default="all" basedir=".">
<property name="project_name" value="junitSamples" />
<property name="src" location="src" />
<property name="build" location="build/classes" />
<property name="lib" location="lib" />
<property name="reports" location="reports" />
<target name="init" depends="clean">
<mkdir dir="${build}" />
<mkdir dir="${lib}" />
<mkdir dir="${reports}" />
<mkdir dir="${reports}/raw/" />
<mkdir dir="${reports}/html/" />
</target>
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${build}" description="compile the source code ">
<classpath>
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
</classpath>
</javac>
</target>
<target name="clean">
<delete dir="build" />
<delete dir="${reports}" />
</target>
<target name="run-tests" depends="compile">
<junit printsummary="yes" haltonfailure="yes" showoutput="yes">
<classpath>
<pathelement path="${build}" />
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
</classpath>
<batchtest fork="yes" todir="${reports}/raw/">
<formatter type="xml" />
<fileset dir="${src}">
<include name="**/*Test*.java" />
</fileset>
</batchtest>
</junit>
</target>
<target name="test" depends="run-tests">
<junitreport todir="${reports}">
<fileset dir="${reports}/raw/">
<include name="TEST-*.xml" />
</fileset>
<report format="noframes" todir="${reports}\html\" />
</junitreport>
</target>
<target name="all" depends="clean, test" />
</project>
To specify your own stylesheets, use the "styledir" attribute:
<report styledir="${resources}/junit" format="..." todir="..." />
As noted, you must use the "junit-noframes.xsl" stylesheet name.
JUnit report docs.
You set haltonfailure="yes" it means that if one test fails build operation will stop.
This is from the ant documentation:
"Stop the build process if a test fails (errors are considered failures as well)."
Read more here- https://ant.apache.org/manual/Tasks/junit.html

Ant is not recognizing Log4j.properties file

I have been struck in this issue for past two days.Please help me in this. I am running my JUNIT scripts by using ANT. Reports are being generated, but ANT is not able to locate my log4j.properites file. When I am running through eclipse, logs are being generated. My problem here is I want logs when I am running through ANT.DO I need to set any properties.
What is the mistake I am doing?
Please help me.
My Log:
#Application Logs
#log4j.logger.devpinoyLogger
log4j.rootLogger=DEBUG, dest1
log4j.appender.dest1=org.apache.log4j.RollingFileAppender
log4j.appender.dest1.maxFileSize=5000KB
log4j.appender.dest1.maxBackupIndex=3
log4j.appender.dest1.layout=org.apache.log4j.PatternLayout
log4j.appender.dest1.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss} %c %m%n
log4j.appender.dest1.File=/Users/Application.log
#do not append the old file. Create a new log file everytime
log4j.appender.dest1.Append=false
build.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE project [
]>
<project name="Module_Junit_Ant" default="usage" basedir=".">
<!-- ========== Initialize Properties =================================== -->
<property environment="env"/>
<property name="ws.home" value="${basedir}"/>
<property name="ws.jars" value="${ws.home}/jars"/>
<property name="test.dest" value="${ws.home}/build"/>
<property name="test.src" value="${ws.home}/src"/>
<property name="test.reportsDir" value="${ws.home}/reports"/>
<path id="testcase.path">
<pathelement location="${test.dest}"/>
<fileset dir="${ws.jars}">
<include name="*.jar"/>
</fileset>
</path>
<!--target name="start-selenium-server">
<java jar="${ws.home}/lib/selenium-server.jar"/>
</target-->
<target name="setClassPath" unless="test.classpath">
<path id="classpath_jars">
<fileset dir="${ws.jars}" includes="*.jar"/>
<fileset dir="${test.src}" includes="*.properties"/>
</path>
<pathconvert pathsep=":"
property="test.classpath"
refid="classpath_jars"/>
</target>
<target name="init" depends="setClassPath">
<tstamp>
<format property="start.time" pattern="MM/dd/yyyy hh:mm aa" />
</tstamp>
<condition property="ANT"
value="${env.ANT_HOME}/bin/ant.bat"
else="${env.ANT_HOME}/bin/ant">
<os family="windows" />
</condition>
</target>
<!-- all -->
<target name="all">
</target>
<!-- clean -->
<target name="clean">
<delete dir="${test.dest}"/>
</target>
<!-- compile -->
<target name="compile" depends="init, clean" >
<delete includeemptydirs="true" quiet="true">
<fileset dir="${test.dest}" includes="**/*"/>
</delete>
<echo message="making directory..."/>
<mkdir dir="${test.dest}"/>
<echo message="classpath------: ${test.classpath}"/>
<echo message="compiling..."/>
<javac
debug="true"
destdir="${test.dest}"
srcdir="${test.src}"
target="1.5"
classpath="${test.classpath}"
>
</javac>
</target>
<!-- build -->
<target name="build" depends="init">
</target>
<target name="usage">
<echo>
ant run will execute the test
</echo>
</target>
<path id="test.c">
<fileset dir="${ws.jars}" includes="*.jar"/>
</path>
<target name="run" >
<delete includeemptydirs="true" quiet="true">
<fileset dir="${test.reportsDir}" includes="**/*"/>
</delete>
<java jar="${ws.jars}" fork="true" spawn="true" />
<junit fork="yes" haltonfailure="no" printsummary="yes">
<classpath refid="testcase.path" />
<!-- <classpath ="${test.classpath}"/> -->
<batchtest todir="${test.reportsDir}" fork="true">
<fileset dir="${test.dest}">
<include name="LogTest.class" />
<!--include name="tests/suite1/FirstSuiteRunner.class" />
<include name="tests/suite1/FirstSuiteRunner.class" /-->
</fileset>
</batchtest>
<formatter type="xml" />
<classpath refid="testcase.path" />
</junit>
<junitreport todir="${test.reportsDir}">
<fileset dir="${test.reportsDir}">
<include name="TEST-*.xml" />
</fileset>
<report todir="${test.reportsDir}" />
</junitreport>
</target>
<target name="email" >
<java classname="util.SendMail" classpath="${test.dest}" classpathref="testcase.path" />
</target>
</project>
Where are your log4j.properties located ?
What works for me it to put a
< pathelement location="x/y/z"/>
inside the
< path id="testcase.path">
which points to a folder which contains a log4j.properties!
So in this example your log4j.properties should be inside the 'z' folder!

Cobertura generates my reports for code coverage, but the coverage shows as 0%

All of my reports get generated, but my coverage shows as 0%. I even created one dummy test to make sure it wasn't the way my tests were written, and it doesn't show for the one dummy class I'm covering. Here is my Ant build for this:
<?xml version="1.0" encoding="UTF-8" ?>
<project name="My Project Name" default="run.cobertura" basedir=".">
<description>My Description</description>
<!-- target: init -->
<target name="init">
<!-- create properties and directory structure -->
<property name="src.path" value="${basedir}/src" />
<property name="lib.path" value="${basedir}/lib" />
<property name="output.path" value="${basedir}/bin" />
<property name="testcase-unit-only.path" value="${basedir}/testcase-unit-only" />
<property name="testcase-unit-only.output.path" value="${basedir}/test-classes" />
<property name="cobertura.lib.path" value="${basedir}/lib-cobertura" />
<property name="cobertura.path" value="${basedir}/cobertura" />
<property name="cobertura.output.path" value="${cobertura.path}/bin" />
<property name="cobertura.reports.path" value="${cobertura.path}/reports" />
<property name="cobertura.data.file" value="${cobertura.path}/cobertura.ser" />
<delete dir="${testcase-unit-only.output.path}" />
<delete dir="${cobertura.path}"/>
<mkdir dir="${testcase-unit-only.output.path}"/>
<mkdir dir="${cobertura.path}"/>
<mkdir dir="${cobertura.output.path}"/>
<!-- define classpath references -->
<path id="cp.lib.path">
<fileset dir="${lib.path}">
<include name="*.jar"/>
</fileset>
</path>
<path id="cp.classes.path">
<pathelement path="${output.path}" />
</path>
<path id="cp.classes.test.path">
<pathelement path="${testcase-unit-only.output.path}" />
</path>
<path id="cp.lib.cobertura.path">
<fileset dir="${cobertura.lib.path}">
<include name="*.jar"/>
</fileset>
</path>
<path id="cp.all.path">
<path refid="cp.lib.path"/>
<path refid="cp.classes.path"/>
<path refid="cp.lib.cobertura.path"/>
</path>
</target>
<!-- target: run.cobertura-instrument -->
<target name="run.cobertura-instrument">
<taskdef classpathref="cp.lib.cobertura.path" resource="tasks.properties"/>
<cobertura-instrument todir="${cobertura.output.path}" datafile="${cobertura.data.file}">
<fileset dir="${output.path}">
<include name="**/*.class" />
</fileset>
</cobertura-instrument>
</target>
<!-- target: compile.classes -->
<target name="compile.classes">
<javac srcdir="${src.path}" destdir="${output.path}">
<classpath refid="cp.lib.path"/>
</javac>
</target>
<!-- target: compile.tests -->
<target name="compile.tests">
<javac srcdir="${testcase-unit-only.path}" destdir="${testcase-unit-only.output.path}">
<classpath refid="cp.all.path"/>
</javac>
</target>
<!-- target: run.junit -->
<target name="run.junit">
<junit fork="true" dir="${basedir}" failureProperty="test.failed">
<classpath location="${cobertura.output.path}"/>
<classpath location="${output.path}"/>
<sysproperty key="net.sourceforge.cobertura.datafile" file="${cobertura.data.file}" />
<classpath refid="cp.lib.path"/>
<classpath refid="cp.classes.test.path"/>
<classpath refid="cp.lib.cobertura.path"/>
<formatter type="xml" />
<!-- <formatter type="brief" usefile="false"/> -->
<batchtest todir="${testcase-unit-only.output.path}" unless="testcase">
<fileset dir="${testcase-unit-only.output.path}">
<include name="**/*UnitTest.java"/>
</fileset>
</batchtest>
</junit>
</target>
<!-- target: run.cobertura -->
<target name="run.cobertura" depends="init,run.cobertura-instrument,compile.classes,compile.tests,run.junit">
<cobertura-report srcdir="src" destdir="${cobertura.reports.path}" datafile="${cobertura.data.file}"/>
</target>
</project>
One thing I notice is that in the depends list for the run.cobertura target you instrument the compiled classes before you compile them. That might work if you run twice, assuming the compiled classes from the first run are not cleared down, but doesn't seem quite right. On the first run if there are no instrumented classes, your report would be empty.
I had the same ant build scripts. It worked on my local workstation, but didn't on jenkins server.
But server had jdk 7 and workstation jdk6. After changing jdk on jenkins server to jdk6, code coverage generates without any problem.
you must set debug on when you compile java
<javac **debug="on"** srcdir="${testcase-unit-only.path}" destdir="${testcase-unit-only.output.path}">

Resources