Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

I then pass the 214 String parameters into an @Test annotation in a separate class called EnterData.

@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. 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.

// 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?...

Edit:

Here is my current code for reading the datapool in the ReadAutoData Class:

@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:

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:

// 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.

like image 998
Dustin N. Avatar asked May 13 '26 19:05

Dustin N.


1 Answers

The only way you can achieve that is to understand how the TestNG framework executes.

Which means:

  1. SkipException is not the solution as it will always count all the test cases, even if they are skipped.
  2. The number of tests is the number of rows returned by the @DataProvider method.

The solution is then to return the correct number of test cases from the method annotated by @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 ThreadLocals.

like image 197
Crazyjavahacking Avatar answered May 16 '26 10:05

Crazyjavahacking



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!