一,memberApi测试编写
- 前面对部门管理的api进行了测试编写,都还比较简单,在进行member的crudApi测试编写的时候,遇到一个问题,请求体数据过于庞大,使用jsonpath之类的小大小闹显然无法满足请求下面这样的数据,后期维护更是灾难。
```java
{
“userid”: “zhangsan”,
“name”: “张三”,
“alias”: “jackzhang”,
“mobile”: “+86 13800000000”,
“department”: [1, 2],
“order”:[10,40],
“position”: “产品经理”,
“gender”: “1”,
“email”: “zhangsan@gzdev.com”,
“isleader_in_dept”: [1, 0],
“enable”:1,
“avatar_mediaid”: “2-G6nrLmr5EC3MNb-zL1dDdzkd0p7cNliYu9V5w7o8K0”,
“telephone”: “020-123456”,
“address”: “广州市海珠区新港中路”,
“main_department”: 1,
“extattr”: {
}, “to_invite”: true, “external_position”: “高级产品经理”, “external_profile”: {"attrs": [
{
"type": 0,
"name": "文本名称",
"text": {
"value": "文本"
}
},
{
"type": 1,
"name": "网页名称",
"web": {
"url": "http://www.test.com",
"title": "标题"
}
}
]
} }"external_corp_name": "企业简称",
"external_attr": [
{
"type": 0,
"name": "文本名称",
"text": {
"value": "文本"
}
},
{
"type": 1,
"name": "网页名称",
"web": {
"url": "http://www.test.com",
"title": "标题"
}
},
{
"type": 2,
"name": "测试app",
"miniprogram": {
"appid": "wx8bd8012614784fake",
"pagepath": "/index",
"title": "my miniprogram"
}
}
]
- 前章对读取json格式数据并通过map键值对赋值的方式进行了参数处理,可以吧它单独的封装出来提高复用率
<a name="pgNnS"></a>
## 封装读json模板,map参数化的方法
```java
// 在所有类的父类,即 Restful中进行封装
package com.addicated.wework;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import java.util.HashMap;
import static io.restassured.RestAssured.given;
/**
* @description: 请求引擎封装
* @author: Adi
* @time: 2020/10/5 11:28
**/
public class Restful {
HashMap<String, Object> query = new HashMap<String, Object>();
public RequestSpecification requestSpecification = given();
public Response send() {
requestSpecification = given().log().all();
query.entrySet().forEach(entry -> {
requestSpecification.queryParam(entry.getKey(), entry.getValue());
});
return requestSpecification.when().request("get", "baidu.com");
}
// TODO 优化1 ,将读取json文件的方法封装到contact继承的父类中 templateFromJson
public static String template(String path, HashMap<String, Object> map) {
// 因为是个静态方法,所以获取路径的时候有些许不一样 注意点
DocumentContext documentContext = JsonPath.parse(Restful.class
.getResourceAsStream(path));
// 这种书写方式的有点是,传值可以传一个也可多可,泛用性更强
map.entrySet().forEach(entry -> {
documentContext.set(entry.getKey(), entry.getValue());
});
return documentContext.jsonString();
}
}
-------使用例子 继承关系为 Member extends Contact extends Restful
package com.addicated.wework.contact;
import io.restassured.response.Response;
import java.util.HashMap;
/**
* @description:
* @author: Adi
* @time: 2020/10/4 19:12
**/
public class Member extends Contact {
public Response create(HashMap<String, Object> map) {
String body = template("/data/member_create.json", map);
reset();
return requestSpecification.body(body)
.when().post("https://qyapi.weixin.qq.com/cgi-bin/user/create")
.then().log().all().extract().response();
}
}
直接在子类中通过template方法进行传参调用即可,一参为path,二参为要进行赋值的测试用例,为map类型
二,Junit5 简单参数化
参照下面如图,junit5提供了多种参数化的实现方式,代码展示最简单的valueSource
- ValueSource注解中声明要进行遍历的测试数据即可
- ```java package com.addicated.wework.contact;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource;
import java.util.Arrays; import java.util.HashMap;
import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.jupiter.api.Assertions.*;
class MemberTest { static Member member;
@BeforeAll
static void setUp() {
member = new Member();
}
@ParameterizedTest
@ValueSource(strings = {"sevenriby_", "hogwarts_", "testerhome_"})
// valueSource 后面列出参数化的数据,之后通过形参name传入测试类方法,用例执行时会自动进行传参进行
// 数据驱动
void create(String name) {
String nameNew = name + member.random;
String random = String.valueOf(System.currentTimeMillis()).substring(5 + 0, 5 + 8);
HashMap<String, Object> map = new HashMap<>();
map.put("userid", nameNew);
map.put("name", nameNew);
map.put("department", Arrays.asList(1, 2));
map.put("mobile", "151" + random);
map.put("userid", nameNew);
map.put("email", random + "@qq.com");
member.create(map).then().statusCode(200).body("errcode", equalTo(0));
}
}
<a name="Prwu6"></a>
# 三,如何避免产生过多的脏数据
- 比如说,创建了N多部门,这时为了避免过多脏数据影响测试,要写一个整体的清除方法,之后在每次添加数据之前进行调用,保证每次测试时环境都是干净的
```java
// delete all 清除测试产生的脏数据
public Response deleteAll(){
reset();
// 取得所有部门的id
List<Integer> idList = list("").then().log().all().extract().path("department.id");
System.out.println(idList);
idList.forEach(id -> delete(id.toString()));
return null;
}
--------进行测试的时候进行清楚
class DepartmentTest {
Department department;
String random=String.valueOf(System.currentTimeMillis());
@BeforeEach
void setUp() {
if(department==null){
department=new Department();
// 写在setup中,这样每次用例执行之前都会去清空之前的数据,以保证清洁
department.deleteAll();
}
}
@Test
void list() {
department.list("").then().statusCode(200);
department.list("33").then().statusCode(200);
}