public class ParameterizedTest1 {
@Test
@Parameters("myName")
public void parameterTest(String myName) {
System.out.println("Parameterized value is : " + myName);
}
}
创建testng.xml文件
public class PrimeNumberChecker {
public Boolean validate(final Integer primeNumber) {
for (int i = 2; i < (primeNumber / 2); i++) {
if (primeNumber % i == 0) {
return false;
}
}
return true;
}
}
创
支持的类型:
String int/Integer boolean/Boolean byte/Byte char Character
public class PrimeNumberChecker {
public Boolean validate(final Integer primeNumber) {
for (int i = 2; i < (primeNumber / 2); i++) {
if (primeNumber % i == 0) {
return false;
}
}
return true;
}
}
创建测试案例类
// This test will run 4 times since we have 5 parameters defined
@Test(dataProvider = "test1")
public void testPrimeNumberChecker(Integer inputNumber,
Boolean expectedResult) {
System.out.println(inputNumber + " " + expectedResult);
Assert.assertEquals(expectedResult,
primeNumberChecker.validate(inputNumber));
}
}
创建testng.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
<test name="test1">
<classes>
<class name="data.ParamTestWithDataProvider1" />
</classes>
</test>
</suite>
测试结果
实例2
@DataProvider传递对象作为参数
创建Java类
package data;
public class Bean {
private String val;
private int i;
public Bean(String val, int i){
this.val=val;
this.i=i;
}
public String getVal() {
return val;
}
public void setVal(String val) {
this.val = val;
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
}
public class ParamTestWithDataProvider2 {
@DataProvider(name = "test1")
public static Object[][] primeNumbers() {
return new Object[][] { { new Bean("hi I am the bean", 111) } };
}