![](/img/trans.png)
[英]TestNg : How to run a method only once for multiple objects in DataProvider
[英]How to configure in int TestNG XML file to run only one test in the dataprovider set containing 3 tests
如何在TestNG XML文件中配置仅在包含3个测试的dataprovider集中运行一个测试
对于简短的回归,我想从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>
否则,您可以在@DataProvider
上使用indices
属性。 如果您无权访问源,则可以使用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
批注添加监听器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.