Chapter 26. TestCase Task
Prev | Additional Ant Tasks | Next

## 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

Attribute | Description | Default | Required
--- | --- | --- | ---
file | The file containing tests. | None | Yes
enabled | Determines if this test should be ran. By using a property for this attribute, it is easy to turn off/on multiple tests. | On | No
assertenabled | Generally tests will use the Assert task. This attribute sets whether asserts should be enabled. | Yes | No
failonerror | If 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. | No | No
showoutput | If true, show intermediate test results | Yes | No
showsummary | If true, show a summary of test results at the end of the test run. | Yes | Yes

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

    <project name="mathtest" basedir="." default="suite"
       xmlns:a="antlib:ise.antelope.tasks">

       <description>
         Build file to run unit tests for the Math task
       </description>

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

    </project>

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"
       xmlns:a="antlib:ise.antelope.tasks">

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

       <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:op>
             </a:op>
          </a:math>
          <a:assert message="failed circle area test">
             <a:bool>
                <a:startswith string="${pi}" with="3.141592653589793"/>
             </a:bool>
          </a:assert>
       </target>

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

    </project>

Prev | Up | Next
Chapter 25. Suite Task | Home | Chapter 27. Performance Monitoring