Chapter 25. Suite Task
Prev | Additional Ant Tasks | Next

## Chapter 25. Suite Task

Modeled after the TestSuite 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 executes all nested tasks, and last calls a target named 'tearDown' (if it exists). Both 'setUp' and 'tearDown' are optional targets in the build file. A build file may contain multiple suite tasks, note that each invocation will call 'setUp' and 'tearDown', so you may want to use some conditionals to only have them execute once.

While this task and the associated 'testcase' task work very well, a similar test framework has been created by the Ant development team. You may want to check out that framework as it is likely to be the "standard" Ant test framework. Look for "AntUnit" at http://ant.apache.org.

Typically, the nested tasks are TestCases, although they can be any task as appropriate to your testing. The nested tasks may also be Suites, so you can group your tests easily. Nested tasks are executed in order.

Suite may also hold FileSets. Each file in the FileSet will be treated as a file suitable for use by a TestCase and will be executed as such. This makes it easy to run an entire directory of tests without having to specify a TestCase for each one individually.

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

        <taskdef name="suite" classname="ise.antelope.tasks.Suite"/>

Table 25.1. Suite Attributes

Attribute | Description | Default | Required
--- | --- | --- | ---
enabled | Determines if this suite 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
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

In the example below, the "suite" tasks is a top-level task, so will execute automatically. This example does not use 'setUp' or 'tearDown' targets.

    <project name="mathtest" basedir="." 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>

       <!-- alternatively, a fileset could be used:
       <a:suite>
          <fileset dir="${basedir}">
             <include name="math_*.xml"/>
          </fileset>
       </a:suite>
       -->
    </project>

    $ ant -f mathtest2.xml
    Buildfile: mathtest2.xml
     [testcase] +-------------------------------------------+
     [testcase] + mathtest
     [testcase] +-------------------------------------------+

    test6:
     [testcase] ERROR: test6 failed: string or property must be set.

    test5:
     [testcase] test5 passed.

    test4:
     [testcase] test4 passed.

    test3:
     [testcase] test3 passed.

    test2:
     [testcase] test2 passed.

    test1:
     [testcase] test1 passed.
     [testcase] +-------------------------------------------+
     [testcase] + mathtest
     [testcase] +-------------------------------------------+
     [testcase]
     [testcase] ---- Errors ---------------------------------
     [testcase] ERROR: test6 failed: string or property must be set.
     [testcase] ---- Results --------------------------------
     [testcase] Ran 6 out of 6 tests.
     [testcase] Passed: 5
     [testcase] Warning: 0
     [testcase] Failed: 1
     [testcase] +-------------------------------------------+
     [testcase] +-------------------------------------------+
     [testcase] + math_precision_tests
     [testcase] +-------------------------------------------+

    test11:
         [echo] Division by zero test
        [a:try] Task 'a:math' in target 'test11' failed, error message is: java.lang.ArithmeticException
     [testcase] ERROR: test11 failed: java.lang.ArithmeticException: / by zero

    test10:
         [echo] Circle area test
     [testcase] ERROR: test10 failed: string or property must be set.
     [testcase] +-------------------------------------------+
     [testcase] + math_precision_tests
     [testcase] +-------------------------------------------+
     [testcase]
     [testcase] ---- Errors ---------------------------------
     [testcase] ERROR: test11 failed: java.lang.ArithmeticException: / by zero
     [testcase] ERROR: test10 failed: string or property must be set.
     [testcase] ---- Results --------------------------------
     [testcase] Ran 2 out of 2 tests.
     [testcase] Passed: 0
     [testcase] Warning: 0
     [testcase] Failed: 2
     [testcase] +-------------------------------------------+
     [testcase] +-------------------------------------------+
     [testcase] + math_rules_tests
     [testcase] +-------------------------------------------+

    test7.2:
     [testcase] test7.2 passed.

    test7.1:
     [testcase] test7.1 passed.

    test7.0:
     [testcase] test7.0 passed.

    test8.3:
     [testcase] test8.3 passed.

    test8.2:
     [testcase] test8.2 passed.

    test9:
     [testcase] test9 passed.

    test8.1:
     [testcase] test8.1 passed.

    test8.0:
     [testcase] test8.0 passed.
     [testcase] +-------------------------------------------+
     [testcase] + math_rules_tests
     [testcase] +-------------------------------------------+
     [testcase] ---- Results --------------------------------
     [testcase] Ran 8 out of 8 tests.
     [testcase] Passed: 8
     [testcase] Warning: 0
     [testcase] Failed: 0
     [testcase] +-------------------------------------------+
        [suite] ++-- Totals -------------------------------++
        [suite] ++ Total Ran 16 out of 16 tests.
        [suite] ++ Total Passed: 13
        [suite] ++ Total Warnings: 0
        [suite] ++ Total Failed: 3
        [suite] ++-----------------------------------------++

    BUILD SUCCESSFUL
    Total time: 1 second

Prev | Up | Next
Chapter 24. Repeat Task | Home | Chapter 26. TestCase Task