I have some unit tests running through Ant, and I'd like to be able to run some cleanup code if the unit tests fail. I was looking for some sort of "finally" block, but I had no luck finding one. I've tried using errorproperty and if statements on tasks, but ant only accepts "true", "on" and "yes" as true properties. A successfully executed task (on unix at least) returns 0, so I've had to construct a ridiculously elaborate apparatus:
<project name="TestBuild" default="build" basedir=".">
<target name="build" depends="truth,checkresult,cleanup" />
<target name="truth">
<echo message="Running Truth" />
<exec executable="false" errorproperty="testfailure"/>
</target>
<target name="checkresult">
<condition property="testfailed">
<not>
<equals arg1="${testfailure}" arg2="0" />
</not>
</condition>
</target>
<target name="cleanup" if="testfailed">
<echo message="cleanup" />
<fail />
</target>
Is there any simpler way to do this? For one, this requires to tasks to complete, which seems ridiculous. It also means I'd have to call both of them after every block of unit tests, because I obviously can't set failonerror as I normally would. In all, it's a hacky, inelegant solution, and I'm hoping someone has better one.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…