简体   繁体   English

TestNG中DataProvider和Factory之间有什么区别?

[英]What's the difference between DataProvider and Factory in TestNG?

何时使用DataProvider以及何时使用Factory?

TestNG factory is used to create instances of test classes dynamically. TestNG工厂用于动态创建测试类的实例。 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工厂,您可以在其中创建多个测试类实例并运行测试。

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[]. 工厂方法可以像@Test和@ Before / After一样接收参数,它必须返回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. 然而,dataprovider用于为测试提供参数。 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. 这对于您希望每次都登录到具有不同用户名和密码集的站点的场景非常有用。

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;
    }

}

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. 因此,如果您需要Person实例,您将始终从数据提供者处获得名为John Wayne的人员。 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. 它们提供动态数据,如随机内容,或者如果您想调用一些带有diffrend参数的方法。

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为测试类的单个实例执行测试方法的位置。

TLDR : TLDR

  • @DataProvider -> params for a SINGLE method @DataProvider - >用于SINGLE方法的params
  • @Factory -> params for ALL methods in a Class @Factory - >类中所有方法的参数

Let me start with using DataProviders : 让我开始使用DataProviders

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 : 使用Factory时请注意两件事:

1) You have to specify a no-arg constructor or make fields + methods static . 1)您必须指定一个无参数构造函数或使字段+方法静态 See more here 在这里查看更多

2) With @DataProvider , your @BeforeClass will be executed once . 2)使用@DataProvider ,您的@BeforeClass将被执行一次 With @Factory it will be executed with every iteration . 使用@Factory ,它将在每次迭代时执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM