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,
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
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
In TestNG there are two ways by which we can pass parameters to a test.They are,
- By TestNG's XML configuration files(testng.xml)
- By @DataProvider
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
- package example.test;
- import org.testng.annotations.*;
- /**
- * TestNG Parameterized Test
- *
- */
- public class TestNGParameterizedTest {
- @Test
- @Parameters(value="input")
- public void parameterTest(int input) {
- }
- }
- <suite name="My test suite">
- <test name="Parameterized test">
- <parameter name="input" value="7"/>
- <classes>
- <class name="example.test.TestNGParameterizedTest" />
- </classes>
- </test>
- </suite>
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
- package sourse;
- public class CharASCII {
- public static int CharToASCII(final char character){
- return (int)character;
- }
- public static char ASCIIToChar(final int ascii){
- return (char)ascii;
- }
- }
- package example.test;
- import org.testng.*;
- import org.testng.annotations.DataProvider;
- import org.testng.annotations.Test;
- import sourse.CharASCII;
- public class CharASCIITest {
- @DataProvider
- }
- @Test(dataProvider = "ValidDataProvider")
- public void CharToASCIITest(final char character, final int ascii) {
- int result = CharASCII.CharToASCII(character);
- Assert.assertEquals(result, ascii);
- }
- @Test(dataProvider = "ValidDataProvider")
- public void ASCIIToCharTest(final char character, final int ascii) {
- char result = CharASCII.ASCIIToChar(ascii);
- Assert.assertEquals(result, character);
- }
- }
No comments:
Post a Comment