How to call testng.xml file from pom.xml in Maven?
Last Updated :
22 Aug, 2024
Maven is a tool that is widely used for automation whenever developers work on Java projects. apart from the Java language, the Maven software supports other projects that are written in C#, Ruby, etc. The Maven project is controlled and hosted by the famous Apache software foundation.
What is a testng.xml file?
Testng.xml is a popular file that is used in the maven software or project, it is provided by the TestNG testing framework and it is used to define and control the execution of the tests. the testng.xml file is used to specify the test classes as well as the test methods to run and also provides other settings for the testing.
What is a pom.xml file?
The pom.xml file stands for the project object model and it is a file that is used for the configuration of the project, it is used for defining the project's structure and dependencies, as well as other configurations for the project.
How to call the testng.xml file from pom.xml in Maven?
Following are the steps required for calling the testng.xml file from pom.xml:
Step 1: Create java file:
The first step is to create a testng.xml file for the original java file, for this, we will need to have a java file.
Note: If you currently do not have a java file then you can use the following java file, it is not mandatory to have the file name as testng.xml
Java
package mynewpackage;
import org.testng.annotations.Test;
public class MyCases {
@Test
public void testOne() {
System.out.println("this is my first test");
}
@Test
public void testTwo() {
System.out.println("this is my second test");
}
@Test
public void testThree() {
System.out.println("this is my third test");
}
@Test
public void testFour() {
System.out.println("this is my fourth test");
}
@Test
public void testFive() {
System.out.println("this is my fifth test");
}
@Test
public void testSix() {
System.out.println("this is my sixth test");
}
}
Step 2: Create testng.xml file:
After creating or copying code for the java file, we will have to create the testng.xml file, for this simply right click on the java file and go to TestNG > Convert to TestNG and click on finish. it will automatically create the testng.xml file for us.
Create testng.xml file.If you are using the above java code then the following testng.xml file will be generated (it will differ if the java code is different):
XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test thread-count="5" name="Test">
<classes>
<class name="mynewpackage.MyCases"/>
</classes>
</test> <!--Test -->
</suite> <!-- Suite -->
Step 3: Run pom.xml file:
Next step is to run the pom.xml file, unlike the testng.xml file the pom.xml file will be already present inside the maven project, as you can see in the image below:
Run pom.xml file.pom.xml file will contain the following code in it:
XML
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ats</groupId>
<artifactId>RuntestngXmlFromPOM</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>RuntestngXmlFromPOM</name>
<url>http://maven.apache.org</url>
<properties>
<project build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testngsecond.xml</suiteXmlFile>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</project>
Step 4: Call testng.xml from pom.xml:
Now the next step is to run the testng.xml file using the pom.xml for this right click on the pom.xml file and click on the maven run option.
Call testng.xml from pom.xmlStep 5: Test output:
Once you click on the maven run option, it will display a small pop up window in which the output will be displayed, it will be similar to the output below:
Test output.This means that we have successfully called the testng.xml file using the pom.xml file in the maven project.
Conclusion
This is a method which allows the maven to execute the various tests which are defined within the testng.xml file, this setup uses the TestNG and it integrates it with the maven software which enables the test execution to happen automatically during the build process and also helps us in making sure that the test management is more efficient.
Similar Reads
How to Run a testng.xml File From Batch for a Maven Project?
In a Maven project, testng.xml is a configuration file used by TestNG to define and organize which tests to run. To automate the process of running these tests, you can use a batch file. A batch file is a simple script that automates command-line tasks. Why Use a Batch File?A batch file allows you t
3 min read
How to Pass Parameter from TestNG.xml File?
Parameters in TestNG allow you to configure test methods flexibly and make them more maintainable and reusable. The parameters feature allows you to pass data directly from the testng.xml file to test methods. This increases reusability at the suite level without the need to hard code values into th
3 min read
How to pass Parameter to testng.xml from Command Line with Maven?
In test automation, we run tests across multiple web browsers. The TestNG with Maven allows us to pass the parameters through the command line dynamically; this enables us to control various test environments without editing the code again and again. Step to Pass Parameter to testng.xml from Command
3 min read
How to exclude TestNG Groups from Maven?
To exclude specific TestNG groups from execution when running tests through Maven, you can configure the maven-surefire-plugin in the Maven pom.xml file. The Surefire plugin is responsible for running the unit tests during the build process, and it supports excluding certain groups from being execut
2 min read
How to create TestNG XML file and Execute TestNG.XML File?
TestNG is a powerful testing framework for Java developers, inspired by JUnit and NUnit. It supports a wide range of testing needs, from unit testing to integration testing. One of the key features of TestNG is the ability to control the execution of tests using an XML file, known as the TestNG XML
4 min read
How to execute TestNG and Maven without testng.xml file?
To run TestNG tests with Maven, it is not mandatory to configure any testng.xml. Instead of this you just let Maven do the execution with maven-surefire-plugin of the Maven build lifecycle and for that you don't need to define any test suite, classes, or methods in testng.xml. The maven-surefire-plu
2 min read
How to run TestNG from command line?
Running TestNG from the command line is a powerful technique for managing your automated tests efficiently. TestNG is a popular testing framework that provides various features for running and configuring tests. While IDE-based execution is common, using the command line offers greater flexibility a
4 min read
How to pass java code a parameter from Maven for testing?
Maven is one of the most commonly used tools in Java projects for build automation, allowing the developer to manage the dependency of their project and many other operations, like compiling codes or running tests. Sometimes, you may want to pass some parameters from Maven to the Java code, so you c
3 min read
How to Run Specific TestNG Suite with Maven from Command Line?
When using Maven as your build automation tool and TestNG for Java-based testing, you may need to execute particular test suites instead of running every test. Running test subsets according to functionality, environment, or any other criteria can be helpful in this regard. You must set up your Mave
2 min read
How to use regular expression in class path in TestNG.xml?
TestNG provides a regular expression in class path features for the testng.xml file. It is useful when you want to run lots of test classes from a package conforming to a particular pattern without listing each class in TestNG. Approach to Using Regular Expressions in Class-Path in testng.xmlTestNG
3 min read