Test NG Parameterized Test

Share

Test NG Parameterized Test

TestNG allows you to create parameterized tests by using the @Parameters annotation. Parameterized tests are useful when you want to run the same test method multiple times with different sets of data. Here’s how you can create parameterized tests in TestNG:

Step 1: Define Test Data

First, define the test data you want to use for your parameterized tests. This could be in the form of arrays, lists, or any other data structure that suits your needs.

java
@DataProvider(name = "testdata") public Object[][] testData() { return new Object[][] { {"data1", 1}, {"data2", 2}, {"data3", 3} }; }

In this example, we have a @DataProvider method called testdata that provides an array of arrays, where each inner array represents a set of test data.

Step 2: Use the @Test Annotation with @Parameters

Next, annotate your test method with @Test and use the @Parameters annotation to specify the names of the parameters your test method expects. These parameter names should match the data provider’s names.

java
@Test(dataProvider = "testdata") public void parameterizedTest(String data, int value) { // Your test logic here, using the 'data' and 'value' parameters System.out.println("Data: " + data); System.out.println("Value: " + value); }

In this example, the parameterizedTest method expects two parameters: data (a string) and value (an integer). These parameters correspond to the data provided by the @DataProvider.

Step 3: Run the Parameterized Test

Now, when you run your TestNG suite, TestNG will execute the parameterizedTest method multiple times, once for each set of data provided by the data provider.

Step 4: Configure Your TestNG XML File

Ensure that your TestNG XML file includes the <suite> and <test> elements, specifies the classes containing the parameterized tests, and sets the data provider class.

xml
<suite name="ParameterizedTestSuite"> <test name="ParameterizedTest"> <classes> <class name="com.example.ParameterizedTestExample"/> </classes> </test> </suite>

In this XML file, replace com.example.ParameterizedTestExample with the actual package and class name containing your parameterized tests.

Step 5: Run the TestNG Suite

Run your TestNG suite using your preferred build tool or TestNG runner. The parameterized tests will execute with the different sets of data provided by the data provider.

Demo Day 1 Video:

 
You can find more information about Selenium in this Selenium Link

 

Conclusion:

Unogeeks is the No.1 IT Training Institute for Selenium Training. Anyone Disagree? Please drop in a comment

You can check out our other latest blogs on  Selenium here – Selenium Blogs

You can check out our Best In Class Selenium Training Details here – Selenium Training

💬 Follow & Connect with us:

———————————-

For Training inquiries:

Call/Whatsapp: +91 73960 33555

Mail us at: info@unogeeks.com

Our Website ➜ https://unogeeks.com

Follow us:

Instagram: https://www.instagram.com/unogeeks

Facebook:https://www.facebook.com/UnogeeksSoftwareTrainingInstitute

Twitter: https://twitter.com/unogeeks


Share

Leave a Reply

Your email address will not be published. Required fields are marked *