What is Parameterization in TestNG?
Parameterization in TestNG allows us to run the same test with different sets of data. It helps in data-driven testing, where test data is separated from the test logic. TestNG provides multiple ways to do this, mainly:
- @Parameters annotation (via
testng.xml)
- @DataProvider annotation (method-based)
@Parameters (from XML)- Parameters are defined using the <parameter> tag within the testng.xml file (at the Suite, Test, or Class level).
- The test method uses the @Parameters annotation to declare the name of the configuration parameter it expects to receive.
testng.xml file to your test methods.Steps for Parameterization in TestNG
Define parameters in
testng.xmlUse
@Parametersin the test methodUse
@BeforeMethodor@Testto access them
import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class ParameterExample { @Test @Parameters({"username", "password"}) public void loginTest(String user, String pass) { System.out.println("Username: " + user); System.out.println("Password: " + pass); } }
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test name="LoginTest"> <parameter name="username" value="admin" /> <parameter name="password" value="admin123" /> <classes> <class name="ParameterExample" /> </classes> </test> </suite>
@DataProvider
The most flexible and common theoretical approach uses the @DataProvider annotation.
(a) Goal: To separate the test data from the test logic.
(b) Mechanism: A dedicated method is marked with the @DataProvider annotation. This method is theoretically responsible for generating and returning multiple sets of input data (e.g., a list of usernames and passwords, or a list of numbers to test a calculator function).
(c) The Contract: The main test method (marked with @Test) declares that it will accept parameters and specifies the name of the Data Provider it needs. TestNG acts as the mediator:- It calls the Data Provider method first.
- For every set of data returned by the provider, TestNG instantiates and runs the @Test method once.
(d) Benefit: This creates Data-Driven Testing, which is crucial for efficiency and coverage, as one test method can cover hundreds of cases simply by supplying different data.@DataProvider method.Below is the Java Code for @DataProvider in TestNG:
Below, there is a getData() which is annotated with @DataProvider annotation having name as 'loginData'.
import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class DataProviderExample { @Test(dataProvider = "loginData") public void loginTest(String username, String password) { System.out.println("Testing with Username: " + username + ", Password: " + password); } @DataProvider(name = "loginData") public Object[][] getData() { return new Object[][] { {"admin", "admin123"}, {"user", "user123"}, {"guest", "guest123"} }; } }
Suggested Posts:
1. Groups in TestNG
2. Annotations in TestNG
3. TestNG Asserts
4. First Script in TestNG
5. Read Excel Data by Data provider