pom.xml
中添加testng
org.testng
testng
7.1.0
test
package com.zhongxin.cases;
import com.zhongxin.utils.HttpUtils;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/*
* 注册接口测试
* */
public class RegisterCase {
@Test(dataProvider = "datas")
public void test(String url, String params) {
try {
HttpUtils.post(url, params);
} catch (Exception e) {
e.printStackTrace();
}
}
@DataProvider
public Object[] datas() {
Object[][] datas = {
{"http://api.lemonban.com/futureloan/member/register", "{\"mobile_phone\":\"13877788811\",\"pwd\":\"\"}"},
{"http://api.lemonban.com/futureloan/member/register", "{\"mobile_phone\":\"\",\"pwd\":\"12345678\"}"},
{"http://api.lemonban.com/futureloan/member/register", "{\"mobile_phone\":\"123\",\"pwd\":\"12345678\"}"},
};
return datas;
}
}
org.apache.poi
poi-ooxml
4.0.0
package com.zhongxin.utils;
import org.apache.poi.ss.usermodel.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ExcelUtils {
public static void read() {
FileInputStream fis = null;
try {
fis = new FileInputStream("src/test/resources/cases_v1.xls");
Workbook sheets = WorkbookFactory.create(fis);
Sheet sheet = sheets.getSheetAt(0);
int lastRowNum = sheet.getLastRowNum();
for (int i = 1; i <= lastRowNum; i++) {
Row row = sheet.getRow(i);
int lastCellNum = row.getLastCellNum();
for (int j = 0; j < lastCellNum; j++) {
Cell cell = row.getCell(j, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
cell.setCellType(CellType.STRING);
String cellValue = cell.getStringCellValue();
System.out.println(cellValue + ',');
}
System.out.println();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
结果
编码复杂,按索引不方便适配后续变更
使用文档:
http://easypoi.mydoc.io/
cn.afterturn
easypoi-base
4.0.0
cn.afterturn
easypoi-annotation
4.0.0
package com.zhongxin.pojo;
import cn.afterturn.easypoi.excel.annotation.Excel;
/**
* excel 表格映射
*/
public class CaseInfo {
@Excel(name = "CaseId(用例编号)")
private int id;
@Excel(name = "Name(接口名)")
private String name;
@Excel(name = "Type(接口提交类型)")
private String method;
@Excel(name = "Url(接口地址)")
private String url;
@Excel(name = "Desc(用例描述)")
private String desc;
@Excel(name = "Params(参数)")
private String params;
@Excel(name = "Content-Type")
private String contentType;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getParams() {
return params;
}
public void setParams(String params) {
this.params = params;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
@Override
public String toString() {
return "CaseInfo{" +
"id=" + id +
", name='" + name + '\'' +
", method='" + method + '\'' +
", url='" + url + '\'' +
", desc='" + desc + '\'' +
", params='" + params + '\'' +
", contentType='" + contentType + '\'' +
'}';
}
}
package com.zhongxin.utils;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.zhongxin.pojo.CaseInfo;
import java.io.FileInputStream;
import java.util.List;
public class ExcelUtils {
public static void main(String[] args) {
read();
}
public static void read() {
try {
// 1. excel文件流
FileInputStream fis = new FileInputStream("src/test/resources/cases_v1.xls");
// 2. easypoi 导入参数
ImportParams params = new ImportParams();
// 3. 导入
List caseInfoList = ExcelImportUtil.importExcel(fis, CaseInfo.class, params);
// 4. 关流
fis.close();
for (CaseInfo caseInfo : caseInfoList) {
System.out.println(caseInfo);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
结果
为了可以让该方法可以读取更多的sheet对其进行提取
params.setStartSheetIndex(sheetIndex);//从第x个sheet开始读取
params.setSheetNum(sheetNum);//读取x个sheet
新写一个sheet2
的映射
package com.zhongxin.pojo;
import cn.afterturn.easypoi.excel.annotation.Excel;
public class API {
@Excel(name = "CaseId")
private int id;
@Excel(name = "Name")
private String name;
@Excel(name = "Url")
private String url;
@Excel(name = "Type")
private String method;
@Override
public String toString() {
return "API{" +
"id=" + id +
", name='" + name + '\'' +
", url='" + url + '\'' +
", method='" + method + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
}
分别读取sheet1
和sheet2
package com.zhongxin.utils;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.zhongxin.pojo.API;
import com.zhongxin.pojo.CaseInfo;
import java.io.FileInputStream;
import java.util.List;
public class ExcelUtils {
public static void main(String[] args) {
List list = read(0, 1, CaseInfo.class);
List list2 = read(1, 1, API.class);
for (CaseInfo caseInfo : list) {
System.out.println(caseInfo);
}
System.out.println("=======");
for (API api : list2) {
System.out.println(api);
}
}
public static List read(int sheetIndex, int sheetNum, Class clazz) {
try {
// 1. excel文件流
FileInputStream fis = new FileInputStream("src/test/resources/cases_v1.xls");
// 2. easypoi 导入参数
ImportParams params = new ImportParams();
params.setStartSheetIndex(sheetIndex);//从第x个sheet开始读取
params.setSheetNum(sheetNum);//读取x个sheet
// 3. 导入
List caseInfoList = ExcelImportUtil.importExcel(fis, clazz, params);
// 4. 关流
fis.close();
return caseInfoList;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
结果
package com.zhongxin.cases;
import cn.afterturn.easypoi.excel.annotation.Excel;
import com.zhongxin.pojo.CaseInfo;
import com.zhongxin.utils.ExcelUtils;
import com.zhongxin.utils.HttpUtils;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.util.List;
/**
* 注册接口测试类型
*/
public class RegisterCase {
@Test(dataProvider = "datas")
public void test(CaseInfo caseInfo) {
try {
HttpUtils.post(caseInfo.getUrl(), caseInfo.getParams());
} catch (Exception e) {
e.printStackTrace();
}
}
@DataProvider
public Object[] datas() {
List list = ExcelUtils.read(0, 1, CaseInfo.class);
return list.toArray();
}
}
反射:java代码在 「运行时」 「动态」 获取一个类的属性和方法,或者调用一个对象的属性和方法
实现反射:必须要有字节码对象
Class 字节码对象 约等于 .class 文件
拿到字节码对象就相当于拿到了整个类所有信息
package com.zhongxin.reflect;
public class Student {
public String name;
private int age;
public void eat() {
System.out.println("Student.eat");
}
private void study() {
System.out.println("Student.study");
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
// 1
Class clazz1 = Student.class;
// 2
Class clazz2 = s.getClass();
// 3
String className = "com.zhongxin.reflect.Student";
Class clazz3 = Class.forName(className);
Object o = clazz3.newInstance();
System.out.println(o);
// Student{name='null', age=0}
// com.zhongxin.reflect.Student@8efb846
Field field = clazz3.getField("name");
field.set(o, "张三");
System.out.println(o);
// Student{name='张三', age=0}
Method method = clazz3.getMethod("eat");
method.invoke(o);
// Student.eat
Field field2 = clazz3.getDeclaredField("age");
field2.setAccessible(true);
field2.set(o, 22);
System.out.println(o);