I want to use PHPUnit to test my PHP class.
Is it possible to put data providers for my test methods in a separate file created only for storing dataproviders? If so how to do that?
Another question is whether it's a good practice or perhaps it's better to keep test and data provider methods in the same test class.
DataProvider in TestNG 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. Additionally, provide a name using the name attribute of the DataProvider annotation.
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.
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.
The Complete TestNG & Automation Framework Design Course @DataProvider annotation helps us write data-driven test cases. The @DataProvider annotation enables us to run a test method multiple times by passing different data-sets.
Simply use @dataProvider class::method
to use a method from a different class than the test case class as a data provider for a test.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With