Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate data provider from test case class

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.

like image 848
David762 Avatar asked Jun 04 '17 09:06

David762


People also ask

How will you specify a data provider under test annotation?

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.

How do I use a data provider from another class?

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.

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.

What is DataProvider annotation in TestNG?

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.


1 Answers

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.

like image 194
Sebastian Bergmann Avatar answered Sep 22 '22 15:09

Sebastian Bergmann