Java 具有跨多个测试的数据驱动测试的 TestNG

Java TestNG with Data Driven Testing Across Multiple Tests

我在电子商务平台上测试了一系列商店,每个商店都有一系列我正在寻找自动化测试的属性。是否有可能有一个数据提供者在整个测试套件中提供数据,而不仅仅是 TestNG 中的测试?我试图不使用 testNG.xml 文件作为机制,因为这些属性直接来自数据库调用。

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

我需要自动化执行以下操作:

  1. @Test 使用当前数据集行中的用户名和密码登录。
  2. @Test 验证 StoreName 和 items-store
  3. @Test 导航到管理并验证商店的信用启用设置和商店的送货位置是否正确给定商品商店价值。

但是这里的每一步都必须是单独的测试。

您可以将数据提供程序保存在单独的 class 中,然后使用数据提供程序注释您的测试。您可以使用 dataProviderClass

指定它

引用自 testng 文档 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) {
    // ...
  }
}