繁体   English   中英

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

[英]Custom class arrays as @DataProvider in TestNG

我想知道是否有一种方法可以将@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;
   }
}

测试被跳过,我收到一条消息,提示must return either Object[][] or Iterator<>[] 这是有限的权利吗? 任何解决方法?

为什么仅使用一列的二维数组无法执行相同的操作。 您有什么具体要求吗? 像这样

@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;
}

我用所做的更改测试了您的代码,我建议它可以正常工作。

更改为

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

我不知道为什么要使用专用的类来保存所有属性,但是您应该考虑以下内容:

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

在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();
}

我一直希望在TestNG中有类似的东西,听说它比JUnit更灵活和强大。 事实证明,我们在@DataProvider方法中仅返回Object [] []。

据我了解您的问题,您想要以下内容的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();
    }
}

解决方案可能是:

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;
    }
}

要么

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;
    }
}

但是好吧,TestNG可以提供convert方法本身和/或将Object[]作为开箱即用的有效返回类型进行管理。

随时在http://github.com/cbeust/testng上提出一个补丁。欢迎随时提供帮助:)

暂无
暂无

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

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