简体   繁体   中英

several dataProvider per one Test in TestNG

I want run same tests for different implementations. In this case I should provide two dataProvider. One for implementations and other for extra data. So I wrote smt like

@DataProvider(name = "ind")
public Object[][] indexes(){
    return new Object[][]{{1}, {2.5}};
}

@DataProvider(name = "str")
public Object[][] strings(){
    return new Object[][]{{"a11", "a12"}, {"a21", "a22"}};
}

 public Object[][] decart(Object[][] a1, Object[][] a2){
    List<Object[]> rez = new LinkedList();
    for(Object[] o : a1){
        for(Object[] o2 : a2){
            rez.add(concatAll(o, o2));
        }
    }
     return rez.toArray(new Object[0][0]);
}

//in future, probably, I will need do decart for varargs two.
public static <T> T[] concatAll(T[] first, T[]... rest) {
  int totalLength = first.length;
  for (T[] array : rest) {
    totalLength += array.length;
  }
  T[] result = Arrays.copyOf(first, totalLength);
  int offset = first.length;
  for (T[] array : rest) {
    System.arraycopy(array, 0, result, offset, array.length);
    offset += array.length;
  }
  return result;
}

@DataProvider(name = "mixed")
public Object[][] mixed(){
    return decart(indexes(),  strings());
}


@Test(dataProvider = "mixed")
public void someTest(Number i, String s1, String s2){
    System.out.println(i + "\t" + s1 + "\t" + s2);
}

But this code seems very unnatural.. I think I've done it in wrong way. How I should do thinks like this?

不,差不多就是这样:如果您想组合多个数据提供者,您可以创建另一个数据提供者来调用其他数据提供者并合并结果。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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