Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TestNG: More than one @DataProvider for one @Test

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); } 
like image 986
Malte Onken Avatar asked May 31 '12 11:05

Malte Onken


People also ask

Can we use multiple DataProvider in TestNG?

However, TestNG parameters enable us to pass the values only once per execution cycle. To overcome this, we can use DataProvider in TestNG that allows us to pass multiple parameters to a single test in a single execution. Using DataProviders, we can easily pass multiple values to a test in just one execution cycle.

When we will use @DataProvider in TestNG?

DataProvider in TestNG The DataProvider method can be in the same test class or one of its superclasses. It is also possible to provide a DataProvider in another class but the method has to be static. After adding this method, annotate it using @DataProvider to let TestNG know that it is a DataProvider method.

What is the difference between DataProvider and parameter in TestNG?

What is the difference between DataProvider and Parameter in TestNG? DataProviders pass the different parameters on a single test in a single execution, whereas parameters pass the parameters just once per execution in TestNG.


2 Answers

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); } 
like image 154
Cedric Beust Avatar answered Oct 09 '22 21:10

Cedric Beust


@DataProvider public Object[][] combinedDataProvider() {     // Using stream to combine the two separate data providers.     return Stream.of(dp1(), dp2())                  .flatMap(Arrays::stream)                  .toArray(Object[][]::new); } 
like image 34
Wee Avatar answered Oct 09 '22 20:10

Wee