-->

Java TestNG with Data Driven Testing Across Multip

2019-03-06 16:42发布

问题:

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) {
    // ...
  }
}