测试用例
![用例](https://i-blog.csdnimg.cn/blog_migrate/20f0d2197089791e06402f6f33ffdc65.png)
![用例2](https://i-blog.csdnimg.cn/blog_migrate/b24063f3d17f3b0aefee78e01a291e26.png)
环境
pom.xml
中添加testng
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.1.0</version>
<scope>test</scope>
</dependency>
使用DataProvider遍历参数
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;
}
}
使用Excel中的数据遍历测试
回忆之前操作Excel的操作
引入POI
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.0</version>
</dependency>
编写一个读取excel的类
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();
}
}
}
}
![结果](https://i-blog.csdnimg.cn/blog_migrate/d6a35966b2d4d5dec52bdbfdef6b9237.png)
问题
编码复杂,按索引不方便适配后续变更
使用EasyPoi
使用文档:
http://easypoi.mydoc.io/
引入
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.0.0</version>
</dependency>
编写excel 表格映射
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 + '\'' +
'}';
}
}
读取excel
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<CaseInfo> caseInfoList = ExcelImportUtil.importExcel(fis, CaseInfo.class, params);
// 4. 关流
fis.close();
for (CaseInfo caseInfo : caseInfoList) {
System.out.println(caseInfo);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
![结果](https://i-blog.csdnimg.cn/blog_migrate/5bd1554a4d80db58eaba5b077a11e0ed.png)
提取封装
为了可以让该方法可以读取更多的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<CaseInfo> list = read(0, 1, CaseInfo.class);
List<API> 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;
}
}
![结果](https://i-blog.csdnimg.cn/blog_migrate/7f4671bc0beaadf6e6c4de1eae37d33b.png)
重写测试代码
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);