Tuesday, June 17, 2014

Implement Java Code Coverage and Junit test cases using Ant

Automating Java Code Coverage and JUnit:

Following jar files are required JUnit:

hamcrest-core-1.3.jar
junit-4.7.jar

Following jar files are required Java Code coverage:

jacocoant.jar


Build Properties:

src.dir=src
test.dir=test
project.name=TestApp
build.dir=appbuild
test.reports=test-reports
coverage.report=coverage-reports

Ant Targets:

<project name="TestApp" default="echo-properties" basedir="." xmlns:jacoco="antlib:org.jacoco.ant">

<property file="build.properties" />

<!--Creating the required directories for test cases-->
<target name="test-init" description="Creates the required directories for test case reports">
<echo>******Cleaning the ${test.reports}******</echo>
<delete dir="${test.reports}" />
<echo>******Creating the required directories for test reports******</echo>
<mkdir dir="${test.reports}" />
</target>
<target name="execute-junit" depends="compile,test-init">
<junit fork="true" printsummary="on" haltonfailure="yes" description="Junit test cases TestApp">
<classpath refid="classpath"/>
<classpath>
       <pathelement location="${build.dir}/WEB-INF/classes"/> 
       <pathelement location="${build.dir}/WEB-INF/test"/> 
       <pathelement location="${build.dir}/WEB-INF/config"/> 
</classpath>

<!--Save test result in xml format-->
    <formatter type="xml"/>
 
<!--Single test case->
     <!--<test name="com.AuthenticationServiceTest"
                                 haltonfailure="no" outfile="unittest_result"/>-->

<!--Batch test run-->
<batchtest fork="yes" todir="${test.reports}">
       <fileset dir="${build.dir}/WEB-INF/test">
        <include name="**/*TestSuite.class"/>
        <exclude name="**/*Test.class"/>
        <exclude name="**/AllTests.class"/>
       </fileset>
</batchtest>
 </junit>
  <antcall target="execute-junit-report"/>
</target>
<!--Report of the Junit Test cases -->
<target name="execute-junit-report">
        <junitreport todir="${test.reports}">
            <fileset dir="${test.reports}" includes="TEST-*.xml"/>
            <report todir="${test.reports}"/>
        </junitreport>
</target>
<!--Creating the required directories for coverage-->
<target name="test-coverage" description="Creates the required directories for coverage">
<echo>******Cleaning the ${coverage.report}******</echo>
<delete dir="${coverage.report}" />
<echo>******Creating the required directories for test reports******</echo>
<mkdir dir="${coverage.report}" />
</target>
<!-- Code Coverage Configurations -->
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
<classpath path="${lib.dir}/jacocoant.jar"/>
</taskdef>
<target name="execute-code-coverage" depends="compile,test-coverage,test-init">
   <jacoco:coverage>
<junit fork="true" printsummary="on" haltonfailure="yes" description="Junit test cases TestApp">
<classpath refid="classpath"/>
<classpath>
       <pathelement location="${build.dir}/WEB-INF/classes"/> 
       <pathelement location="${build.dir}/WEB-INF/test"/> 
       <pathelement location="${build.dir}/WEB-INF/config"/> 
</classpath>

    <!--Save test result in xml format-->
    <formatter type="xml"/>
<!--Batch test run-->
<batchtest fork="yes" todir="${test.reports}">
       <fileset dir="${build.dir}/WEB-INF/test">
        <include name="**/*TestSuite.class"/>
        <exclude name="**/*Test.class"/>
        <exclude name="**/AllTests.class"/>
       </fileset>
</batchtest>
</junit>
</jacoco:coverage>
</target>
<target name="code-coverage" depends="execute-code-coverage">
<jacoco:report>
   <executiondata>
       <file file="jacoco.exec"/>
   </executiondata>
   <structure name="TestApp Code Coverage">
       <classfiles>
           <fileset dir="${build.dir}/WEB-INF/classes"/>
       </classfiles>
       <sourcefiles encoding="UTF-8">
           <fileset dir="${src.dir}"/>
       </sourcefiles>
   </structure>
   <html destdir="${coverage.report}"/>
<csv destfile="${coverage.report}/coverage-report.csv"/>
<xml destfile="${coverage.report}/coverage-report.xml"/>
</jacoco:report>
</target>

</project>




No comments:

Post a Comment

Thanks for your comments/Suggestions.