2017-07-26 27 views
0
<test name="abc"> 
    <classes> 
    <class name="myClass"> 
     **<exclude name="testMethod" invocation-numbers="4"/>** 
    </class> 
    </classes> 
</test> 

上面的testng XML簡單地排除了「testMethod」作爲一個整體。但是,如果將「exclude」替換爲「include」,則它只能正確運行數據提供程序的第5次迭代作爲測試數據。當包含這種情況時,當我將exclude排除時,testng應該運行「testMethod」的所有迭代,不包括數據提供者的第5次迭代。但它只是將整個方法排除在外。這是爲什麼?應該怎樣才能達到我的期望?如何使用testng xml中的特定調用號排除方法?

+0

您可以通過http://github.com/cbeust/testng打開一個問題問的功能 – juherr

回答

1

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; 
    } 
} 
相關問題