TestNG:一个@Test 有多个@DataProvider

我正在为 Eclipse 使用 TestNG.

是否可以逐步将两个数据提供者提供给相同的测试功能?

Is it possible to give two data providers step by step to the same test-function?

我可以将两个提供商合二为一,但这不是我想要的.

I could put both providers in one, but that is not what I want.

我需要(不像本例那样)独立生成数据.

I need (not like in this example) to generate independently data.

@DataProvider(name = "dataSet1")
public Object[][] createDataX() {
    return new Object[][] { { 1, 1 }, { 2, 2 } };
}

@DataProvider(name = "dataSet2")
public Object[][] createDataY() {
    return new Object[][] { { 0, 0 }, { 3, 3 } };
}

我想让两个提供者都接受相同的测试.这可能吗?

I want to give both providers to the same test. Is this possible?

@Test(dataProvider = "dataSet1") // ??? and "dataSet2" ???
public void testThisFunction(int val1, int val2) {
    boolean solution = oracle(val1,val2);
    assert (solution);
}

推荐答案

不,但没有什么能阻止您将这两个数据提供者合并为一个并将其指定为您的数据提供者:

No, but nothing stops you from merging these two data providers into one and specifying that one as your data provider:

public Object[][] dp1() {
  return new Object[][] {
      new Object[] { "a", "b" },
      new Object[] { "c", "d" },
  };
}

public Object[][] dp2() {
  return new Object[][] {
      new Object[] { "e", "f" },
      new Object[] { "g", "h" },
  };
}

@DataProvider
public Object[][] dp() {
  List<Object[]> result = Lists.newArrayList();
  result.addAll(Arrays.asList(dp1()));
  result.addAll(Arrays.asList(dp2()));
  return result.toArray(new Object[result.size()][]);
}

@Test(dataProvider = "dp")
public void f(String a, String b) {
  System.out.println("f " + a + " " + b);
}

相关文章