接口测试进阶3-1数据驱动测试
数据驱动
相同的测试脚本使用不用的测试数据执行,并完全分离了测试数据和测试行为。这样的模式成为数据驱动
testNG实现数据驱动的两个关键注解
@DataProvider
@Parameters
在当前类中指定数据源
#java
package DateDriver812;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class Lesson1Data {
@DataProvider(name = "provider")
public Object[][] createData(){
/**
* 提供数据源
*/
return new Object[][]{
{"阿里","马云"},
{"腾讯","马化腾"}
};
}
@Test(dataProvider="provider")
public void testDataProvider(String company,String people){
/**
* 接收数据源,每一行作为一组数据,每次接收一组执行一次测试
*/
System.out.println("company:"+company+"\t"+"people:"+people);
}
}
company:阿里 people:马云
company:腾讯 people:马化腾
数据源在其他类中
#java
package DateDriver812;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class Lesson1Data {
@Test(dataProviderClass = testdata.TestData.class,dataProvider = "test1")
/**
* 指定类:packageName.className.class(class不能少)
* 指定dataProvider
*/
public void testDataProviderClass(String company,String people){
System.out.println("company:"+company+"\t"+"people:"+people);
}
}
//数据源
package testdata;
import org.testng.annotations.DataProvider;
public class TestData {
@DataProvider(name = "test1")
public static Object[][] createDdata(){
return new Object[][]{
{"网易","丁磊"},
{"京东","刘强东"}
};
}
}
company:网易 people:丁磊
company:京东 people:刘强东