且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java TestNG跨多个测试进行数据驱动测试

更新时间:2023-09-17 19:25:22

您可以将dataprovider保存在单独的类中,然后使用dataprovider注释您的测试。您可以使用 dataProviderClass 指定它

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

从testng doc引用这里

Quoting from testng doc here:


默认情况下,将查看数据提供者for in current test
class或其中一个基类。如果你想把你的数据
提供者放在另一个类中,它需要是一个静态方法,你需要
指定可以在dataProviderClass
属性中找到它的类:

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