使用TestNG DataProvider跳过某些测试迭代

时间:2015-07-08 20:25:54

标签: java selenium-webdriver testng testng-dataprovider

我使用TestNG DataProvider读取一个datapool.xls文件,该文件包含1017个测试用例和一个名为ReadData的类中的214列。

然后我将214 String参数传递给一个名为EnterData的单独类中的@Test注释。

@Test(dataProvider="autodata", dataProviderClass = ReadAutoData.class)
public void autoentry(String Quote, String Garage_Zip, etc...) {

我在@Test中创建了一个for循环,只执行ceratin迭代的操作(比如1-10),它只能输入10个测试用例。我的问题是,在运行结束时它仍然显示"总测试运行:1017"而不是因为for循环而实际做了任何事情的10个。

// Start for loop for entering auto policies
for (int a = start; a <= end; a++)
{
    if (quote == a)
    {       
        System.out.println("Tier = " + Tier);                       
    } 
}

我意识到为什么它显示了作为整个数据池运行的测试总数,因为它在技术上传入并运行了数据池中的所有内容,我只是无法弄清楚如何只显示它的数量实际运行的测试。

基本上,我正在寻找一种方法将@Test注释本身放在for循环中,可能吗?...

编辑:

以下是我在ReadAutoData类中读取数据池的当前代码:

@DataProvider(name = "autodata")
public static Object[][] autodata() {
Object[][] arrayObject = getExcelData("C:/dev/AutoDP.xls","NON-COMBO-DP");
return arrayObject;
}

/**
 * @param File Name
 * @param Sheet Name
 * @return
 */
public static String[][] getExcelData(String fileName, String sheetName) {
    String[][] arrayExcelData = null;
    try {
        FileInputStream fs = new FileInputStream(fileName);
        Workbook wb = Workbook.getWorkbook(fs);
        Sheet sh = wb.getSheet(sheetName);

        int totalNoOfCols = sh.getColumns();
        int totalNoOfRows = sh.getRows();

        arrayExcelData = new String[totalNoOfRows-1][totalNoOfCols];

        for (int i= 1 ; i < totalNoOfRows; i++) {

            for (int j=0; j < totalNoOfCols; j++) {
                arrayExcelData[i-1][j] = sh.getCell(j, i).getContents();
            }

        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        e.printStackTrace();
    } catch (BiffException e) {
        e.printStackTrace();
    }
    return arrayExcelData;
}

编辑2:

到目前为止,在1017个测试用例中,我已经通过在for循环的开头添加以下内容来显示1007 Skips:

// Start for loop for entering auto policies
    for (int a = start; a <= end; a++)
    {
        if (quote < start) throw new SkipException("Testing skip.");
        if (quote > end) throw new SkipException("Testing skip.");

        if (quote == a)
        {

            System.out.println("Starting Iteration = " + Quote);

然而,它仍在显示1017次测试。

3 个答案:

答案 0 :(得分:4)

实现这一目标的唯一方法是了解TestNG框架的执行方式。

这意味着:

  1. SkipException不是解决方案,因为它总是会计算所有测试用例,即使它们被跳过。
  2. 测试次数是@DataProvider方法返回的行数。
  3. 然后,解决方案将从@DataProvider 注释的方法返回正确数量的测试用例:

    public class ReadAutoData {
        private static int indexFrom;
        private static int indexTo;
    
        @DataProvider(name = "autodata")
        public static Object[][] autodata() {
            // you should probably cache this into static variable
            Object[][] arrayObject = getExcelData("C:/dev/AutoDP.xls","NON-COMBO-DP"); 
    
            return java.util.Arrays.copyOfRange(arrayObject, indexFrom, indexTo);
        }
    
        public Class<?> autoDataWithinRange(int from, int to) {
            indexFrom = from;
            indexTo   = to;
    
            return ReadAutoData.class;
        }
    }
    
    @Test(dataProvider="autodata", dataProviderClass = ReadAutoData.autoDataWithinRange(0, 10))
    public void autoentry(String Quote, String Garage_Zip, etc...) {
    

    如果您没有同时运行测试,这将有效。如果是,您仍然可以使用ThreadLocal s进行小修改。

答案 1 :(得分:1)

你总是可以抛出TestNG的@QueryResult class PersonWithHash { Person person; int reporteeCount; } // in PersonRepository @Query("MATCH (starter:Person {userId: {0}})<-[r:WORKS_FOR]-(n:Person) OPTIONAL MATCH (n)<-[q:WORKS_FOR]-(p:Person) RETURN n as person, count(q) as reporteeCount") Collection<PersonWithHash> findBy...(String userId); 来将跳过的执行标记为跳过而不是传递。 SkipException是一个特殊的例外,不会被视为失败。

除此之外,您可以修改SkipException以返回更少的数据集(行),例如只有您文件中数千的前10名。

答案 2 :(得分:0)

如何通过实现function getCurrentTemp($obj){ return (($obj->query->results->channel->item->forecast[0]->high)+($obj->query->results->channel->item->forecast[0]->low))/2; } function getCurrentCondition($obj){ return $obj->query->results->channel->item->forecast[0]->text; } function fahrenheit_to_celsius($given_value) { $celsius=5/9*($given_value-32); return $celsius ; } echo fahrenheit_to_celsius(getCurrentTemp($phpObj))." ".getCurrentCondition($phpObj); 并实现方法IInvokedMethodListener来创建侦听器类。在这方面,您可以获取测试结果对象并获取参数并决定如何为该数据集执行操作。

beforeInvocation(IInvokedMethod method, ITestResult tr)

我假设您想要根据参数运行或不运行。

相关问题