It’s a bad sign if the only feedback you have for a project is a long build with only one set of tests. Do yourself (and your team) a favour by splitting test execution into logical groupings, with the fastest (or most important) running first. Here’s an ant macro you can reuse easily that’s optimised to only generate the junit HTML report and fail the build if any tests fail. (Sorry about the funny quote characters – I can’t seem to get my blog software to properly encode them)
<macrodef name="run_junit_tests" description="Macro for running junit tests"><br /> <attribute name="testclasspath" default="unit.test"/><br /> <attribute name="testfileset" default="unittest.fileset"/><br /> <attribute name="outputdir" default="build/output/test"/><br /> <attribute name="basedir" default="."/><br />
<sequential><br /> <mkdir dir="@{outputdir}"/><br /> <junit forkmode="perBatch"<br /> printsummary="yes"<br /> haltonfailure="false"<br /> failureproperty="unit.test.failure"<br /> haltonerror="false"<br /> errorproperty="unit.test.error"<br /> dir="@{basedir}"><br /> <classpath refid="@{testclasspath}"/><br /> <formatter type="xml"/><br /> <batchtest fork="yes" todir="@{outputdir}"><br /> <fileset refid="@{testfileset}"/><br /> </batchtest><br /> </junit><br /> <condition property="tests.failed.or.errored"><br /> <or><br /> <isset property="unit.test.failure"/><br /> <isset property="unit.test.error"/><br /> </or><br /> </condition><br /> <property name="_junit_report_dir_" value="@{outputdir}"/><br /> <antcall target="-generate_junit_report_and_fail"/><br /> </sequential><br /></macrodef><br />
<target name="-generate_junit_report_and_fail" if="tests.failed.or.errored"<br /> description="Generate the unit test report if tests failed and cause build to stop short"><br /> <junitreport todir="${_junit_report_dir_}"><br /> <fileset dir="${_junit_report_dir_}"><br /> <include name="TEST-*.xml"/><br /> </fileset><br /> <report format="frames" todir="${_junit_report_dir_}/output"/><br /> </junitreport><br /> <fail if="tests.failed.or.errored" message="Build failed due to Unit test failures or errors"/><br /></target>
Awesome – thanks Pat. I was about to start struggling with that this week…
No problems. Glad I could help 🙂