TestNG: More than one @DataProvider for one @Test

后端 未结 4 1383
失恋的感觉
失恋的感觉 2021-02-02 10:27

I\'m using TestNG for Eclipse.

Is it possible to give two data providers step by step to the same test-function?

I co

相关标签:
4条回答
  • 2021-02-02 10:59
        @DataProvider
        public Object[][] combinedDataProvider() {
            return Stream.of(dp1(), dp2())
                    .flatMap(Arrays::stream)
                    .toArray(Object[][]::new);
        }
    
    0 讨论(0)
  • 2021-02-02 11:09

    Please refer to this answer:

    TestNG using multiple DataProviders with single Test method

    It is much cleaner and will work for more complex things.

    Maybe someone will need it too, I rewrote this method public static T[] concatAll(T[] first, T[]... rest) in a different way:

    public static Object[] concat(Object[] first, Object[] second) {
        Object[] result = ArrayUtils.addAll(first, second);
        return result;
    }
    
    0 讨论(0)
  • 2021-02-02 11:12

    No, but nothing stops you from merging these two data providers into one and specifying that one as your data provider:

    public Object[][] dp1() {
      return new Object[][] {
          new Object[] { "a", "b" },
          new Object[] { "c", "d" },
      };
    }
    
    public Object[][] dp2() {
      return new Object[][] {
          new Object[] { "e", "f" },
          new Object[] { "g", "h" },
      };
    }
    
    @DataProvider
    public Object[][] dp() {
      List<Object[]> result = Lists.newArrayList();
      result.addAll(Arrays.asList(dp1()));
      result.addAll(Arrays.asList(dp2()));
      return result.toArray(new Object[result.size()][]);
    }
    
    @Test(dataProvider = "dp")
    public void f(String a, String b) {
      System.out.println("f " + a + " " + b);
    }
    
    0 讨论(0)
  • 2021-02-02 11:13

    Yes,

    You can write @Test(dataProvider="name_of_first_dataprovider,name_of_second_dataprovider")

    0 讨论(0)
提交回复
热议问题