简体   繁体   English

自定义类数组作为TestNG中的@DataProvider

[英]Custom class arrays as @DataProvider in TestNG

I wanted to know if there is a way to package all parameters to a @Test method into one class Name and provide Name[] as the @DataProvider ? 我想知道是否有一种方法可以将@Test方法的所有参数打包到一个类Name并提供Name[]作为@DataProvider吗?

public class FirstTestClass {

  @Test (dataProvider = "getNames")
  public void test01(Name name) {
    System.out.println(name.name + " " + name.id);
  }
  @DataProvider
  public Name[] getNames() {
      Name[] result = new Name[2];
      result[0] = new Name("john", 5);
      result[1] = new Name("doe", 4);
      return result;
  }
}

class Name {
    public String name;
    public Integer id;
    public Name(String name, Integer id) {
        this.name = name;
        this.id = id;
   }
}

The test is getting skipped and I get a message saying must return either Object[][] or Iterator<>[] . 测试被跳过,我收到一条消息,提示must return either Object[][] or Iterator<>[] This is kind of limited right? 这是有限的权利吗? Any workaround for this? 任何解决方法?

Why are you not able to do the same thing using two dimensional array with only 1 column. 为什么仅使用一列的二维数组无法执行相同的操作。 Is there any specific requirement that you have? 您有什么具体要求吗? Something like this 像这样

@Test(dataProvider = "getNames")
public void test01(Name name) {
    System.out.println(name.name + " " + name.id);
}

@DataProvider
public Object[][] getNames() {
    Name[][] result = new Name[2][1];
    result[0][0] = new Name("john", 5);
    result[1][0] = new Name("doe", 4);
    return result;
}

I tested your code with the change I am suggesting it works just fine. 我用所做的更改测试了您的代码,我建议它可以正常工作。

Change it to 更改为

@DataProvider
public Name[][] getNames() {
   Name[][] result = new Name[2][];
   result[0] = {new Name("john", 5)};
   result[1] = {new Name("doe", 4)};
   return result;
}

I don't know why you use a dedicated class to hold all attributes, but you should consider something like: 我不知道为什么要使用专用的类来保存所有属性,但是您应该考虑以下内容:

public class FirstTestClass {

  @Test(dataProvider = "getNames")
  public void test01(String name, Integer id) {
    System.out.println(name + " " + id);
  }

  @DataProvider
  public Object[][] getNames() {
    return new Object[][] {
      new Object[] { "john", 5 },
      new Object[] { "doe", 4 }
    }
  }
}

In JUnit 4.x, the following is the way to achieve the above scenario. 在JUnit 4.x中,以下是实现上述方案的方法。

@RunWith(Parameterized.class)
public class FirstTest {
    private LoginCredentials loginData;

    public FirstTest(LoginCredentials testData) {
        loginData = testData;
    }

@Parameterized.Parameters
public static LoginCredentials[] generateTestData() {
    return ReadingExcelSheets.generateTestData();
}

I was hoping for something similar in TestNG, which I was told is more flexible and powerful than JUnit. 我一直希望在TestNG中有类似的东西,听说它比JUnit更灵活和强大。 Turns out, we have return only Object[][] in the @DataProvider method. 事实证明,我们在@DataProvider方法中仅返回Object [] []。

As I understand your question now, you'd like a TestNG translation of: 据我了解您的问题,您想要以下内容的TestNG翻译:

@RunWith(Parameterized.class)
public class FirstTest {
    private LoginCredentials loginData;

    public FirstTest(LoginCredentials testData) {
        loginData = testData;
    }

    @Parameterized.Parameters
    public static LoginCredentials[] generateTestData() {
        return ReadingExcelSheets.generateTestData();
    }
}

A solution could be: 解决方案可能是:

public class FirstTest {

    private final LoginCredentials loginData;

    @Factory(dataprovider = "generateTestData")
    public FirstTest(LoginCredentials testData) {
        loginData = testData;
    }

    @Test
    public void test01() {
        System.out.println(loginData);
    }

    @DataProvider
    public static Object[][] generateTestData() {
        return convert(ReadingExcelSheets.generateTestData());
    }

    public static Object[][] convert(LoginCredentials[] values) {
        Object[][] result = new Object[values.length];
        for (int i=0; i<result.length; i++) {
            result[i] = new Object[]{ values[i] };
        }
        return result;
    }
}

Or 要么

public class FirstTest {

    @Test(dataprovider = "generateTestData")
    public void test01(LoginCredentials loginData) {
        System.out.println(loginData);
    }

    @DataProvider
    public static Object[][] generateTestData() {
        return convert(ReadingExcelSheets.generateTestData());
    }

    public static Object[][] convert(LoginCredentials[] values) {
        Object[][] result = new Object[values.length];
        for (int i=0; i<result.length; i++) {
            result[i] = new Object[]{ values[i] };
        }
        return result;
    }
}

But ok, TestNG could provide the convert method itself and/or manage Object[] as a valid return type out-of-the-box. 但是好吧,TestNG可以提供convert方法本身和/或将Object[]作为开箱即用的有效返回类型进行管理。

Feel free to propose a patch on http://github.com/cbeust/testng Help is always welcome :) 随时在http://github.com/cbeust/testng上提出一个补丁。欢迎随时提供帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM