Java TestNG with Data Driven Testing Across Multiple Tests

冷暖自知 提交于 2019-12-04 06:32:54

问题


I have a series of stores that I am testing in an ecommerce platform, and each store has a series of properties that I am looking at automating a test for. Is it possible to have a data provider that gives data across a test suite instead of just a test in TestNG? I'm trying not to use the testNG.xml file as mechanism because these properties are coming directly from a database call.

["StoreName", "username", "password", "credit-enabled", "items-store", "shipping-location", ]

What I need the automation to do is the following:

  1. @Test Login with the username and password in the current dataset row.
  2. @Test Verify the StoreName and items-store
  3. @Test Navigate to the administration and verify that credit-enabled setting for the store and the shipping location of the store is correct given the items-store value.

But each step here would have to be its separate test.


回答1:


You can keep dataprovider in a separate class and then annotate your tests with the dataprovider. You can specify it using dataProviderClass

Quoting from testng doc here:

By default, the data provider will be looked for in the current test class or one of its base classes. If you want to put your data provider in a different class, it needs to be a static method and you specify the class where it can be found in the dataProviderClass attribute:

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.class)
  public void test(Integer n) {
    // ...
  }
}


来源:https://stackoverflow.com/questions/33091726/java-testng-with-data-driven-testing-across-multiple-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!