AFAIK,你不能通過testng.xml文件來做到這一點。
很容易指定要執行的迭代而不必知道數據集的大小,但當您排除特定的迭代時不可能這樣做。
我能想到的最簡單的方法就是在數據提供者中插入這種過濾邏輯。這裏將告訴您如何做到這一點的例子:
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
public class SampleTestClass {
@Test(dataProvider = "dp")
public void testMethod(int value) {
System.out.println("Value is " + value);
}
@DataProvider(name = "dp")
public Object[][] getData() {
Object[][] data = new Object[][]{
{1},
{2},
{3},
{4},
{5}
};
//Calculate how many parameters we will be providing the @Test method for every iteration.
int numberOfParameters = data[0].length;
//Get the list of iterations to exclude from the user as comma separated values
//Here for the sake of example we are simulating that only the 3rd iteration needs to be excluded.
String exclude = System.getProperty("exclude", "3");
if (exclude == null || exclude.trim().isEmpty()) {
return data;
}
//Translate that comma separated values into a list of integers
List<Integer> excludeValues = Arrays.stream(exclude.split(","))
.map(Integer::parseInt).collect(Collectors.toList());
Object[][] finalData = data;
//Now nullify all the rows which we were asked to exclude so that its easy for us to filter out all the non
//null values.
excludeValues.forEach(value -> finalData[value] = null);
Object[] newData = Arrays.stream(data).filter(Objects::nonNull).collect(Collectors.toList()).toArray();
//Lets copy the contents back to the original array
data = new Object[newData.length][numberOfParameters];
for (int i=0;i<newData.length;i++) {
data[i] = (Object[]) newData[i];
}
return data;
}
}
您可以通過http://github.com/cbeust/testng打開一個問題問的功能 – juherr