automatictester

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 \
       -DartifactId=sample-maven-project

    (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:
    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.21</version>
        </dependency>
    </dependencies>
  • Create src/test/resources/testng.xml:
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="Test runner" parallel="classes" thread-count="10">
        <test name="Package with subpackages">
            <packages>
                <package name="com.wordpress.automatictester.projects.maven.*"/>
            </packages>
        </test>
    </suite>
  • Add TestNG configuration to pom.xml:
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>${filename}</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>
  • Create src/test/java/com/wordpress/automatictester/tests/SampleTestA.class:
    package com.wordpress.automatictester.projects.maven;
    
    import org.testng.annotations.Test;
    
    public class SampleTestA {
    
        @Test
        public void sampleTest() throws InterruptedException {
            System.out.println("Starting " + this.getClass().getName());
            Thread.sleep(5000);
            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.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: