TestNG Parametrization: Part-1

Parametrization means passing values in Unit test script using TestNG xml file, or using @DataProvider testing annotation

Parametrization using testing xml file:

For this type of parametrization need to declare “@Parameters” annotation in method which needs parameter testing, the value of parameter will be provided by using TestNG suite xml file. Using this we can Test a single unit test for multiple set of values.
Example – we need to create java test file as I created MyTestNG.java file.


package com.test;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class MyTestNG {       
           
        @BeforeClass
        public void beforeClass() {
            System.out.println("@BeforeClass - executed");
        }
         
        @AfterClass
        public void afterClass() {
            System.out.println("@AfterClass - executed");
         }     
                
        @Test
        @Parameters({"fName", "lName"})
        public void testParameterMethod(String fName, String lName) 
        {       
           System.out.println(fName);
           System.out.println(lName);
        }
}

Suite.xml file.


<?xml version="1.0" encoding="UTF-8"?>
<suite name="TestNG Suite">
  <test name="method">
    <parameter name="fName" value="First Name"/>
    <parameter name="lName" value="Last Name"/>
    <classes>
       <class name="com.test.FirstTest" />     
    </classes>
  </test>
</suite>

Output:
 
@BeforeClass - executed
First Name
Last Name
@AfterClass - executed

No comments:

Post a Comment

Leave your comments, queries, suggestion I will try to provide solution