Showing posts with label Data Driven Testing. Show all posts
Showing posts with label Data Driven Testing. Show all posts

Parameterization in TestNG

  


What is Parameterization in TestNG?


The theory of Parameterization in TestNG is a mechanism for running the same test logic multiple times with different input values (parameters). This technique eliminates the need to duplicate test code when you want to verify a feature across various scenarios, datasets, or environments. Parameterization allows you to externalize the data used by the test methods.

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)
















1. Using @Parameters (from XML)

This approach uses parameters defined externally in the TestNG XML configuration file, suitable for configuration-based data.

(a) Goal: To pass environment-specific or global configuration values into a test without hardcoding them.

(b) Mechanism:
  • 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.

(c) The Contract: TestNG reads the XML file, finds the defined parameter value, and passes it directly to the annotated test method.

(d) Benefit: This is ideal for parameters that don't change per execution but vary per deployment environment, such as a browser name (chrome, firefox), a base URL, or a database connection type. The test method runs only once, using the configured value.

Pass parameters directly from the testng.xml file to your test methods.


Steps for Parameterization in TestNG

  • Define parameters in testng.xml

  • Use @Parameters in the test method

  • Use @BeforeMethod or @Test to access them



Java Code for parameterization in TestNG

Below is Test class with name loginTest() having two parameters user and pass. These parameters get values from @Parameters annotations variables: username and password. these variables username and password gets values from parameter tag in testng.xml. We can see below in testng.xml file

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);
    }
}



testng.xml:

It contains two parameters username and password having name and values as defined below

<!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>



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

We can pass multiple sets of data to a test method from a @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'. 

In test method that is loginTest, We have to include dataprovider method with dataProvider attribute with name of dataprovider method.
Like below we have, @Test(dataProvider = "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