使用ITestContext在Testng中数据提供者

时间:2017-08-21 06:24:14

标签: testng dataprovider testng-dataprovider

我正在尝试使用ITestContext和使用xml的组使用数据提供程序但我收到错误并且不确定如何解决它。有人可以检查并告诉我。

要运行的实际类低于使用另一个类

中的数据提供程序类
package dataProvider;

import org.testng.annotations.Test;

public class DataProviderClassWithITestContextExample {


@Test(dataProvider = "NumberInputs", groups = "Group20", dataProviderClass= myDataProviderWithITestContextClass.class)
public void sumOfTwoNumbersMethodOne(int num1, int num2 ) {

    int sum = num1 + num2;

    System.out.println(" Sum of two numbers is " + sum);

}

@Test(dataProvider = "NumberInputs", groups = "Group20", dataProviderClass= myDataProviderWithITestContextClass.class)
public void sumOfTwoNumbersMethodThree(int num1, int num2 ) {

    int sum = num1 + num2;

    System.out.println(" Sum of two numbers is " + sum);

}

@Test(dataProvider = "NumberInputs", groups = "Group21", dataProviderClass= myDataProviderWithITestContextClass.class)
public void sumOfTwoNumbersMethodTwo(int num1, int num2 ) {

    int sum = num1 + num2;

    System.out.println(" Sum of two numbers is " + sum);

}
}

数据提供者类如下所示:

package dataProvider;

import org.testng.ITestContext;
import org.testng.annotations.DataProvider;

public class myDataProviderWithITestContextClass {

@DataProvider(name = "NumberInputs")
public Object[][] getDataFromDataprovider(ITestContext c) {
    Object[][] groupArray=null;
        for(String group : c.getIncludedGroups()) {
            if(group.equalsIgnoreCase("Group20")) {
                groupArray = new Object[][] {
                    { 22, 33 }, 
                    { 12, 13 } 
                };
                break;
            }
            else if(group.equalsIgnoreCase("Group21")) {
                groupArray = new Object [][] {
                    { 10, 20 }, 
                    { 40, 20 } 
                };
            //  break;
            }
            break;
        }
            return groupArray;
        }
}

2 个答案:

答案 0 :(得分:2)

您需要将groupArray初始化为有效数组。

所以请添加:

Object[][] groupArray = new Object[0][0];

正如@niharika_neo指出的那样,您的数据提供者返回null值。

TestNG中的错误消息绝对可以即兴创作,并将在下一个TestNG版本中提供:https://github.com/cbeust/testng/issues/1509

答案 1 :(得分:-1)

你需要在课堂上使用@Guice

@Guice
public class DataProviderClassWithITestContextExample {
 ...

为此,如果没有,需要jars javax.inject-1.jar和guice-3.0.jar