TestNG使用多个DataProviders和单一测试方法

时间:2014-08-28 12:59:29

标签: java testing automation testng

我一直在寻找一种在我的测试方法中使用多个DataProviders的方法。我的方案如下:

假设我们有一个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 }
        };
    }
}

现在,在我的测试方法中,我希望针对所有场景组合运行:

  1. 绿方
  2. 红方
  3. 绿三角
  4. 红三角
  5. 我应该如何在我的代码中实现这一点,因为我无法使用@Test注释指定多个DataProviders

    @Test(dataProvider = "ShapeCodes", dataProviderClass = ExampleDataProvider.class)
        public void test(String ShapeCode, String ColorCode) throws IOException {
            .............
            /* tests for color shape combination */
            .............
        }
    
    编辑:我发现了一个类似的问题和一个@ workaround,但我仍然想知道是否有更好的方法来解决这个问题。

3 个答案:

答案 0 :(得分:9)

由于缺乏更好的方法,我决定坚持解决方法。以下是如何实现上述方案的示例:

@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 */
        .............
    }

答案 1 :(得分:2)

很好的答案分段

也许有人也会需要它,我以不同的方式重写了这个方法。

// public static <T> T[] concatAll(T[] first, T[]... rest) {

public static Object[] concat(Object[] first, Object[] second) {
        Object[] result = ArrayUtils.addAll(first, second);
        return result;

答案 2 :(得分:1)

https://stackoverflow.com/a/59993761/5016494

这可能会帮助

    @DataProvider
    public Object[][] combinedDataProvider() {
        return Stream.of(dp1(), dp2())
                .flatMap(Arrays::stream)
                .toArray(Object[][]::new);
    }