1. XML file for parameterized test.
Declare “@Parameters” annotation in method which needs parameter testing, the parametric data will be provide by 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 or QE can provide their own data sets for testing.
import org.testng.annotations.*;
/**
* TestNG Parameterized Test
* @author mkyong
*
*/
public class TestNGTest6_1_0 {
@Test
@Parameters(value="number")
public void parameterIntTest(int number) {
System.out.println("Parameterized Number is : " + number);
}
}
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
<suite name="My test suite">
<test name="testing">
<parameter name="number" value="2"/>
<classes>
<class name="TestNGTest6_1_0" />
</classes>
</test>
</suite>
Result
Parameterized Number is : 2
2. @DataProvider for parameterized test.
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 @DataProviderannotation, which facilitates the mapping of complex parameter types to a test method.
1. @DataProvider for Vector, String or Integer as parameter
import java.util.Vector;
import org.testng.annotations.*;
/**
* TestNG Parameterized Test - Advance
* @author mkyong
*
*/
public class TestNGTest6_2 {
@Test(dataProvider = "Data-Provider-Function")
public void parameterIntTest(Class clzz, String[] number) {
System.out.println("Parameterized Number is : " + number[0]);
System.out.println("Parameterized Number is : " + number[1]);
}
//This function will provide the parameter data
@DataProvider(name = "Data-Provider-Function")
public Object[][] parameterIntTestProvider() {
return new Object[][]{
{Vector.class, new String[] {"java.util.AbstractList",
"java.util.AbstractCollection"}},
{String.class, new String[] {"1", "2"}},
{Integer.class, new String[] {"1", "2"}}
};
}
}
Result
Parameterized Number is : java.util.AbstractList
Parameterized Number is : java.util.AbstractCollection
Parameterized Number is : 1
Parameterized Number is : 2
Parameterized Number is : 1
Parameterized Number is : 2
PASSED: parameterIntTest(class java.util.Vector, [Ljava.lang.String;@1016632)
PASSED: parameterIntTest(class java.lang.String, [Ljava.lang.String;@10a6ae2)
PASSED: parameterIntTest(class java.lang.Integer, [Ljava.lang.String;@4a6cbf)
2. @DataProvider for object as parameter
“TestNGTest6_3_0” is a simple object with simple get set methods.
/**
* TestNG Parameterized Test - Advance
* @author mkyong
*
*/
public class TestNGTest6_3_0 {
private int number;
private String msg;
public void setNumber(int number){
this.number = number;
}
public int getNumber(){
return this.number;
}
public void setMsg(String msg){
this.msg = msg;
}
public String getMsg(){
return this.msg;
}
}
import org.testng.annotations.*;
/**
* TestNG Parameterized Test - Advance
*
*/
public class TestNGTest6_3_1 {
@Test(dataProvider = "Data-Provider-Function")
public void parameterIntTest(TestNGTest6_3_0 clzz) {
System.out.println("Parameterized Number is : " + clzz.getMsg());
System.out.println("Parameterized Number is : " + clzz.getNumber());
}
//This function will provide the patameter data
@DataProvider(name = "Data-Provider-Function")
public Object[][] parameterIntTestProvider() {
TestNGTest6_3_0 obj = new TestNGTest6_3_0();
obj.setMsg("Hello");
obj.setNumber(123);
return new Object[][]{
{obj}
};
}
}
Result
Parameterized Number is : 123
PASSED: parameterIntTest(TestNGTest6_3_0@dc6a77)
TestNG’s Parameterized test is very user friendly and flexible (either in XML file or inside the class). It can support many complex data type as parameter value and the possibility is unlimited. As example above, you even can pass in your own object (TestNGTest6_3_0) for Parameterized test.
No comments:
Post a Comment