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

标签 java testing automation 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 }
        };
    }
}

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

  1. 绿色方 block
  2. 红场
  3. 绿色三角形
  4. 红色三角形

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

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

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

最佳答案

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

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

关于java - TestNG 使用多个 DataProvider 和单个测试方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25549443/

相关文章:

java - socket.shutdownOutput() 的目的

java - 使用 RandomAccessFile 和 BufferedReader 来加速文件读取

unit-testing - Elixir : test has_many association

java - 如何使用 Java ProcessBuilder 自动执行命令行脚本

Eclipse 不会自动将 servlet 添加到 web.xml 中?

java - java中通过流实现对象的多态性?

java - 无法使用 HTTPS 连接到套接字

testing - 需要在 .yaml 文件中带上实际日期

testing - 如何在没有 Mock 的情况下测试 Angular2/TypeScript HTTPService

azure - SSIS 完成将数据加载到 Azure SQL DWH 后运行 Azure 自动化 Runbook