如何在TestNG XML文件中配置为仅在包含3个测试的数据提供者集中运行一个测试

对于简短的回归,我只想从dataprovider集中运行一个测试。仅以TC1为例。仅当我需要运行完全回归时,我才想运行所有内容。有没有一种方法可以在Testng XML中进行配置?

@DataProvider(name = "testInternational")
public Object[][] testInternational() {
    return new Object[][]{
            {"TC1", "Afghanistan",Abo},
            {"TC2", "Albanië",Abo},
            {"TC3", "Zwitserland",Abo},
    };
}
class Test {

@Test(dataProvider = "testInternational")//To retry a failed test a few times
public void testInternational(String testcase, String country, String type) {
    open(url);
    //do something
}




**Testng XML**
<test name="TestDataProvider">
    <parameter name="Browser" value="firefox />
    <classes>
        <class name="Test"/>
          <methods>
            <include name="testInternational" />
        </methods>
    </classes>
</test>

最佳答案

如果您能够修改代码,则可以使用参数或环境变量:

@DataProvider(name = "testInternational")
@Parameters({ "indices" })
public Object[][] testInternational(String indices) {
    // Instead of parameters, you can use sys env
    // String[] ids = System.getProperty("indices").split(":");
    String[] ids = indices.split(":");
    Object[][] values = new Object[][]{
            {"TC1", "Afghanistan",Abo},
            {"TC2", "Albanië",Abo},
            {"TC3", "Zwitserland",Abo},
    };
    Object[][] result = new Object[ids.length][];
    for (int i=0; i<ids.length; i++) {
       result[i] = values[Integer.parseInt(ids[i]);
    }
    return result;
}




<test name="TestDataProvider">
    <parameter name="Browser" value="firefox />
    <classes>
        <class name="Test"/>
          <methods>
            <parameter name="indices" value="0:2" />
            <include name="testInternational" />
        </methods>
    </classes>
</test>


否则,您可以使用indices上的@DataProvider属性。
如果您无权访问源,则可以使用IAnnotationTransformer2修改值。

public class MyAnnotationTransformer implements IAnnotationTransformer2 {
  [...empty methods...]

  public void transform(IDataProviderAnnotation annotation, Method method) {
    if (method.getName().equals("testInternational")) {
      // Custom way to find indices (sys properties?)
      annotation.setIndices(Collections.asList(0, 2));
    }
  }
}


然后,您可以通过suite.xml@Listeners批注添加侦听器。

10-08 03:18