When to use DataProvider and when to use Factory ?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Data provider always create the same data set. So if you need Person instance you will always get person called John Wayne from data provider. They provide static data. This is good for test parametrization when you supply your test with two objects - first is method input, second that you expect.
Factories allow you to create tests dynamically.. They provide dynamic data like random content or if you want call some method with diffrend parameters.
TestNG factory is used to create instances of test classes dynamically. This is useful if you want to run the test class any no of times. For example, if you have a test to login into a site and you want to run this test multiple times,then its easy to use TestNG factory where you create multiple instances of test class and run the tests.
and the test class is now:
Your testng.xml only needs to reference the class that contains the factory method, since the test instances themselves will be created at runtime:
The factory method can receive parameters just like @Test and @Before/After and it must return Object[]. The objects returned can be of any class (not necessarily the same class as the factory class).
Whereas, dataprovider is used to provide parameters to a test. If you provide dataprovider to a test, the test will be run taking different sets of value each time. This is useful for a scenario like where you want to login into a site with different sets of username and password each time.
TLDR:
@DataProvider
-> params for a SINGLE method@Factory
-> params for ALL methods in a ClassLet me start with using
DataProviders
:But not with the
@Factory
:DO note two things when using
Factory
:1) You have to specify a no-arg constructor or make fields + methods static. See more here
2) With
@DataProvider
, your@BeforeClass
will be executed once. With@Factory
it will be executed with every iteration.Factory implementation executes the test method for each individual instance of the test class. Where as DataProvider executes the test method for a single instance of the test class.