Saturday, December 31, 2011

Parameterized Test TestNG

Sometimes in TestNG test we need various parameter values for the unit test,we call the test as 'Parameterized Test'.
In TestNG there are two ways by which we can pass parameters to a test.They are,
  1. By TestNG's XML configuration files(testng.xml)
  2. By @DataProvider
1.By TestNG's XML configuration files(testng.xml):

Declare "@Parameters" annotation in method which needs parameter testing, the parametric data will be provide by testng.xml(TestNG's XML configuration files). By doing this, you can reuse a single test case with different data sets easily. In addition, even end user, QA can provide their own data sets for testing.
//TestNGParameterizedTest
  1. package example.test;
  2. import org.testng.annotations.*;
  3. /**
  4.  * TestNG Parameterized Test
  5.  *
  6.  */
  7. public class TestNGParameterizedTest {
  8.     @Test
  9.     @Parameters(value="input")
  10.     public void parameterTest(int input) {
  11.        System.out.println("Parameterized input is : " + input);
  12.     }
  13. }
//testng.xml
  1. <suite name="My test suite">
  2.   <test name="Parameterized test">
  3.     <parameter name="input" value="7"/>      
  4.     <classes>
  5.        <class name="example.test.TestNGParameterizedTest" />
  6.     </classes>
  7.   </test>
  8. </suite>
2 .By @DataProvider:

In the 1st way,while pulling data values into an XML file can be quite handy, but test cases occasionally may require complex data types, which can’t be represented as a String or a primitive value in XML file. TestNG handles this scenario with "@DataProvider" annotation, which facilitates the mapping of complex parameter types to a test method.
//CharASCII.java
  1. package sourse;
  2. public class CharASCII {
  3.    
  4.     public static int CharToASCII(final char character){
  5.         return (int)character;
  6.     }
  7.     public static char ASCIIToChar(final int ascii){
  8.         return (char)ascii;    
  9.     }
  10. }
//CharASCIITest.java
  1. package example.test;
  2. import org.testng.*;
  3. import org.testng.annotations.DataProvider;
  4. import org.testng.annotations.Test;
  5. import sourse.CharASCII;
  6. public class CharASCIITest  {
  7.     @DataProvider
  8.     public Object[][] ValidDataProvider() {
  9.         return new Object[][]{ { 'A'65 },{ 'B'66 },{ 'C'67 },{ 'D'68 },  { 'Z'90 } };
  10.     }
  11.     @Test(dataProvider = "ValidDataProvider")
  12.     public void CharToASCIITest(final char character, final int ascii) {
  13.         System.out.println("ASCII of "+character+" is "+ascii);
  14.         int result = CharASCII.CharToASCII(character);
  15.         Assert.assertEquals(result, ascii);
  16.     }
  17.     @Test(dataProvider = "ValidDataProvider")
  18.     public void ASCIIToCharTest(final char character, final int ascii) {
  19.         System.out.println("Char of "+ascii+" is "+character);
  20.         char result = CharASCII.ASCIIToChar(ascii);
  21.         Assert.assertEquals(result, character);
  22.     }  
  23. }

No comments:

Post a Comment