TestNG中DataProvider和Factory有什么区别?

发布于 2024-10-24 15:22:44 字数 41 浏览 1 评论 0原文

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

When to use DataProvider and when to use Factory ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

緦唸λ蓇 2024-10-31 15:22:44

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

测试类现在是:

public class WebTest {     
  private int m_numberOfTimes;     
  public WebTest(int numberOfTimes) {      
    m_numberOfTimes = numberOfTimes;       
  }    

  @Test    
  public void testServer() {       
   //Code to test the application   
  }    
}    

您的 testng.xml 只需引用包含工厂方法的类,因为测试实例本身将在运行时创建:

<class name="WebTestFactory" />  

工厂方法可以像 @Test 和 @Before/After 一样接收参数它必须返回 Object[]。返回的对象可以是任何类(不一定是与工厂类相同的类)。

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

}

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.

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:

<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).

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.

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

}
红颜悴 2024-10-31 15:22:44

数据提供者总是创建相同的数据集。因此,如果您需要 Person 实例,您总是会从数据提供者处获得名为 John Wayne 的人。他们提供静态数据。当您为测试提供两个对象时,这对于测试参数化很有用 - 第一个是方法输入,第二个是您期望的。

工厂允许您动态创建测试。。它们提供动态数据,例如随机内容,或者如果您想使用 diffrend 参数调用某些方法。

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.

太傻旳人生 2024-10-31 15:22:44

工厂实现为测试类的每个单独实例执行测试方法。 DataProvider 为测试类的单个实例执行测试方法。

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.

装迷糊 2024-10-31 15:22:44

TLDR

  • @DataProvider -> SINGLE 方法的参数
  • @Factory ->类中所有方法的参数

让我从使用 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...
}

但不使用 @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
       // ...
    }
}

使用 Factory 时请注意两件事:

1) 您必须指定一个无参数构造函数或使字段+方法静态。查看更多此处

2) 使用@DataProvider,您的@BeforeClass将被执行一次。使用@Factory,它将在每次迭代中执行。

TLDR:

  • @DataProvider -> params for a SINGLE method
  • @Factory -> params for ALL methods in a Class

Let me start with using 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:

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

2) With @DataProvider, your @BeforeClass will be executed once. With @Factory it will be executed with every iteration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文