Definition:
TestNG Parameterization means passing different inputs (parameters) to test methods while running the test. This helps in testing the same code with multiple sets of data or running tests in different environments (like on Chrome, Firefox, etc.).
---
Simple Analogy
1. @DataProvider Analogy
Think of a vending machine. You press a button for a snack (test method), and the vending machine gives you a different snack based on what you choose (different inputs or test data).
Test Method = Snack Dispenser
@DataProvider = Different Snacks
Example: The vending machine gives Chips, Chocolate, or Soda (different test data).
2. XML Parameter Analogy
Think of a recipe book. Each recipe specifies ingredients. You decide which recipe to follow based on the occasion.
XML File = Recipe Book
@Parameters = Ingredients specified for the dish
Example: "Make Cake" recipe uses Flour, Sugar, and Eggs (parameters).
---
Examples with Simple Code
1. Using @DataProvider (Testing with Different Data Sets)
Example: Testing a login feature with different usernames and passwords.
import org.testng.annotations.Test;
import org.testng.annotations.DataProvider;
public class LoginTest {
@Test(dataProvider = "loginData")
public void testLogin(String username, String password) {
System.out.println("Testing Login with: " + username + " and " + password);
// Here you can write the login logic
}
@DataProvider(name = "loginData")
public Object[][] provideData() {
return new Object[][] {
{"user1", "pass123"},
{"admin", "admin123"},
{"guest", "guest123"}
};
}
}
Real-Life Example:
Logging into a website with multiple accounts (like Gmail: user1, user2, admin).
---
2. Using XML Parameters (Different Environments or Configurations)
Example: Running the same test on different browsers.
testng.xml File:
<suite name="Cross Browser Suite">
<test name="Chrome Browser">
<parameter name="browser" value="chrome"/>
<classes>
<class name="TestExample"/>
</classes>
</test>
<test name="Firefox Browser">
<parameter name="browser" value="firefox"/>
<classes>
<class name="TestExample"/>
</classes>
</test>
</suite>
Java Test Code:
import org.testng.annotations.Test;
import org.testng.annotations.Parameters;
public class TestExample {
@Test
@Parameters("browser")
public void testBrowser(String browser) {
System.out.println("Running test on: " + browser);
// Code to launch Chrome or Firefox browser
}
}
Real-Life Example:
Testing a website on Chrome and Firefox to make sure it works everywhere.
---
Simple Shortcuts to Remember
1. DAX Rule
D = @DataProvider for Data-Driven Testing.
A = @Parameters to Accept inputs from XML.
X = XML file for multiple configurations.
2. Think of “Vending Machines and Recipe Books”
@DataProvider = Vending Machine (Different snacks).
XML Parameters = Recipe Book (Ingredients for specific dishes).
3. Cheat Sheet:
---
More Examples for Better Understanding
Example 1: Testing a Calculator App
Scenario: Test addition of numbers with different inputs.
@Test(dataProvider = "additionData")
public void testAddition(int num1, int num2, int expectedResult) {
int result = num1 + num2;
System.out.println("Adding: " + num1 + " + " + num2 + " = " + result);
assert result == expectedResult;
}
@DataProvider(name = "additionData")
public Object[][] provideAdditionData() {
return new Object[][] {
{2, 3, 5},
{10, 5, 15},
{0, 0, 0}
};
}
Analogy: Testing a calculator by pressing the + button with different numbers.
---
Example 2: API Testing with Different Payloads
Scenario: Test an API using different JSON payloads.
@Test(dataProvider = "apiData")
public void testAPI(String payload) {
System.out.println("Sending API payload: " + payload);
// Logic to test API with the payload
}
@DataProvider(name = "apiData")
public Object[][] provideApiData() {
return new Object[][] {
{"{\"user\":\"user1\",\"age\":25}"},
{"{\"user\":\"user2\",\"age\":30}"},
{"{\"user\":\"admin\",\"age\":35}"}
};
}
Analogy: Sending different letters (payloads) to the same post office (API) and checking the response.
---
Tricks to Use TestNG Parameterization
1. For Reusability: Use @DataProvider for tests that need to run with different inputs.
2. Environment Flexibility: Use @Parameters and XML to test on different browsers or environments.
3. Dynamic Data: Fetch data from files (like CSV, Excel) or databases in @DataProvider.
4. Parallel Execution: Use XML with parallel="tests" to save time by running tests simultaneously.
---
Quick Recap (In Simple Words)
@DataProvider = Provides data (like a vending machine).
XML + @Parameters = Sets up test environments (like a recipe book).
Use DAX Rule to remember the techniques.
More Data → Use @DataProvider.
More Configurations → Use XML Parameters.
---
Conclusion
TestNG Parameterization helps testers:
1. Test the same code with different inputs (Data-Driven Testing).
2. Test in different settings like browsers or environments.
3. Save time and avoid writing repetitive code.
By using vending machines (DataProvider) and recipe books (XML Parameters), you can create more flexible and powerful tests.