HTML Report for JUNIT by ANT - 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

Related

Use JUnit with Jenkins & Ant Project

I am running Junit Tests on Jenkins via ant.
I can received the build success message in Jenkins, but JUnit result is error.
java.lang.ClassNotFoundException: JUnitTest
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
This is my build.xml.
<?xml version="1.0"?>
<project name="testProject" default="main" basedir=".">
<target name="init">
<property name="src" location="src" />
<property name="bin" location="bin" />
<property name="lib" location="WebContent/WEB-INF/lib" />
<property name="war.name" location="testProject.war" />
<property name="web" location="WebContent" />
<mkdir dir="${bin}" />
<tstamp>
<format property="DSTAMP" pattern="yyyyHHdd" />
<format property="TSTAMP" pattern="HHmm" />
</tstamp>
</target>
<target name="compile" depends="init">
<fileset dir="WebContent/WEB-INF/lib" includes="*.jar" />
</path>
<javac srcdir="src/test" destdir="${bin}" debug="on" includeantruntime="false">
<classpath refid="junit.jar"/>
<include name="*.java"/>
</javac>
<javac srcdir="src/testProject" destdir="${bin}" debug="on" includeantruntime="false">
<include name="*.java"/>
</javac>
</target>
<target name="junit4" depends="compile">
<delete dir="report" />
<mkdir dir="report" />
<junit printsummary="on" fork="false" haltonfailure="false">
<classpath refid="junit.jar"/>
<formatter type="xml"/>
<batchtest todir="report">
<fileset dir="src/test" includes="**/JUnit*.java" />
</batchtest>
</junit>
<junitreport todir="report">
<fileset dir="report" includes="TEST-*.xml" />
<report format="frames" todir="report"/>
</junitreport>
</target>
<target name="main" depends="clean,war">
<java classpath="${bin}" classname="testProject.HelloWorld">
</java>
</target>
<target name="war" depends="compile,junit4">
<war destfile="${bin}/${DSTAMP}.war" webxml="${web}/WEB-INF/web.xml">
<fileset dir="${web}">
<include name="**/*.*" />
<exclude name="WEB-INF/web.xml" />
</fileset>
</war>
</target>
<target name="clean">
<delete dir="${bin}" />
</target>
</project>
I've reviewed other similar issues, but it didn't work.
Why can't it find the Class?

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.

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!

Junit Test Not Deploying from Ant Script

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">

Resources