Tom
Tom

Reputation: 23

Need a hand with debugging build.xml logic

Below is the code snippet from my build.xml file, when the Beta target fails it's expected to rerun the same beta target, but the control isn't going to the retry logic upon beta failure, not sure what i am missing here

    <!-- US Beta Target -->
    <target name="test-integration-assert-beta">
       <test-environment country="US" stage="Beta" host.name="URL Goes Here" emailid="" password="" company="" invalidpassword="" materialset=""/>
           <echo message="integTest.failure = ${integTest.failure}" />
           <echo message="failedTests = ${failedTests}" />
           <condition property="failedTests">
               <and>
                   <istrue value="${integTest.failure}" />
                   <available file="${integ.test.dir}/testng-failed.xml" />
               </and>
           </condition>
           <antcall target="test-integration-assert-beta-rerun">
           </antcall>
    </target>   

    <!-- US Beta Target (Re-run) -->
        <target name="test-integration-assert-beta-rerun" description="Rerunning Failed Beta Tests" if="failedTests">
           <echo message="Running Failed Integration Tests..." />
           <echo message="rerunFailedTests.failure = ${rerunFailedTests.failure}" />
           <copy file="${output.dir}/brazil-integ-tests/testng-failed.xml"
              tofile="${output.dir}/testng-failed.xml" />
       <test-environment country="US" stage="Beta" host.name="URL Goes Here" emailid="" password="" company="" invalidpassword="" materialset=""/>
           <echo message="rerunFailedTests.failure = ${rerunFailedTests.failure}" />
           <fail message="Tests Failed on rerun">
               <condition>
                   <istrue value="${rerunFailedTests.failure}" />
               </condition>
           </fail>
        </target>

Upvotes: 0

Views: 562

Answers (1)

Chad Nouis
Chad Nouis

Reputation: 7051

<antcall> can be useful at times, but it has a lot of pitfalls that can make scripts difficult to reason about. In particular, <antcall> conflicts with the normal dependency flows of Ant.

Instead of <antcall>, consider using <target depends="..."> along with a <macrodef>:

  • The depends attribute of <target> enables conditional logic.
  • <macrodef> enables code reuse and reduces code duplication.

The following build.xml gives an example:

<project name="ant-macrodef-retry" default="run">
    <macrodef name="my-test-environment">
        <attribute name="try"/>
        <sequential>
            <echo>Call test-environment here.</echo>
            <!-- The following <condition> tasks are just for testing purposes. -->
            <!-- The real <test-environment> would set these properties. -->
            <condition property="integTest.failure" value="true">
                <and>
                    <equals arg1="@{try}" arg2="1"/>
                    <isset property="failTry1"/>
                </and>
            </condition>
            <condition property="rerunFailedTests.failure" value="true">
                <and>
                    <equals arg1="@{try}" arg2="2"/>
                    <isset property="failTry2"/>
                </and>
            </condition>
        </sequential>
    </macrodef>

    <target name="try1">
        <my-test-environment try="1"/>
        <echo message="integTest.failure = ${integTest.failure}" />
        <condition property="failedTests">
            <istrue value="${integTest.failure}" />
        </condition>
    </target>

    <target name="try2" if="failedTests">
        <echo message="Running Failed Integration Tests..." />
        <my-test-environment try="2"/>
        <echo message="rerunFailedTests.failure = ${rerunFailedTests.failure}" />
        <fail message="Tests Failed on rerun">
            <condition>
                <istrue value="${rerunFailedTests.failure}" />
            </condition>
        </fail>
    </target>

    <target name="run" depends="try1,try2"/>
</project>

Output

Below are various tests of the above Ant script. It shows how the script behaves in the various failure scenarios...

=================================
Test command line: ant

try1:
     [echo] Call test-environment here.
     [echo] integTest.failure = ${integTest.failure}

try2:

run:

BUILD SUCCESSFUL

=================================
Test command line: ant -DfailTry1=true

try1:
     [echo] Call test-environment here.
     [echo] integTest.failure = true

try2:
     [echo] Running Failed Integration Tests...
     [echo] Call test-environment here.
     [echo] rerunFailedTests.failure = ${rerunFailedTests.failure}

run:

BUILD SUCCESSFUL

=================================
Test command line: ant -DfailTry1=true -DfailTry2=true

try1:
     [echo] Call test-environment here.
     [echo] integTest.failure = true

try2:
     [echo] Running Failed Integration Tests...
     [echo] Call test-environment here.
     [echo] rerunFailedTests.failure = true

BUILD FAILED
C:\ant\build.xml:35: Tests Failed on rerun

Upvotes: 1

Related Questions