使用TestNG的单试验方法多的dataProvider [英] TestNG using multiple DataProviders with single Test method

查看:191
本文介绍了使用TestNG的单试验方法多的dataProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种方式,在我的测试方法,使用多个的dataProvider。我的情况如下:

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:

@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. 绿色广场

  2. 红坊

  3. 绿三角

  4. 红三角

我应该如何在我的code,因为我不能用指定多个的dataProvider实现这个 @Test 注释

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

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

编辑:我发现了一个类似的问题和@ <一个href=\"http://stackoverflow.com/questions/4525989/several-dataprovider-per-one-test-in-testng?rq=1\">workaround但我如果有更好的方法来处理这​​个问题我仍然不知道。

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

推荐答案

有关缺乏一个更好的方法,我决定坚持的解决方法。这里是上述情况下如何实现的例子:

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){
                cardCodesList.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;
    }
}

这样我可以用我的色彩codeS和形状codeS分开,并提供我使用的组合。

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

这篇关于使用TestNG的单试验方法多的dataProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆