1. 通過(guò)parameter傳參
java代碼部分:
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class ParameterizedTest1 {
@Test
@Parameters("myName") //這里可以帶多個(gè)參數(shù),且參數(shù)名要和xml文件里的一致;但不必與parameterTest()方法里的參數(shù)名一致,需要與其參數(shù)順序一致。
public void parameterTest(String myName) {
System.out.println("Parameterized value is : " + myName);
}
}
XML數(shù)據(jù)文件部分:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
<test name="test1">
<parameter name="myName" value="manisha"/>
<classes>
<class name="ParameterizedTest1" />
</classes>
</test>
</suite>
2. 通過(guò)dataprovider傳參
2.1 默認(rèn)使用方法名作為dataprovider標(biāo)識(shí)
public class DataProviderTest {
@DataProvider
public Object[][] providerMethod(Method method){
Object[][] result = null;
if(method.getName().equals("testmethod1")){
result = {{,1}};
}else if(method.getName().equals("testmethod2")){
result = {{,2}};
}return result;
}
@Test(dataProvider="providerMethod")
public void testmethod1(int param){
System.out.println("method1 received:"+param);
}
@Test(dataProvider="providerMethod")
public void testmethod2(int param){
System.out.println("method2 received:"+param);
}
}
2.2 通過(guò)dataprovider制定name標(biāo)識(shí)傳參
public class DataProviderTest {
@DataProvider(name="test")
public Object[][] providerMethod(Method method){
Object[][] result = null;
if(method.getName().equals("testmethod1")){
result = new Object[][]{new Object[]{1}};
}else if(method.getName().equals("testmethod2")){
result = new Object[][]{new Object[]{2}};
}else{
result = new Object[][]{new Object[]{3}};
}
return result;
}
@Test(dataProvider="test")
public void testmethod1(int param){
System.out.println("method1 received:"+param);
}
@Test(dataProvider="test")
public void testmethod2(int param){
System.out.println("method2 received:"+param);
}
}