Search code examples
testngtestng-dataprovider

Testng - Is it okay to have one data provider input per test method?


I understand that testng DataProviders are meant to provide multiple inputs/input arrays to test methods. But, is it ever okay to have only one input per test method ? Is that an anti-pattern ?


Solution

  • TestNG does not have any recommendation in terms of how many data inputs are at the minimum needed if one were to use a @DataProvider.

    But here's the general convention that users follow.

    When its known in advance that a @Test method is going to be run with just one set of parameter values then you should try and use @Parameters annotation which receives inputs from the suite xml file via the <parameters> tag.

    If its known in advance that a @Test method would need to run with more than one set of parameter values, then you should use a @DataProvider.

    If its not known in advance as to whether there would be just one parameter value or more than one, then you can just go with @DataProvider.

    The advantage that @DataProvider has over @Parameters is that, @DataProvider driven @Test methods dont have to be mandatorily driven via a suite xml. You can run them as individual tests as well.

    But when a @Test method receives values via a @Parameters annotation, then it would need to be run via a suite xml only (ofc-course you can circumvent that by injecting parameters using a TestNG listener, but that's more of a workaround than a solution)