savaged.info

2009-09-24

groovy functional testing with gradle

Filed under: workfriendly — savaged @ 10:22
Tags: , , , , , , , ,

In my previous posting I created a ‘hello world’ style functional test example using the neat BDD framework, easyb. In that post my attention was focussed on the easyb side of things. In this post I want to draw attention to the use of Groovy when building and running the tests.

Instead of an Ant script I now can use a Groovy scripted solution. I have two choices (of course there are others); I can use Gant or Gradle. I would like to share both in that order, along with a few comments about them (please add IMHO before each comment).

Gant:

ant.path(id: 'project.classpath') {
    pathelement location: 'lib/easyb-0.9.6.jar'
    pathelement location: 'lib/commons-cli-1.2.jar'
    pathelement location: 'lib/groovy-1.6.4.jar'
    pathelement location: 'lib/selenium-java-client-driver.jar'
}

ant.taskdef(name: 'easyb', classname: 'org.easyb.ant.BehaviorRunnerTask') {
    classpath refid: 'project.classpath'
}

target(run: 'runs the tests') {
  easyb(failureProperty: 'easyb.failed') {
    classpath refid: 'project.classpath'
    report location: 'test/reports/behavior/Stories.txt', format: 'txtstory'
    behaviors(dir: '.') {
      include name: 'test/behavior/**/*Story.groovy'
    }
  }
}

setDefaultTarget run

So Gant is very much like Ant, to state the obvious, except instead of XML one can leverage the power of Groovy. I used a neat tool, called ant2gant, to help me convert my original Ant script (from the previous posting) to the version above. (I did a few tweaks, but the straight conversion would have been just as good.)

Gradle:

/**
 * needs: java -jar selenium-server.jar -interactive
 */
repositories {
    mavenCentral()
    mavenRepo urls: 'http://www.easyb.org/repoa/'
    mavenRepo urls: 'http://nexus.openqa.org/content/repositories/releases'
}

configurations {
    easybtaskdef
    easybrunner.extendsFrom easybtaskdef
}

dependencies {
    easybtaskdef(
        'org.easyb:easyb:0.9.6',
        'commons-cli:commons-cli:1.2',
        'org.codehaus.groovy:groovy-all:1.6.4'
    )
    easybrunner (
        'org.seleniumhq.selenium.client-drivers:selenium-java-client-driver:1.0.1'
    )
}

defaultTasks 'clean', 'run'

task clean << {
    new File('test/reports/behavior/Stories.txt').delete()
} 

task run << {
    ant.taskdef(
        name: 'easyb',
        classname: 'org.easyb.ant.BehaviorRunnerTask',
        classpath: configurations.easybtaskdef.asPath
    )
    ant.easyb(failureProperty: 'easyb.failed') {
	classpath {
            pathelement path: configurations.easybrunner.asPath
        }
        behaviors(dir: '.') {
            include name: 'test/behavior/**/*Story.groovy'
        }
	report location: 'test/reports/behavior/Stories.txt', format: 'txtstory'
    }
}

The Gradle version, is not too dissimilar to the Gant one, but we’re now able to use the dependency resolution Gradle offers. At a high-level view, Gradle is somewhat akin to Maven in this regard. And once again we get the advantages of using a powerful script language rather than XML.

There are enhancements I could make to my scripts, particularly to take advantage of the Groovy language. For example, I could use a variable for the easyb report, removing the repeated literal.

Here’s a small footnote for any vim folks (see my groovy minimalism posting). I found that I can get Groovy syntax highlighting in my build.gradle file simply by adding the follwing at the top.

#!groovy

Enjoy.

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 57 other followers