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.

2009-07-31

groovy minimalism

Filed under: technology,workfriendly — savaged @ 12:05
Tags: , , , , , , , ,

Being a minimalist, I’m not a fan of a heavy IDE, using them only when I have to. I think IDEs are like dogs, they sense your fear. At present Netbeans 6.7 seems to me to be the best trained pooch. For now however, let me propose an alternative and some background…

Recently I’ve been getting into Groovy and Grails, no doubt also appealing to my minimalist leanings. Convention over configuration let’s someone else do all the stuff that would normally clutter my development process. You have to give the tutorials a go (Groovy / Grails). If you’re a minimalist, lean processes type too, I feel sure you’ll not look back.

Now I’ve found I can mix the agility of Groovy and Grails and my favourite editor vi. At home I use Mac (did I mention I’m a minimalist). On my Mac I have MacVim. At work I’m forced to used MS Windows, I have CygWin and include vim from the setup tool.

Of course, I could have used TextMate on my Mac, and at work, the clone for MS “e“. However, I’m also a minimalist when it comes to paying money out (aka: being tight).

There are a few hoops to jump through to get going; I wanted to share them with all the other minimalists.

Step 1

Get a vimrc file working. See http://macvim.org/OSX/index.php. Particularly note the instructions about getting the $VIMRUNTIME/vimrc_example.vim files. (Works on MS Windows too, and probably Linux).

Step 2

Get Groovy language highlighting. Download groovy.vim. You might need to create a .vim directory in your home, and a ftplugin directory in there. Once you have that you just drop the groovy.vim file into ftplugin (works for both Mac and MS).

Step 3

Add NERDTree. Download and unzip NERD_tree.zip. Put the NERD_tree.vim file into your ./vim/plugin directory and NERD_tree.txt into your .vim/doc directory. (Note that you might need to create plugin and doc directories, if it’s your first plugin).

Step 4

Add snipMate. Download and unzip snipMate.zip into your ./vim/plugin and doc directories; as you did above.

Step 5

In vim run :helptags ~/.vim/doc then :help local-additions to view the docs for both the plugins.

Step 6

In vim run something like :NERDTree /mycode/mygroovyproj/

Step 7

Revel in the simplicity of your new development environment ;)

Finally, when you really do need an IDE then there’s great Groovy & Grails support in Netbeans and now in Eclipse. Oh and the one you have to pay for, Intellij.

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 57 other followers