1. 通过parameter传参
java代码部分:
import org.testng.annotations.Parameters;
import org.testng.annotations.Test; public class ParameterizedTest1 {
@Test
@Parameters("myName") //这里可以带多个参数,且参数名要和xml文件里的一致;但不必与parameterTest()方法里的参数名一致,需要与其参数顺序一致。
public void parameterTest(String myName) {
System.out.println("Parameterized value is : " + myName);
}
}
XML数据文件部分:
<?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. 通过dataprovider传参
2.1 默认使用方法名作为dataprovider标识
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 通过dataprovider制定name标识传参
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);
} }