针对于个人公司接口测试环境的小优化,解决测试人员因为不深刻理解代码逻辑,或者不知道数据结构,或者不明白dao 对象的组成属性,而做的一个统一的自动化的结果数据对比。针对api 请求结果做断言,传统的我们可能会使用assert 方法针对我们需要的关键信息进行断言,比如请求相应的code 值,断言是否接口请求成功:
而我们今天要写的方法,是要直接对请求结果全部信息一次性对比。思路是这样的:
因为通常我们做接口测试是,给一组固定的入参,请求api 获得一固定的响应,然后可能需要去查询数据库,进行请求结果和实际结果进行断言。如下:
如图,这才是我们要真正要写的东西,其他的处理,以及对比,我们最好都交给框架和工具去做,我们针对规范化的API 结果数据,做这样的处理,
import org.json.JSONException;
import org.skyscreamer.jsonassert.JSONAssert;
import java.util.Map;
public class AssertResponseResult {
public static void assertResponseResult( String result,String CaseName){
Map resultMap= JsonUtil.parseJson2Map(result);
resultMap.remove("monitorInfo");
String resultJson=JsonUtil.map2JsonString(resultMap);
JSONAndFile.WriteStringToFile(resultJson,CaseName);
String expected = JSONAndFile.readFileToString(CaseName);
try {
JSONAssert.assertEquals(expected, resultJson, true);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
其中:JsonUtil.parseJson2Map(result); 方法是我们已经封装过的 com.alibaba.fastjson 的方法,来把json 对象对象转化成map 对象,然后剔除掉接口响应里每次随机变化的数据,再次转化为json String ,写入文档中。第一次写入的数据作为BaseData数据,后续写入的数据作为 ContrastData 数据,每次接口测试都拿当前请求的结果与 BaseData数据做对比断言。这里我们使用JSONAssert.assertEquals(expected, resultJson, true); 该方法,依赖开源jar
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.4.0</version>
</dependency>
针对数据的写入和读取,我们做如下处理:
public class JSONAndFile {
/**
* 读文件
* @return
*/
public static String readFileToString(String caseName) {
String fileName = String.format("%s_BaseData.html", caseName);
int len = 0;
StringBuffer str = new StringBuffer();
File file = new File("src/main/resources/BaseResult/" + fileName);
try {
FileInputStream is = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader in = new BufferedReader(isr);
String line = null;
while ((line = in.readLine()) != null) {
if (len != 0) { // 处理换行符的问题
str.append("\r\n" + line);
} else {
str.append(line);
}
len++;
}
in.close();
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
/**
* 写文件
* @param data
*/
public static void WriteStringToFile(String data,String caseName) {
String fileBaseDataName = String.format("%s_BaseData.html", caseName);
File fileBase = new File("src/main/resources/BaseResult/" + fileBaseDataName);
System.out.println(fileBase.getPath());
if (!fileBase.exists()) { //如果存在不写入
try {
FileOutputStream fos = new FileOutputStream(fileBase.getPath());
String s = data;
fos.write(s.getBytes());
System.out.println("写入"+fileBaseDataName+"成功!");
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
} else {
try {
String fileContrastDataName=String.format("%s_ContrastData.html", caseName);
File fileContrast = new File("src/main/resources/ContrastResult/" + fileContrastDataName);
FileOutputStream fos = new FileOutputStream(fileContrast.getPath());
String s = data;
fos.write(s.getBytes());
System.out.println("写入"+fileContrastDataName+"成功!");
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
我们将基础数据和对比数据分别放入不同的文件路径下,区别对待。后续针对本次的写入的结果,可以尝试做一个前端对比工具,用于分析接口测试结果和基础数据的差异。便不必要每次都去分析代码哪里问题了,附上类似当前已有的线上json 对比工具:https://www.sojson.com/jsondiff.html