在 TestNG 中每个测试有几个 dataProvider [英] several dataProvider per one Test in TestNG
本文介绍了在 TestNG 中每个测试有几个 dataProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为不同的实现运行相同的测试.在这种情况下,我应该提供两个 dataProvider.一个用于实现,另一个用于额外数据.所以我写了 smt 就像
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?
推荐答案
不,差不多就是这样:如果您想组合多个数据提供者,您可以创建另一个数据提供者来调用其他数据提供者并合并结果.
No, that's pretty much it: if you want to combine several data providers, you create another data provider that calls the others and merges the results.
这篇关于在 TestNG 中每个测试有几个 dataProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文