28

I'm using TestNG for Eclipse.

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

I could put both providers in one, but that is not what I want.

I need (not like in this example) to generate independently data.

@DataProvider(name = "dataSet1")
public Object[][] createDataX() {
    return new Object[][] { { 1, 1 }, { 2, 2 } };
}

@DataProvider(name = "dataSet2")
public Object[][] createDataY() {
    return new Object[][] { { 0, 0 }, { 3, 3 } };
}

I want to give both providers to the same test. Is this possible?

@Test(dataProvider = "dataSet1") // ??? and "dataSet2" ???
public void testThisFunction(int val1, int val2) {
    boolean solution = oracle(val1,val2);
    assert (solution);
}
Gautham M
  • 4,816
  • 3
  • 15
  • 37
Malte Onken
  • 769
  • 1
  • 9
  • 20

5 Answers5

36

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);
}
Cedric Beust
  • 15,480
  • 2
  • 55
  • 55
3
@DataProvider
public Object[][] combinedDataProvider() {
    // Using stream to combine the two separate data providers.
    return Stream.of(dp1(), dp2())
                 .flatMap(Arrays::stream)
                 .toArray(Object[][]::new);
}
Gautham M
  • 4,816
  • 3
  • 15
  • 37
Wee
  • 131
  • 4
1

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;
}
Community
  • 1
  • 1
Sashko
  • 191
  • 3
  • 10
0

This is the working fix:

@DataProvider(name = "requestValidator")
public Object[][] getRequestValidationMap() {
    ResponseEntity clientDetailsResponse = ResponseUtil.successResponse(HttpStatus.OK,
            clientDetailsEntityDao);
    ResponseEntity failureResponse = ResponseUtil.handleErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR,
            ERRCONSTANT.CUSTOM_CODE.getErrorDescription(), ERRCONSTANT.CUSTOM_CODE.getCategory(),
            ERRCONSTANT.CUSTOM_CODE.getValue());
    return new Object[][] { { "client", clientDetailsResponse, 200, HttpServletResponse.SC_OK },
            { "error", failureResponse, 500, HttpServletResponse.SC_INTERNAL_SERVER_ERROR } };
}

In the utility method

@Test(dataProvider = "requestValidator")
public void testMultipleArgs(String key, ResponseEntity requestValidatorResponse, int statusCodeExpected, int actualStatusCode) throws Exception {
        
}
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
-10

Yes,

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

  • If used like this, which data provider will be used for the test? – hotzst Jan 15 '16 at 07:35
  • 2
    This would be nice, but does not work for me. Which version of TestNG supports this dataProvider syntax? – Gary Sep 03 '16 at 00:03
  • 2
    This does not have any base, it does not work and it is not pointing to any documentation specifying such functionality – Ordiel Oct 03 '16 at 23:58