当前位置: 首页 > 面试题库 >

使用单个测试方法使用多个DataProvider的TestNG

郏景澄
2023-03-14
问题内容

我一直在寻找一种在测试方法中使用多个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方法中,我要针对方案的所有组合运行:

  1. 绿广场
  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 */
        .............
    }

问题答案:

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

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


 类似资料:
  • 我目前正在使用testng selenium来自动化我的测试,我有以下场景: 我需要从excel文件中读取数据,转换对象中的每一行,并为每一行运行一个测试。我试图使用annotation@DataProvider返回一个对象数组,但是它只能返回迭代器和对象[]。是否有任何变通方法可用于从数据提供程序返回客户机对象数组?我尝试了以下代码,但它只打印来自Client2的数据: 编辑1:客户类别: 在控

  • 我正在使用数据提供程序向每个测试方法传递数据。假设数据提供程序中有2行。 当前运行单个测试方法的所有迭代,然后运行第二个测试方法...例如: 但我想按以下顺序运行。 http://fruzenshtein.com/testng-dataprovider-run-tests-sequency/

  • 设置: 我有一门考试课。 这个测试类有几个带有注释的方法。 数据中有7条记录(行)。 所有测试都是顺序的 问题: 我需要用一行数据运行所有方法(测试),但到目前为止,它会在所有行上重复相同的测试,即如果有7行数据,那么测试1将运行7次,然后第二次测试将开始,第二次测试也会如此,以此类推。 我希望对第1行运行所有测试,然后对第2行再次运行所有测试。 我正在使用ApachePOIAPI从ms exce

  • 使用下面的JSON对象,如何在一个lodash的方法中为每个下拉列表传递“”属性? JSON 例如,我现在所拥有的仅包含Dropdown1的数据路径: 如何更好地编写上面的代码,以便在同一个get方法中传递for所有3个下拉列表的数据路径?

  • 我在java testng测试中使用allure testng(2.12.1)适配器。我有使用@DataProvider的测试。我的测试实现了ITest,以在运行时更改测试方法实例名称。当我运行测试时,我看到了不同的测试方法名称,但在allure报告中,它为每个测试运行显示了相同的测试方法。如何配置诱惑报告以显示类似IDE的内容? 预期:测试一测试二测试三 实际:myTest myTest myT