How to exit out of data driven testing

33 views
Skip to first unread message

MCD dotCom

unread,
Feb 25, 2024, 6:13:30 PMFeb 25
to testng-users

Hello,

I have a total of 10 rows of test data in an .xlsx file containing both valid and invalid reference numbers.

I would like to configure my test to execute only the first 5 valid reference numbers.

How can I set this limit? The exit below is not working for me.



public class FirstTest extends BaseTest {
int count = 0;

@DataProvider(name = "Data")
public Object[][] getWorkflowData(ITestContext context) {
// fetch test data
return testData;
}

@Test(dataProvider = "Data")
public void test(Map<String, String> rowData) {

if (validRefNo()) {

// perform actions

        count++;        

               if (count >= 5) {
          System.out.println("COUNT: " + count);
                  throw new SkipException("Stopping iteration after 5 iterations"); // second way to exit but not working

                          //return; // First way to exit but not working
                }
}
}

}

MCD dotCom

unread,
Feb 25, 2024, 6:19:10 PMFeb 25
to testng-users
I forgot to mention that the test iterates through all the rows regardless of the number of valid reference numbers. But, I only want to process up to 5 valid reference numbers.

Krishnan Mahadevan

unread,
Feb 25, 2024, 8:52:43 PMFeb 25
to testng...@googlegroups.com
Have you tried altering your data provider to ensure that it creates the 2D array only with those records that you want to process? 

What happens when you add conditional logic to your data provider such that if it stumbles into an invalid reference it skips from adding it to the 2D array? 

To me this sounds more like a programming problem than a TestNG problem. 

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

From: testng...@googlegroups.com <testng...@googlegroups.com> on behalf of MCD dotCom <mycheap...@gmail.com>
Sent: Monday, February 26, 2024 4:49:10 AM
To: testng-users <testng...@googlegroups.com>
Subject: [testng-users] Re: How to exit out of data driven testing
 
--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/cfedc327-0178-41ef-811c-d5a90d162400n%40googlegroups.com.

MCD dotCom

unread,
Feb 25, 2024, 10:11:39 PMFeb 25
to testng-users
I can add some logic in my readData method, which fetches the data from the .xlsx file, by adding a counter. I'm just checking to see if there is a way to exit out of the loop in the test method, given the example above.

Grigoriy Frank

unread,
Feb 25, 2024, 10:52:48 PMFeb 25
to testng...@googlegroups.com
Maybe you can try @DataProvider(indices = {0,1,2,3,4}), but you will need to have several data providers for all needed ranges.

пн, 26 февр. 2024 г. в 11:11, MCD dotCom <mycheap...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages