简体   繁体   English

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

[英]Skip Certain Test Iterations Using TestNG DataProvider

I'm using the TestNG DataProvider to read a datapool.xls file that contains 1017 test cases and 214 columns in a class called ReadData. 我正在使用TestNG DataProvider读取一个datapool.xls文件,该文件包含1017个测试用例和一个名为ReadData的类中的214列。

I then pass the 214 String parameters into an @Test annotation in a separate class called EnterData. 然后,我将214 String参数传递给一个名为EnterData的单独类中的@Test注释。

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

I have created a for loop within @Test to only perform the actions for ceratin iterations (say 1-10), which does work by only entering 10 test cases in total. 我在@Test中创建了一个for循环,只执行ceratin迭代的操作(比如1-10),它只能输入10个测试用例。 My problem is that at the end of the run it still shows "Total tests run: 1017" instead of only the 10 that actually did anything because of the for loop. 我的问题是,在运行结束时,它仍然显示“Total tests run: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);                       
    } 
}

I realize why it shows the total number of tests run as being the entire datapool because it technically passed in and ran through everything in the datapool, I just can't figure out how to only have it show the number of tests that actually ran. 我意识到为什么它显示了作为整个数据池运行的测试总数,因为它在技术上传入并运行了数据池中的所有内容,我只是无法弄清楚如何只显示实际运行的测试数量。

Basically, I'm looking for a way to put the @Test annotation itself in the for loop, possibly?... 基本上,我正在寻找一种方法将@Test注释本身置于for循环中,可能吗?...

Edit: 编辑:

Here is my current code for reading the datapool in the ReadAutoData Class: 以下是我在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;
}

Edit 2: 编辑2:

Here's where I'm at so far out of the 1017 test cases, I have gotten it to now show 1007 Skips by adding the following at the beginning of my for loop: 这是我在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);

however, it is still showing 1017 Tests run. 但是,它仍然显示1017测试运行。

The only way you can achieve that is to understand how the TestNG framework executes. 实现这一目标的唯一方法是了解TestNG框架的执行方式。

Which means: 意思是:

  1. SkipException is not the solution as it will always count all the test cases, even if they are skipped. SkipException不是解决方案,因为它总是会计算所有测试用例,即使它们被跳过。
  2. The number of tests is the number of rows returned by the @DataProvider method. 测试次数是@DataProvider方法返回的行数。

The solution is then to return the correct number of test cases from the method annotated by @DataProvider : 然后,解决方案是从@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...) {

This would work if you are not running test concurrently. 如果您没有同时运行测试,这将有效。 If you are, you can still use it with small modification using ThreadLocal s. 如果你是,你仍然可以使用ThreadLocal进行小修改。

You can always throw TestNG's SkipException to mark skipped executions as skipped rather than passed. 你总是可以抛出TestNG的SkipException来将跳过的执行标记为跳过而不是传递。 SkipException is a special exception that is not recognized as a failure. SkipException是一个特殊的异常,不会被识别为失败。

Other that this, you can modify ReadAutoData.autodata to return fewer data sets (rows), eg only first 10 from your thousands you have in the file. 除此之外,您可以修改ReadAutoData.autodata以返回更少的数据集(行),例如,只有您文件中的数千个中的前10个。

How about creating listener class by implementing IInvokedMethodListener and implementing method beforeInvocation(IInvokedMethod method, ITestResult tr) In side that you can take the test result object and get the params and decide what to do for that data set. 如何通过实现IInvokedMethodListener和实现方法beforeInvocation(IInvokedMethod method, ITestResult tr)创建监听器类beforeInvocation(IInvokedMethod method, ITestResult tr)在这方面,您可以获取测试结果对象并获取参数并决定如何为该数据集执行操作。

Object [] params = tr.getParameters();
// put some logic on params and decide
if ( params is something ) {
tr.setStatus(ITestResult.SKIP);
}

I am assuming that you want to run or not run based on the params. 我假设您想要运行或不运行基于参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM