Post date: Nov 05, 2012 12:55:23 PM
AntUnit is a simple way of introducing Unit Tests to ant build scripts.
It lends itself more readily to ant build scripts that use macrodef to encapsulate build activities.
Create a top level build file that contains antunit elements that refer to a unit test file. Pass in any properties that the target build script requires.
build.xml
<project xmlns:au="antlib:org.apache.ant.antunit" xmlns="antlib:org.apache.tools.ant"> <property environment="env" /> <property name="antunit_path" value="${basedir}/resources/lib/ant-antunit-1.2.jar" /> <property name="target_path" value="${basedir}/src" /> <property name="commonTestScripts_path" value="${basedir/resources/scripts/common.xml" /> <taskdef uri="antlib:org.apache.ant.antunit" resource="org/apache/ant/antunit/antlib.xml"> <classpath> <pathelement location="${antunit_path}"/> </classpath> </taskdef> <au:antunit> <fileset dir="tests" includes="test_macrodef_name.xml"/> <au:plainlistener loglevel="info" sendLogTo="both" /> <propertyset> <propertyref name="antunit_path" /> <propertyref name="target_path" /> <propertyref name="commonTestScripts_path" /> <propertyref name="SVN_USERNAME" /> <propertyref name="SVN_PASSWORD" /> <propertyref name="env.SVN_HOME" /> </propertyset> </au:antunit> </project>
The UnitTest file can contain optional setUp and tearDown targets. These are run before and after each target that begins with 'test'.
tests/test_macrodef_name.xml
<project xmlns:au="antlib:org.apache.ant.antunit" xmlns="antlib:org.apache.tools.ant"> <taskdef uri="antlib:org.apache.ant.antunit" resource="org/apache/ant/antunit/antlib.xml"> <classpath> <pathelement location="${antunit_path}"/> </classpath> </taskdef> <!-- import the build under test --> <import file="${target_path}/build.xml"/> <!-- Some additional test scripts --> <import file="${commonTestScripts_path}"/> <target name="setUp" > <!-- Setup required for testing macrodef --> <m_clean /> <m_load_libs /> <m_init_properties /> <property name="emptyLogName" value="empty.log" /> <property name="oraErrorLogName" value="oraError.log" /> <touch file="${wip_dir}/${emptyLogName}" /> </target> <target name="test_m_errorHandling_processLogFileForErrors_logFileMissing" > <!-- Now test the macrodef and validate result --> <au:expectfailure message="Should fail if log file not found" > <m_errorHandling_processLogFileForErrors regExpString="NOT_USED" csvignorelist="NOT_USED" logName="missing.log" errorMessage="NOT_USED" errorLogDir="${wip_dir}" /> </au:expectfailure> </target> </project>