
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Pass Parameter to TestNG XML from Command Line with Maven
TestNG is a testing framework and can use Maven as build tool. It helps to maintain dependencies and their version at one place in pom.xml
Maven provides flexibility to run using surefire plugin. Surefire plugin has feature to pass parameter from command line to testng.xml. To achieve this, user should have to define parameter in testng.xml with default value. At run time, the user must send to parameter value to override default value. In this way, the value passed in command line take the preference and TestNG class uses it. If the value is not passed from command line, then the default value is used.
There is another way to access the parameter in TestNG class is System.getProperty("propertyName"). The parameter sends in maven's command line is saved as system property and can be used anywhere in the code.
In this tutorial, we will illustrate how to pass a parameter to testng.xml from command line with maven.
Approach/Algorithm to solve this problem
Step 1: Create a TestNG classes ? NewTest
Step 2: Write 2 @Test method in the class.
Step 3: Mention testng.xml path into pom.xml file. Please make sure systemPropertyVariables are not mentioned in pom.xml.
Step 4: Write a logic or parameterize the code to access this variable.
Step 5: Create testng.xml and mention the parameter that needs to be send through command line as shown in program section.
-
Step 6: Now pass the value of the variable at run time in command.
mvn test ?Denvironment=UAT
Step 7: Now, verify whether the same value of the variable is accessed or not.
Example
The following code to show how to run only 1 test method from a large suite:
src/test/java/NewTest.java
import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class NewTest { @Test() public void testCase1() { System.out.println("in test case 1 of NewTest"); System.out.println("Regression " + System.getProperty("environment")); } @Parameters("environment") @Test() public void testCase2(String environment) { System.out.println("in test case 2 of NewTest"); System.out.println("Environment name is- "+environment); } }
pom.xml
This is a maven configuration file that is used to organize dependencies, plugins and run the TestNG test cases.
It is very handy when limited tests are needed to execute rather than full suite.
<?xml version="1.0" encoding="UTF-8"?> <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.sample</groupId> <artifactId>TestNGProjectct</artifactId> <version>1.0-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <configuration> <suiteXmlFiles> <suiteXmlFile>src/main/java/testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> </plugins> </build> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.3.0</version> </dependency> </dependencies> </project>
testng.xml
This is a configuration file that is used to organize and run the TestNG test cases.
It is very handy when limited tests are needed to execute rather than full suite.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="suite" parallel="none" verbose="5"> <test name="test"> <parameter name="environment" value="QA"/> <classes> <class name="NewTest"/> </classes> </test> </suite>
Command to Run specific suite
Please note if multiple suits are required to exclude simply write all suits name separated by commas
mvn clean test -Denvironment=UAT
Output
[INFO] Scanning for projects... [INFO] [INFO] ---------------------< com.sample:TestNGProjectct >--------------------- [INFO] Building TestNGProjectct 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ TestNGProjectct --- [INFO] Deleting C:\Users\anandas\IdeaProjects\TestNGProjectct\target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ TestNGProjectct --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ TestNGProjectct --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 8 source files to C:\Users\anandas\IdeaProjects\TestNGProjectct\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ TestNGProjectct --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ TestNGProjectct --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to C:\Users\anandas\IdeaProjects\TestNGProjectct\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ TestNGProjectct --- [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running TestSuite ... ... TestNG 7.3.0 by Cédric Beust ([email protected]) ... in test case 1 of NewTest Regression UAT in test case 2 of NewTest Environment name is- UAT [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.619 s - in TestSuite [INFO] [INFO] Results: [INFO] [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] -------------------------------------------------------- [INFO] BUILD SUCCESS [INFO] -------------------------------------------------------- [INFO] Total time: 3.443 s [INFO] Finished at: 2022-02-11T10:52:24+05:30 [INFO] -------------------------------------------------------- Process finished with exit code 0