使用单个测试方法使用多个DataProvider的TestNG
我一直在寻找一种在测试方法中使用多个DataProvider的方法。我的情况如下:
假设我们有一个DataProvider类:
@Test
public class ExampleDataProvider {
/**
* Returns the list of shape codes.
*
* @return the collection shape codes.
*/
@DataProvider(name = "ShapeCodes")
public static Object[][] getShapeCodes() {
return new Object[][] { new Object[] { Shape.Square },
new Object[] { Shape.Triangle }
};
}
/**
* Returns the list of color codes.
*
* @return the collection of color codes.
*/
@DataProvider(name = "ColorCodes")
public static Object[][] geColorCodes() {
return new Object[][] { new Object[] { Color.Green },
new Object[] { Color.Red }
};
}
}
现在,在我的Test方法中,我要针对方案的所有组合运行:
- 绿广场
- 红方格
- 绿三角
- 红三角
鉴于我无法使用@Test
注释指定多个DataProvider,因此应如何在代码中实现此目标
@Test(dataProvider = "ShapeCodes", dataProviderClass = ExampleDataProvider.class)
public void test(String ShapeCode, String ColorCode) throws IOException {
.............
/* tests for color shape combination */
.............
}
-
由于缺乏更好的方法,我决定坚持解决方法。这是如何实现上述方案的示例:
@Test public class ExampleDataProvider { /** * Returns the list of shape codes. * * @return the collection shape codes. */ @DataProvider(name = "ShapeCodes") public static Object[][] getShapeCodes() { return new Object[][] { new Object[] { Shape.Square }, new Object[] { Shape.Triangle } }; } /** * Returns the list of color codes. * * @return the collection of color codes. */ @DataProvider(name = "ColorCodes") public static Object[][] geColorCodes() { return new Object[][] { new Object[] { Color.Green }, new Object[] { Color.Red } }; } /** * Returns the list object codes providing a color shape combination. * * @return the collection of object codes. */ @DataProvider(name = "objectCodes") public static Object[][] getObjectCodes(){ return combine(geColorCodes(), getShapeCodes()); } /** * Returns the list of combination of color and shape codes. * * @return the collection of combined color and shape codes. */ public static Object[][] combine(Object[][] a1, Object[][] a2){ List<Object[]> objectCodesList = new LinkedList<Object[]>(); for(Object[] o : a1){ for(Object[] o2 : a2){ objectCodesList.add(concatAll(o, o2)); } } return objectCodesList.toArray(new Object[0][0]); } @SafeVarargs public static <T> T[] concatAll(T[] first, T[]... rest) { //calculate the total length of the final object array after the concat int totalLength = first.length; for (T[] array : rest) { totalLength += array.length; } //copy the first array to result array and then copy each array completely to result 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; } }
这样,我就可以分别使用我的颜色代码和形状代码,并且还可以组合使用。
因此,我的测试方法如下所示:
@Test(dataProvider = "objectCodes", dataProviderClass = ExampleDataProvider.class) public void test(String ShapeCode, String ColorCode) throws IOException { ............. /* tests for color shape combination */ ............. } @Test(dataProvider = "ShapeCodes", dataProviderClass = ExampleDataProvider.class) public void test(String ShapeCode) throws IOException { ............. /* tests for shapes */ ............. } @Test(dataProvider = "ColorCodes", dataProviderClass = ExampleDataProvider.class) public void test(String ColorCode) throws IOException { ............. /* tests for colors */ ............. }