Chapter 26. TestCase Task

Modeled after the TestCase provided by jUnit, this class is an Ant task that looks through the build file that contains this task, calls a 'setUp' target (if it exists), then all targets whose names start with 'test', and last calls a target named 'tearDown' (if it exists). Both 'setUp' and 'tearDown' are optional targets in the build file.

Ant stores targets in a hashtable, so there is no guaranteed order in which the 'test*' targets will be called. If order is important, use the 'depends' attribue of a target to enforce order, and do not name dependent targets with a name starting with 'test'.

Most unit tests will make use of Assert. As the Assert task requires that the property "ant.enable.asserts" be set to true before it will do anything, this task automatically sets this property to true. The Assert task has a "level" attribute. By default, the level is set to "error", so if the Assert fails, the TestCase fails. If the level is set to "warning", the test case will be marked as a warning rather than a failure. If the level is set to "info" or "debug" and the Assert fails, any message associated with the Assert will be written out, but otherwise will be ignored by TestCase.

To use this task in your build files, include a task definition like this:

    <taskdef name="testcase" classname="ise.antelope.tasks.TestCase"/>

Table 26.1. TestCase Attributes

fileThe file containing tests.NoneYes
enabledDetermines if this test should be ran. By using a property for this attribute, it is easy to turn off/on multiple tests.OnNo
assertenabledGenerally tests will use the Assert task. This attribute sets whether asserts should be enabled.YesNo
failonerrorIf true, cause the build to fail. By default, a failed test does not cause the build to fail, so all tests may have the opportunity to run.NoNo
showoutputIf true, show intermediate test resultsYesNo
showsummaryIf true, show a summary of test results at the end of the test run.YesYes

TestCase is most often used in conjunction with the Suite task.

<project name="mathtest" basedir="." default="suite"

     Build file to run unit tests for the Math task

      <a:testcase file="math_basic_tests.xml"/>
      <a:testcase file="math_rules_tests.xml"/>
      <a:testcase file="math_precision_tests.xml"/>


Here is an example build file containing actual tests. The 'setUp' target will execute first, then the two test targets.

<project name="math_precision_tests" basedir="." default="suite"

   <target name="setUp">
      <echo>Running math precision tests.</echo>

   <target name="test10">
      <echo>Circle area test</echo>
      <a:math result="pi">
         <a:op op="*">
            <a:num value="PI"/>
            <a:op op="pow">
               <a:num value="1"/>
               <a:num value="2"/>
      <a:assert message="failed circle area test">
            <a:startswith string="${pi}" with="3.141592653589793"/>

   <target name="test11">
      <echo>Division by zero test</echo>
      <!-- division by zero -->
         <a:math result="x">
            <a:op op="/">
               <a:num value="PI"/>
               <a:num value="0"/>
         <fail>Division by 0 succeeded: ${x}</fail>