使用Junit和TestNG对测试进行数据驱动

上一篇 / 下一篇  2015-06-13 12:27:34 / 个人分类:selenium

一.junit
1.创建1个返回值为collection的静态方法,用@parameters标记
2.创建成员变量
3.创建构造函数
4.指定类测试必须和Parameterized一起运行

fFp+uP H F0
@RunWith(Parameterized.class)
public class login{
private String username;
private String password;
public login(String username, String password) {
 this.username = username;
 this.password= password;
}
@Parameters
public static Collection getdata() {
 return Arrays.asList(new Object[][] {
  {"username1", "password1" },
  {"username2", "password2" },
  
}
@Test
public void logintest() throws Exception{
  system.out.println(username+password);
}
}
注:junit的参数化是在类级别的,即,每循环一次都将要执行整个类,包括@before和@after
二.TestNG
1.使用dataprovider
2.不用构函数,直接映射
@dataProvider
public object[][] getdata{
   return new object[][]{
{"username1","password1"},{"username2","password2"}
}
}
@Test(dataProvider="getdata")
public void logintest(Stgring username,String password) throws Exception{
  system.out.println(username+password);
}
TestNG的参数化是在测试级别的,
三.使用testNG和POI读取excel
@dataProvider
public object[][] getdata{
 return getexcel("d:\\testdata.xlsx")
}
只需要编写一个返回为二维数组的方法即可
public String[][] getexcel(String Filepath){
   InputStream is=new FileInputStream(filepath);
   XSSFWorkBook wordkbook=new XSSFWorkBook(is);
   XSSFSheet sheet=workbook.getsheet("Sheet1");
   String[][] records=null;
   int count=sheet.getLastRowNum();
   records=new String[count+1][]
   for(Integer i=0;i<=count;i++){
      Row row=sheet.getrow(i);
      Integer cellcount=row.getLastCellNum();
      records[i]=new String[cellcount];
      for(Integer j=0;j<cellcount;i++){
      records[i][j]=sheet.getrow(i).getsheet(j).getStringCellValue();}
51Testing软件测试网1r!mA^.g&J \d
    }
   return records;

oy,O^~0
}

Ocp_3X_@0

TAG:

引用 删除 jack01   /   2019-07-25 18:22:43
1
 

评分:0

我来说两句

Open Toolbar