Integration Of Maven With TestNg Using Maven Surefire Plugin

By Sruthy

By Sruthy

Sruthy, with her 10+ years of experience, is a dynamic professional who seamlessly blends her creative soul with technical prowess. With a Technical Degree in Graphics Design and Communications and a Bachelor’s Degree in Electronics and Communication, she brings a unique combination of artistic flair…

Learn about our editorial policies.
Updated March 10, 2024

This tutorial Explains How to Use The Maven Surefire Plugin to Manage our Dependencies and Select & Execute Particular Test Scripts or Suites Using TestNG:

Here is a perfect guide for you on the Integration of Maven and TestNG using the Maven Surefire plugin and how to execute the script using this plugin.

Let’s Move On!!

=> Check ALL TestNG Tutorials Here.

Integration Of Maven With TestNg

What Is Maven Surefire Plugin?

  • The Surefire plugin is designed to execute the unit tests of an application and can generate the reports using HTML format.
  • We can integrate Surefire plugins with the other testing frameworks such as TestNG, Junit, and POJO Tests, etc.
  • It also supports other languages like C#, Ruby, Scala, etc.

Basic Terminologies

Let’s refresh/better understand the most basic terminologies used in this tutorial.

#1) Maven: It is a build automation tool that is primarily used for java projects. It dynamically downloads Java libraries and Maven plugins from Maven Central repository which is called Dependency Management.

#2) Maven Central Repository: It is a place where all the project jars, libraries, and plugins are stored and it can be accessed by Maven easily.

#3) POM (Project Object Model): It is an XML file that contains information about the project and configuration details used by maven to build the project.

#4) TestNG: It is an open-source testing framework that helps us to run before/after tests, by grouping the tests using annotations and can generate reports. It also supports Data-driven testing, Parallel execution, and Parametrization. It is easier to use.

These are the basic terminologies of Maven and TestNG. Now, let’s see the purpose of the Surefire plugin and the integration procedure.

Why We Need Maven With TestNG Integration?

  • Whenever we are executing test scripts or suites using the Maven project, our dependencies are managed in the POM.xml file. However, a specific test suite cannot be selected to execute from a list of available suites.
  • In TestNG, we cannot manage our dependencies but we can select and execute particular test scripts or suites.
  • Given that Maven and TestNG have different capabilities, we are integrating both using the Maven Surefire plugin.

Work Flow Using The Maven Surefire Plugin

Work Flow using Maven Surefire Plugin

  • Here, execution starts from the Maven project using POM.xml. Initially, it connects to the Maven Online Repository and downloads the latest version of the dependencies.
  • As TestNG has the capability to select and execute particular test scripts or suites, we are integrating this with Maven using the Maven Surefire plugin.

Configuration Of Maven Surefire Plugin

Step 1: Select the POM.xml file from the Maven project. Right-click and select Maven => Add Plugin

Maven - Add Plugin

Step 2: Add the Plugin window will be displayed.

Add Plugin window

To enter the Plugin details:

  1. Go to Google and Type Maven Surefire plugin.
  2. Click the link, maven.apache.org/surefire/maven-surefire-plugin and Select ‘Using TestNg’ link on the left pane of the window.
  3. Select the XML code that is displayed under the ‘Using Suite XML Files’ header.
  4. Enter the Group Id, Artifact Id and Version details in the Add Plugin Window using the below XML code snippet and click Ok.

Source Code:

<Plugins>
	<plugin>
	 <groupId>org.maven.plugins</groupId>
	<artifactId>maven-surefire-plugin</artifactId>
	<version>2.20</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>

Maven Surefire plugin

Step 3: On clicking the OK button, the Plugin is added in POM.xml file.

, Plugin is added in POM.xml file

Step 4: Copy the <Configuration> xml code snippet and add it to below the <version>tag.

Step 5: Finally, the POM.xml code configuration looks as shown below.

<build>
     <plugins>
                    <Plugin>
                                 <groupId>org.maven.plugins</groupId>
	      <artifactId>maven-surefire-plugin</artifactId>
	      <version>2.20</version>
	      <configuration>
	      <suiteXmlFiles>
	      <suiteXmlFile>testng.xml</suiteXmlFile>
	      </suiteXmlFiles>
	      </configuration>
                    </plugin>
     </plugins>
</build>

Executing Test Suite Using Maven Surefire Plugin

Step 1: Select any script(LoginLogoutTest), Right-click and Select TestNG-> Test. Here we are trying to run the Batch execution using TestNG.

Batch execution using TestNG

Step 2: XML file will be generated in the Temp folder. Rename the file as fullRegressionsuite.xml (Renaming it for our convenience).

fullRegressionsuite.xml

Step 3: Create a class name for each script and add under the <classes> tag.

Create a class name

Step 4: In the POM.xml file, name the fullRegressionsuite.xml in the <suiteXmlFile> tag.

  • It is the test suite that contains an XML file of the TestNG that is to be triggered by Maven.
  • We can have any number of test suites in the <suiteXmlFile> tag. So that the Scripts we have in each suite will be executed.

Name fullRegressionsuite.xml in <suiteXmlFile> tag

Step 5: Right-click on POM.xml => Run => Maven test.

Run as Maven test.

Step 6: Regression Test Suite is successfully executed and we can see the output in the Console window.

Output in Console window

Step 7: Refresh the entire project and the test suite Report can be seen in the target folder of the Project Explorer window.

Project Explorer window

Step 8: Execution report showing all the information about the test suite is displayed.

Execution report

Conclusion

Maven Surefire plugin helps us to manage our dependencies and select & execute particular test scripts or suites using TestNG.

Thus, in this tutorial, we have achieved the Integration of Maven with TestNg.

Happy Reading!!

=> Visit Here To See The TestNG Training Series For All.

Was this helpful?

Thanks for your feedback!

Leave a Comment