Reputation: 13
testng.xml file
<test name="Regression 1">
<parameter name="group" value="regression"></parameter>
<groups>
<run>
<include name = "regression"/>
</run>
</groups>
<packages>
<package name="com.turvo.tests.regressiontests.regression1.*">
</package>
</packages>
</test>
<test name="Regression 2">
<parameter name="group" value="regression"></parameter>
<groups>
<run>
<include name = "regression"/>
</run>
</groups>
<packages>
<package name="com.turvo.tests.regressiontests.regression2.*">
</package>
</packages>
</test>
I want parameterize testng.xml such that I can run only Regression 1 tests or Regression 2 tests or if required both by passing variable from command line. Is there any approach to achieve this?
Upvotes: 1
Views: 957
Reputation: 14746
There are basically two ways of achieving this.
Using the Maven surefire plugin
To achieve this via Maven surefire plugin, configure your surefire plugin to look like below.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
</suiteXmlFiles>
<properties>
<property>
<name>testnames</name>
<value>${tests}</value>
</property>
</configuration>
</plugin>
Within your pom.xml's <properties>
section, add properties as below
<properties>
<suiteXmlFile>src/test/resources/suite.xml</suiteXmlFile>
<tests>48341304_Test1</tests>
</properties>
Lets say your suite xml looks like below
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="48341304_Suite" parallel="false" verbose="2">
<test name="48341304_Test1" verbose="2">
<classes>
<class name="com.rationaleemotions.stackoverflow.qn48341304.TestClassOne"/>
</classes>
</test>
<test name="48341304_Test2" verbose="2">
<classes>
<class name="com.rationaleemotions.stackoverflow.qn48341304.TestClassTwo"/>
</classes>
</test>
</suite>
You can now choose which <test>
you want to execute via the maven command line : mvn test -Dtests=48341304_Test2
Caveat
The only catch with this is that, you will mandatorily have to provide some value for the JVM argument -Dtests
(That is one of the reasons why I have defined a default value for the tag <tests>
in the <properties>
section.
Using a beanshell expression as shown below
Here's how the suite xml file would look like:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="48341304_Suite" parallel="false" verbose="2">
<test name="48341304_Test1" verbose="2">
<method-selectors>
<method-selector>
<script language="beanshell">
<![CDATA[
whatTest = System.getProperty("testToRun");
print(whatTest)
if (whatTest == null || whatTest.trim().isEmpty()) return true;
whatTest.contains(testngMethod.getXmlTest().getName());
]]>
</script>
</method-selector>
</method-selectors>
<classes>
<class name="com.rationaleemotions.stackoverflow.qn48341304.TestClassOne"/>
</classes>
</test>
<test name="48341304_Test2" verbose="2">
<classes>
<class name="com.rationaleemotions.stackoverflow.qn48341304.TestClassTwo"/>
</classes>
</test>
</suite>
Now you can run this suite xml file even via your favorite IDE and this suite xml file is now agnostic of the build tool (It would work with Maven (or) Gradle (or) Ant). For you to choose which <test>
tag that needs to be run, pass on the value via the JVM argument -DtestToRun
Here's a snapshot of the run configurations from IntelliJ
Upvotes: 1