java TestNG 使用多个 DataProvider 和单个测试方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25549443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:12:31  来源:igfitidea点击:

TestNG using multiple DataProviders with single Test method

javatestingautomationtestng

提问by Segmented

I have been looking for a way to use multiple DataProviders in my test method. My scenario is as follows:

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

lets say we have a DataProvider class:

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

Now in my Test method I want to run for all combinations of scenarios:

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

  1. Green-Square
  2. Red-Square
  3. Green-Triangle
  4. Red-triangle
  1. 绿色广场
  2. 红方格
  3. 绿三角
  4. 红三角

How should I achieve this in my code, given that I cant specify multiple DataProviders with @Testannotation

鉴于我无法使用@Test注释指定多个 DataProviders,我应该如何在我的代码中实现这一点

@Test(dataProvider = "ShapeCodes", dataProviderClass = ExampleDataProvider.class)
    public void test(String ShapeCode, String ColorCode) throws IOException {
        .............
        /* tests for color shape combination */
        .............
    }

EDIT : I found a similar problem and a @ workaroundbut I am still wondering if there are better ways to handle this.

编辑:我发现了一个类似的问题和一个 @解决方法,但我仍然想知道是否有更好的方法来处理这个问题。

回答by Segmented

For the lack of a better approach, I decided to stick to the workaround. here is an example of how the above scenario could be implemented:

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

@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;
    }
}

This way I get to use my Color codes and Shape codes separately and also provides me to use the combination.

通过这种方式,我可以分别使用我的颜色代码和形状代码,并且还为我提供了使用组合的机会。

So, my test methods would look like:

所以,我的测试方法看起来像:

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

回答by Alex

Great answer Segmented

很好的答案

Maybe someone will need it too, I rewrote this method in a different way.

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

// 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;

回答by Wee

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

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

This may help

这可能有帮助

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