TestNG is so Groovy - No Fluff Just Stuff

TestNG is so Groovy

Posted by: Andrew Glover on October 4, 2006

As I wrote about previously, TestNG can execute an existing suite of JUnit tests, thus providing a unified reporting mechanism for all developer tests. This means that TestNG can also execute developer test written in Groovy.

In order for this process to work, however, you must compile any Groovy tests into byte code and add references in TestNG’s testng.xml file. You can either:

  • Reference the corresponding class directly or
  • Reference the package the class resides in

and ensure that the junit attribute of the test element these classes are defined in is set to true.

The following test, for instance, defined in a testng.xml file, is designated as a JUnit test. It will be run JUnit style– a fixture (if defined) for every test found in the class.

<test name="junits" junit="true">
  <classes>
   <class name="test.com.acme.sedo.frmwrk.filter.impl.ClassInclusionTest" />
  </classes>
</test>

In order to run all copasetic unit tests (defined in both TestNG and Groovy) in an Ant build, I create a TestNG task, which depends on a compilation of Groovy files, shown below.

<groovyc destdir="${classesdir}"
    srcdir="./test/groovy" >
   <classpath>
    <path refid="build.classpath"/>
    <pathelement path="${testclassesdir}"/>
    <pathelement path="${classesdir}"/>
   </classpath>
</groovyc>

The task above compiles any associated Groovy tests (found in the test/groovy directory) and puts the class files in a specific location.

Now, when testng-unit is invoked, any corresponding hip tests written in Groovy will be compiled into normal .class files and therefore picked up by TestNG (and run as normal JUnit tests). The TestNG task is accordingly defined to pick up a testng.xml file designated for running unit tests.

<target name="testng-unit" depends="compile,groovy-compile">
 <testng outputDir="${testng.output.dir.unit}"
        sourceDir="${testng.source.dir}"
        classpath="${testclassesdir};${classesdir}">

   <xmlfileset dir="${testng.suitexml.dir}"
        includes="${testng.suitexml-unit.name}"/>

   <classpath>
     <path refid="build.classpath"/>
   </classpath>
  </testng>
</target>

Now that this process has been defined, I can write tests in either JUnit, TestNG, or even Groovy and one runner will run them all. Dig it, man?

Andrew Glover

About Andrew Glover

Andrew is the Engineering Manager for Netflix's Delivery Engineering Team. He and his team are building the next generation Continuous Delivery platform that is facilitating Netflix's rapid global expansion. Before joining Netflix, he served as the CTO of App47, where he lead the development of a SaaS Mobile Application Management platform. Andrew is also the co-author of Addison Wesley's “Continuous Integration” and he actively blogs about software at thediscoblog.com.

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »