my thoughts on test automation

Integrating TestNG with Maven

This article is similar to the one on SBT and TestNG integration. If you are new to Maven and/or Maven + TestNG integration, it can save you a good few moments.

  • Set up Maven project. You can do it with your favourite IDE or just use Maven archetype:
    mvn -B archetype:generate \
       -DgroupId=com.wordpress.automatictester.projects.maven \

    (It is assumed you have Maven set up correctly on your machine)

  • Get rid of automatically created app and test classes
  • Remove JUnit dependency from pom.xml file, add TestNG dependency instead:
  • 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.projects.maven.*"/>
  • Add TestNG configuration to pom.xml:
  • Create src/test/java/com/wordpress/automatictester/tests/SampleTestA.class:
    package com.wordpress.automatictester.projects.maven;
    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 sample tests, if you want to see how they run in parallel.

  • If you are using Intellij, add TestNG to classpath as suggested by IDE.
  • Now you should be ready to run your tests with Maven:
    mvn test -Dfilename=src/test/resources/testng.xml

    You should now see your test(s) passing.


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: