When to use DataProvider and when to use Factory ?
何时使用DataProvider和何时使用工厂?
4 个解决方案
#1
10
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.
TestNG工厂用于动态创建测试类的实例。如果您想在任何时候运行测试类,这是很有用的。例如,如果您有一个登录到一个站点的测试,并且您想要多次运行这个测试,那么它很容易使用TestNG工厂,在那里您可以创建多个测试类的实例并运行测试。
public class WebTestFactory {
//createInstances method will create 10 objects of WebTest class
@Factory
public Object[] createInstances() {
Object[] result = new Object[10];
for (int i = 0; i < 10; i++) {
result[i] = new WebTest(i);
}
return result;
}
and the test class is now:
测试类现在是:
public class WebTest {
private int m_numberOfTimes;
public WebTest(int numberOfTimes) {
m_numberOfTimes = numberOfTimes;
}
@Test
public void testServer() {
//Code to test the application
}
}
Your testng.xml only needs to reference the class that contains the factory method, since the test instances themselves will be created at runtime:
你的testng。xml只需要引用包含工厂方法的类,因为测试实例本身将在运行时创建:
<class name="WebTestFactory" />
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).
工厂方法可以接收参数,如@Test和@Before/After,它必须返回Object[]。返回的对象可以是任何类(不一定是与工厂类相同的类)。
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.
而dataprovider用于为测试提供参数。如果您为测试提供了dataprovider,那么每次测试都将使用不同的值集。这对于您希望每次登录到具有不同用户名和密码的站点的场景非常有用。
public class DataProviderTest {
@Test(dataProvider= "data")
public void TestUltimatixFromExcelData(String userName,String password) {
WebDriver driver;
driver=new FirefoxDriver();
//Test to login into a site
}
@DataProvider(name="data")
public static Object[][] dataProviderTest() throws Exception{
Object[][] returnArray={new Object[]{"username1","password1"},new Object[]{"username2","password2"},new Object[]{"username3","password3"}
};
return returnArray;
}
}
#2
2
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.
数据提供程序总是创建相同的数据集,因此,如果您需要Person实例,您将总是从数据提供者那里得到一个叫John Wayne的人。他们提供了静态数据。当您用两个对象提供测试时,这对测试参数化有好处——首先是方法输入,其次是您所期望的。
Factories allow you to create tests dynamically.. They provide dynamic data like random content or if you want call some method with diffrend parameters.
工厂允许您动态地创建测试。它们提供动态的数据,比如随机的内容,或者如果你想用不同的参数调用某个方法。
#3
1
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.
工厂实现为测试类的每个实例执行测试方法。当DataProvider为测试类的单个实例执行测试方法时。
#4
0
TLDR:
TLDR:
-
@DataProvider
-> params for a SINGLE method - @DataProvider ->对单个方法的params。
-
@Factory
-> params for ALL methods in a Class - @Factory ->对类中的所有方法进行解析。
Let me start with using DataProviders
:
让我从使用dataprovider开始:
public class VeryImportantTest {
@DataProvider
public static Object[][] numberProvider() {
return new Object[][]{
{1},
{2}
};
}
// DataProvider provides data to a SINGLE method
@Test(dataProvider = "numberProvider")
public void test1(int num){
Assert.assertNotEquals(3, num);
}
@Test(dataProvider = "numberProvider")
public void test2(int num){
// ...
}
@Test(dataProvider = "numberProvider")
public void test3(int num){
// ...
}
// Hmmm... I still have 10 tests to write here,
// and it's getting annoying to specify the dataprovider everytime...
}
But not with the @Factory
:
但不是@Factory:
public class FactoryExample {
@Factory
public Object[] factoryMethod() {
return new Object[] {
new FactoryExample(0),
new FactoryExample(1)
};
}
private int number;
private FactoryExample(){}
private FactoryExample(int number) {
this.number = number;
}
// Now there's no need to specify dataproviders everywhere, nice
@Test
public void test1(){
Assert.assertNotEquals(3, number);
}
@Test
public void test2(){ // <-- No need to specify params in each method either
// ...
}
}
DO note two things when using Factory
:
使用工厂时要注意两点:
1) You have to specify a no-arg constructor or make fields + methods static. See more here
1)您必须指定一个无arg构造函数或使字段+方法为静态。看到更多的
2) With @DataProvider
, your @BeforeClass
will be executed once. With @Factory
it will be executed with every iteration.
2)@DataProvider,你的@BeforeClass将被执行一次。通过@Factory,它将在每次迭代中执行。
#1
10
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.
TestNG工厂用于动态创建测试类的实例。如果您想在任何时候运行测试类,这是很有用的。例如,如果您有一个登录到一个站点的测试,并且您想要多次运行这个测试,那么它很容易使用TestNG工厂,在那里您可以创建多个测试类的实例并运行测试。
public class WebTestFactory {
//createInstances method will create 10 objects of WebTest class
@Factory
public Object[] createInstances() {
Object[] result = new Object[10];
for (int i = 0; i < 10; i++) {
result[i] = new WebTest(i);
}
return result;
}
and the test class is now:
测试类现在是:
public class WebTest {
private int m_numberOfTimes;
public WebTest(int numberOfTimes) {
m_numberOfTimes = numberOfTimes;
}
@Test
public void testServer() {
//Code to test the application
}
}
Your testng.xml only needs to reference the class that contains the factory method, since the test instances themselves will be created at runtime:
你的testng。xml只需要引用包含工厂方法的类,因为测试实例本身将在运行时创建:
<class name="WebTestFactory" />
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).
工厂方法可以接收参数,如@Test和@Before/After,它必须返回Object[]。返回的对象可以是任何类(不一定是与工厂类相同的类)。
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.
而dataprovider用于为测试提供参数。如果您为测试提供了dataprovider,那么每次测试都将使用不同的值集。这对于您希望每次登录到具有不同用户名和密码的站点的场景非常有用。
public class DataProviderTest {
@Test(dataProvider= "data")
public void TestUltimatixFromExcelData(String userName,String password) {
WebDriver driver;
driver=new FirefoxDriver();
//Test to login into a site
}
@DataProvider(name="data")
public static Object[][] dataProviderTest() throws Exception{
Object[][] returnArray={new Object[]{"username1","password1"},new Object[]{"username2","password2"},new Object[]{"username3","password3"}
};
return returnArray;
}
}
#2
2
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.
数据提供程序总是创建相同的数据集,因此,如果您需要Person实例,您将总是从数据提供者那里得到一个叫John Wayne的人。他们提供了静态数据。当您用两个对象提供测试时,这对测试参数化有好处——首先是方法输入,其次是您所期望的。
Factories allow you to create tests dynamically.. They provide dynamic data like random content or if you want call some method with diffrend parameters.
工厂允许您动态地创建测试。它们提供动态的数据,比如随机的内容,或者如果你想用不同的参数调用某个方法。
#3
1
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.
工厂实现为测试类的每个实例执行测试方法。当DataProvider为测试类的单个实例执行测试方法时。
#4
0
TLDR:
TLDR:
-
@DataProvider
-> params for a SINGLE method - @DataProvider ->对单个方法的params。
-
@Factory
-> params for ALL methods in a Class - @Factory ->对类中的所有方法进行解析。
Let me start with using DataProviders
:
让我从使用dataprovider开始:
public class VeryImportantTest {
@DataProvider
public static Object[][] numberProvider() {
return new Object[][]{
{1},
{2}
};
}
// DataProvider provides data to a SINGLE method
@Test(dataProvider = "numberProvider")
public void test1(int num){
Assert.assertNotEquals(3, num);
}
@Test(dataProvider = "numberProvider")
public void test2(int num){
// ...
}
@Test(dataProvider = "numberProvider")
public void test3(int num){
// ...
}
// Hmmm... I still have 10 tests to write here,
// and it's getting annoying to specify the dataprovider everytime...
}
But not with the @Factory
:
但不是@Factory:
public class FactoryExample {
@Factory
public Object[] factoryMethod() {
return new Object[] {
new FactoryExample(0),
new FactoryExample(1)
};
}
private int number;
private FactoryExample(){}
private FactoryExample(int number) {
this.number = number;
}
// Now there's no need to specify dataproviders everywhere, nice
@Test
public void test1(){
Assert.assertNotEquals(3, number);
}
@Test
public void test2(){ // <-- No need to specify params in each method either
// ...
}
}
DO note two things when using Factory
:
使用工厂时要注意两点:
1) You have to specify a no-arg constructor or make fields + methods static. See more here
1)您必须指定一个无arg构造函数或使字段+方法为静态。看到更多的
2) With @DataProvider
, your @BeforeClass
will be executed once. With @Factory
it will be executed with every iteration.
2)@DataProvider,你的@BeforeClass将被执行一次。通过@Factory,它将在每次迭代中执行。