使用Beanshell引用testng.xml中定义的参数

时间:2019-01-23 20:30:19

标签: java xml jenkins testng

我正在尝试使用一个参数来确定将运行TestNG套件中的哪组测试。为此,我的testng.xml文件当前如下所示。

<?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> 

这个想法是groupToRun的值是通过触发该测试套件的Jenkins作业传递的。然后,Beanshell读取参数以确定应该运行哪个组。

问题是我不知道如何引用在testng.xml文件的参数标签中定义的参数,并且找不到任何文档来说明如何执行此操作。

有人知道如何使用Beanshell引用testng.xml文件中定义的参数吗?

1 个答案:

答案 0 :(得分:1)

引用here的TestNG文档

  

TestNG为方便起见定义了以下变量:

     
      
  • java.lang.reflect.Method method:当前的测试方法。
  •   
  • org.testng.ITestNGMethod testngMethod:当前测试方法的说明。
  •   
  • java.util.Map<String, String> groups:当前测试方法所属的组的映射。
  •   

因此,您只需通过ITestNGMethod对象提取参数即可。

这是您的操作方式

<?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>