4

How to use @DataProvider that is present in a different class?

I have created a different package and I have defined data providers next to each test cases. Please share how I may to use that in a different class.

Nathan
  • 8,093
  • 8
  • 50
  • 76
Gaurav G Raj
  • 43
  • 1
  • 1
  • 6

2 Answers2

19

You can use the dataProviderClass attribute of @Test:

public class StaticProvider {
  @DataProvider(name = "create")
  public static Object[][] createData() {
    return new Object[][] {
      new Object[] { new Integer(42) }
    };
  }
}

public class MyTest {
  @Test(dataProvider = "create", dataProviderClass = StaticProvider.class)
  public void test(Integer n) {
    // ...
  }
}

Check the documentation for more details.

juherr
  • 5,640
  • 1
  • 21
  • 63
0

If you have unique dataProvider method name (createData), and if you choose not to give name after DataProvider annotation as below,

@DataProvider    
public Object[][] createData(){

}

Then you can use the method name as below,

@Test(dataProvider = "createData", dataProviderClass = StaticProvider.class)
John Difool
  • 5,572
  • 5
  • 45
  • 80
Shiv
  • 9
  • 3