13

Does anyone know if there is a way to use a TestNG DataProvider with a test at the same time as using the @Parameter annotation? Our test suites have some constant configuration information that is being passed to the test methods via the @Parameter annotation. We would now like to use a DataProvider to run these tests over a set of data values.

I understand the internal problem of determining the order the resulting parameters would be in but we need to this feature if possible.

Any thoughts?

In an ideal world, I could do something like this:

@Test(dataprovider = "dataLoader")
@Parameters("suiteParam")
public void testMethod(String suiteParam, String fromDataParam) {
...
}
Benjamin Lee
  • 1,160
  • 3
  • 13
  • 18

2 Answers2

10

Hey, it may be a bit clunky but why don't you use a @BeforeClass method to store the suiteParam locally on a field of the test class like so.

private String suiteParam;

@BeforeClass
@Parameter("suiteParam")
public void init(String suiteParam) {
  this.suiteParam = suiteParam;
}

This way you can use your data providers in the usual way and still have access to your suite param.

mR_fr0g
  • 8,462
  • 7
  • 39
  • 54
2

Yes, using the using TestNG's dependency injection capabilies. You can access all defined parameters in your DataProvider. This is some example DataProvider in need of the test_param parameter:

@DataProvider(name = "usesParameter")
public Object[][] provideTestParam(ITestContext context) {
    String testParam = context.getCurrentXmlTest().getParameter("test_param");
    return new Object[][] {{ testParam }};
}

This way you can collect configured and generated parameters in a DataProvider which is then used for your test. See the TestNG JavaDoc for details on the ITestContext class.

desolat
  • 4,123
  • 6
  • 36
  • 47