my thoughts on test automation

Integrating TestNG with SBT

TestNG is a great unit test framework. In many work environments, it is highly desirable (or just most natural) to execute tests using build tool. To makes most of TestNG, one needs to utilise its configurability via testng.xml file. However, SBT doesn’t work with testng.xml files out of the box. In this blog post, I will show you how to combine both to work together. That should save you a few moments of your own time. In this tutorial I will create SBT project from scratch, without using any IDE. However, if you want to use IDE of your choice, feel free to do so.

    • Create sample-sbt-project directory
    • In project root directory, create build.sbt file:
      name := "sample-sbt-project"
      version := "1.0"
      scalaVersion := "2.10.4"
      libraryDependencies ++= Seq(
        "org.testng" % "testng" % "6.8.21",
        "org.scalatest" % "scalatest_2.10" % "2.2.4"
    • Download  sbt-launch.jar and copy project root directory
    • Create sbt text file in your project root directory:
      SBT_OPTS="-Xms512M -Xmx1536M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256M"
      java $SBT_OPTS -jar `dirname $0`/sbt-launch.jar "$@“
    • In root project directory run  chmod +x sbt  to make it executable
    • If you plan to use IntelliJ, create project/plugins.sbt file:
      addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0”)

      This will enable sbt gen-idea.

    • Create folder structure:
      mkdir -p src/test/java/com/wordpress/automatictester/tests
      mkdir -p src/test/scala
      mkdir -p src/test/resources
    • Create test src/test/java/com/wordpress/automatictester/tests/
      package com.wordpress.automatictester.tests;
      import org.testng.annotations.Test;
      public class SampleTestA {
         public void sampleTest() throws InterruptedException {
            System.out.println("Starting " + this.getClass().getName());
            System.out.println("Finishing " + this.getClass().getName());

      You can create more classes like this in the same folder, if you want to see your tests running in parallel.

    • Create src/test/resources/testng.xml:
      <!DOCTYPE suite SYSTEM "">
      <suite name="Test runner" parallel="classes" thread-count="10">
          <test name="Package with subpackages">
                  <package name="com.wordpress.automatictester.tests.*"/>
    • Create src/test/scala/TestRunner.scala:
      import org.scalatest.testng.TestNGWrapperSuite
      class TestRunner extends TestNGWrapperSuite (

      This file will act as a wrapper for xml file.

    • Take care of dependencies and compile your project:
      ./sbt clean compile test:compile
    • Now you can run your test(s) via created Scala wrapper from command line:
      ./sbt "test-only TestRunner"

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: