Hudson - Ant Build Steps with Junit Test Call

1 - About

This article shows you an example on how you can start your junit test with the Ant builder (runner) and Hudson as Scheduler and reporter.

  • An Hudson Job can call an Ant script.
  • Ant can call junit script with its junit task.
  • Ant can format Junit report with the junitreport task
  • To be able to see the Junit reports created by the junitreport ant task, you have to (create|push) them in the Hudson directory Workspace of the job.

3 - Prerequisites

4 - Directory Structure

Directory File
ProjectHome\ant\junit\ ant build file
ProjectHome\src\junit\ Junit test case
HudsonHome\TestWithReport\workspace The location of the Hudson workspace for the project “TestWithReport”

5 - Ant Build File


<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="MyFirstTest" basedir="../.." default="execute_all">

	<!-- Add an ANT property for the directory containing the HTML reports: -->
	<property name="hudson.workspace.root" value="C:\Users\gerard\.hudson\jobs\TestWithReport\workspace"/>
	<property name="dir.test.reports" value="${hudson.workspace.root}\junitreports" />
	<property name="dir.test.class" value="${basedir}\src" />
	<property name="dir.lib" value="C:\Users\gerard\.m2\repository" />
	<property name="classpath" value="${basedir}\classes" />

	<!-- Add the test jar (junit and testng) -->
	<path id="classpath">
		<pathelement location="${classpath}" />
		<fileset dir="${dir.lib}">
			<include name="**/junit*.jar" />
			<include name="**/*test*.jar" />
		</fileset>
	</path>

	<target name="create_directory">
		<echo message="Create the directory report ${dir.test.reports}" />
		<mkdir dir="${dir.test.reports}" />
	</target>

	<!-- Define the Ant task for running JUnit and generating reports -->
	<target name="execute_batch_test" depends="create_directory">

		<echo message="Execute Tests in Batch" />
		<junit fork="yes" printsummary="yes" haltonfailure="no">
			<classpath refid="classpath" />
			<batchtest fork="yes" todir="${dir.test.reports}">
				<fileset dir="${dir.test.class}">
					<include name="junit/*" />
				</fileset>
			</batchtest>
			<formatter type="xml" />
		</junit>

		<echo message="Format the tests report" />
		<junitreport todir="${dir.test.reports}">
			<fileset dir="${dir.test.reports}">
				<include name="TEST-*.xml" />
			</fileset>
			<report todir="${dir.test.reports}" />
		</junitreport>
	</target>

	<target name="execute_one_test" depends="create_directory">
		<echo message="Execute One Test" />
		<junit printsummary="yes" fork="yes" haltonfailure="no">
			<classpath refid="classpath" />
			<formatter type="xml" />
			<test name="junit.HelloWorld" todir="${dir.test.reports}" />
		</junit>
	</target>

	<target name="execute_all" depends="execute_one_test,execute_batch_test" />

</project>

where:

  • the hudson.workspace.root property is the location of the hudson workspace.
  • the dir.lib property is the location of the directory library (here a Maven Directory) where you can find the junit library
  • the dir.test.class property is the root directory of the source java file
  • the classpath property contains the root directory of the java compiled class classpath

Executing ant where the build file reside:


D:\svn\Java\Snippet\ant\junit>ant

You must get the following output.


Buildfile: D:\svn\Java\Snippet\ant\junit\build.xml

create_directory:
     [echo] Create the directory report C:\Users\gerard\.hudson\jobs\TestWithReport\workspace\junitreports

execute_one_test:
     [echo] Execute One Test
    [junit] Running junit.HelloWorld
    [junit] Tests run: 2, Failures: 1, Errors: 0, Time elapsed: 0.071 sec
    [junit] Test junit.HelloWorld FAILED

execute_batch_test:
     [echo] Execute Tests in Batch
    [junit] Running junit.Exception
    [junit] Tests run: 2, Failures: 0, Errors: 1, Time elapsed: 0.062 sec
    [junit] Test junit.Exception FAILED
    [junit] Running junit.HelloWorld
    [junit] Tests run: 2, Failures: 1, Errors: 0, Time elapsed: 0.051 sec
    [junit] Test junit.HelloWorld FAILED
    [junit] Running junit.MultipleFailure
    [junit] Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0.062 sec
    [junit] Test junit.MultipleFailure FAILED
    [junit] Running junit.TestFixture
    [junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 0.045 sec
     [echo] Format the tests report
[junitreport] Processing C:\Users\gerard\.hudson\jobs\TestWithReport\workspace\junitreports\TESTS-TestSuites.xml to
 C:\Users\gerard\AppData\Local\Temp\null434515434
[junitreport] Loading stylesheet jar:file:/C:/ant/lib/ant-junit.jar!/org/apache/tools/ant/taskdefs/optional/junitxsl/junit-frames.xsl
[junitreport] Transform time: 516ms
[junitreport] Deleting: C:\Users\gerard\AppData\Local\Temp\null434515434

execute_all:

BUILD SUCCESSFUL
Total time: 7 seconds

6 - Hudson New Job

Create a new job:

  • New Job as Name “TestWithReport” and as type “Build a free-style software project”
  • Build
    • Step: Invoke Ant
    • Script: ProjectHome\ant\junit\build.xml
  • Post-build Actions
    • Publish JUnit test result report: /junitreports/*.xml

And start the build by clicking on “Build Now”.

7 - Test Reports

You can then see the test reports on this two places:

8 - Documentation / Reference


Data Science
Data Analysis
Statistics
Data Science
Linear Algebra Mathematics
Trigonometry

Powered by ComboStrap