@Parameters
測試的時候,測試的數(shù)據(jù)可以放在testng.xml文件中,被測試的方法通過@Parameters注解傳遞到測試方法中
parameterizedTest.java
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class parameterizedTest {
@Test
@Parameters("myName")
public void testMethod(String myName) {
System.out.println("myName:" + myName);
}
}
testng.xml:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" >
<test name="Regression1" preserve-order="true">
<parameter name="myName" value="大盜如風(fēng)"></parameter>
<classes>
<class name="parameterizedTest"></class>
</classes>
</test>
</suite>
運行testng.xml,結(jié)果如下:
myName:大盜如風(fēng)
@DataProvider
@Test(dataProvider = "")
如果測試的數(shù)據(jù)較多的情況下,很顯然這種方式不適合,那么可以通過@DataProvider生成測試數(shù)據(jù),通過@Test(dataProvider = "")使用測試數(shù)據(jù)
parameterizedTest2.java
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class parameterizedTest2 {
// 定義數(shù)據(jù)源
@DataProvider(name = "test")
public Object[][] createData() {
return new Object[][]{{"dadaorufeng",29},{"zhangsan",28}};
}
// 使用數(shù)據(jù)源
@Test(dataProvider = "test")
public void verifyData(String name, int age) {
System.out.println("name:" + name + ",age:" + age);
}
}
testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" >
<test name="Regression1" preserve-order="true">
<classes>
<class name="parameterizedTest2"></class>
</classes>
</test>
</suite>