Reputation: 4240
I am trying to use a parameter to determine which group of tests in my TestNG suite will be run. To do that, my testng.xml file currently looks like this.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test">
<parameter name="groupToRun" value="${valueFromJenkins}" />
<method-selectors>
<method-selector>
<script language="beanshell"><![CDATA[
return groups.containsKey(groupToRun);
]]></script>
</method-selector>
</method-selectors>
<classes>
<class name="main.java.CWV_Functional.CWV_Functionals" />
</classes>
</test>
</suite>
The idea is that the value of groupToRun is passed from a Jenkins job triggering this test suite. Beanshell then reads the parameter to determine which group should be run.
The problem is I do not know how to reference a parameter defined in parameter tags of the testng.xml file and cannot find any documentation showing how to do this.
Does anyone know how to use Beanshell to reference a parameter defined in the testng.xml file?
Upvotes: 1
Views: 803
Reputation: 14746
Quoting the TestNG documentation from here
TestNG defines the following variables for your convenience:
java.lang.reflect.Method
method
: the current test method.org.testng.ITestNGMethod
testngMethod
: the description of the current test method.java.util.Map<String, String>
groups
: a map of the groups the current test method belongs to.
So you just extract out the parameters via the ITestNGMethod
object.
Here's how you do it
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="54335160_suite" parallel="false" verbose="2" configfailurepolicy="continue">
<parameter name="groupToRun" value="foo"/>
<method-selectors>
<method-selector>
<script language="beanshell"><![CDATA[
grpParameter = testngMethod.getXmlTest().getParameter("groupToRun");
return groups.containsKey(grpParameter);
]]></script>
</method-selector>
</method-selectors>
<test name="54335160_test">
<classes>
<class name="com.rationaleemotions.stackoverflow.qn54335160.Qn54335160Sample">
</class>
</classes>
</test>
</suite>
Upvotes: 1