如何将参数传递给testng.xml中的组执行的方法?

时间:2018-01-24 08:00:08

标签: testng

我有两种测试方法

@Test (groups = {"group1"})
public test1(){
}
@Test (groups = {"group1"})
public test2(){
}

现在我想使用testng.xml中的组来执行这些测试用例

例如

<suite>
  <test>
    <groups> 
        <run>
           <include name="group1"/>
        </run>
    </groups>
  </test>
</suite>

现在我想将参数传递给test1和test2,就像

一样
<include name="test1"><parameter name="abc"  value="ABC"/></include>
<include name="test2"><parameter name="def"  value="DEF"/></include>

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

您可以使用带有Method类的dataprovider将diff参数传递给您的测试方法。请参阅此link

 @Test (groups = {"group1"},dataProvider="test")
    public test1(){
    }
    @Test (groups = {"group1"},dataProvider="test")
    public test2(){
    } 

    @DataProvider(name="test")
        public Object[][] getDataFromDataprovider(Method m){
            if(m.getName().equalsIgnoreCase("test1")){
            return new Object[][] {
                    {  "ABC" },

                };}
            else{
            return new Object[][] {
                    { "DEF" },

                };}       
        }
    }

答案 1 :(得分:0)

如果要从testng xml文件传递参数。你已经在test中定义了参数,并从xml传递它,如下所示。

@Test (groups = {"group1"})
@Paramters({"parameter1"})
public test1(String arg1){
   System.out.println(arg1);
}

@Test (groups = {"group1"})
@Paramters({"parameter2"})
public test2(String arg2){
   System.out.println(arg2);

}

您可以从xml传递它,如下所示。

<suite>
  <test>
    <parameter name="parameter1" value="test1"/>
    <parameter name="parameter2" value="Test2"/>
    <groups> 
        <run>
           <include name="group1"/>
        </run>
    </groups>
  </test>
</suite>

您可以使用数据提供程序传递值,但是从源代码中获取值,无需在testNG xml中提及。