Skip to content

Double dereferenced properties in Ant with Groovy

The TestNG report system is nice, but sometimes you need to integrate the TestNG output with some other test reporting system. At work, this other system requires that a single ".suc" or ".dif" file written to a common directory for each "test" you run, where "test" is defined however you want. In our case, we do one test for each TestNG group we run.

Due to Ant's immutable properties, I found it difficult to get exactly behavior I wanted. The simple way was to set a property "has.failure" if the testng-failed.xml file existed, but then would have the effect of appearing that a bunch of tests had failed, rather than a single one. It wasn't that important of an issue, since we it it was easy to see which test had actually failed afterward.

I finally got around to fixing this today. I screwed around with ant tasks for a while, but finally decided to use Groovy, which I'll choose first next time. The main issue was with the double dereference — I wanted to create a property based on the groups the test was running, and then reference it the same way, e.g., create property ${foo}.failed, and then access it like "${${foo}.failed}" (which doesn't work). Ant lets you create this property, but then requires you to jump through some as-yet-unknown-to-me hoops in order to actually reference the property. However, this is simple in Groovy, shown below.

This basically replaces the "condition" tasks in my description of calling the testng task in this post.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<target name="run-testng" depends="" >
    ... call testng here
 
       <condition property="${groups}.has.failure" value="true" else="false">
          <available file="${testng.report.dir}/${groups}/testng-failed.xml"/>
       </condition>
 
       <antcall target="process-results">
          <param name="infix" value="${groups}"/>
       </antcall>
</target>
 
<target name="process-results">
       <groovy>
          def infix = properties['infix']
          def dest = properties['RESULTS_DIR']
          def suc = new File("${dest}/product.name.${infix}.suc")
          def dif = new File("${dest}/product.name.${infix}.dif")
          if (properties["${infix}.has.failure"] == "true"){
            dif.write('Pass')
            suc.delete()
          } else {
            suc.write('Pass')
            dif.delete()
          }
    </groovy>
</target>

And, yes, the framework we have requires "Pass" in the dif file– don't ask me.

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*