写点什么

干货 | REST-assured 获取日志到文件并结合 Allure 报告进行展示

  • 2022 年 9 月 09 日
    北京
  • 本文字数:2841 字

    阅读完需:约 9 分钟

使用 Rest-assured 集合 Allure 运行完用例之后,在生成的报告中只有断言信息,没有请求的日志信息。而当我们的用例失败时,特别是接口失败时,请求日志是分析原因的第一手资源。那如何将 Rest-assured 产生的日志存入 Allure 里,并且能和用例一一对应起来呢?


在使用 Rest-assured 集合 Allure 运行完用例之后,查看生成的报告信息如下:


我们可以看到在生成的报告中只有断言信息,而没有请求的日志信息,而当我们的用例失败时,特别是接口失败时,请求日志是分析原因的第一手资源;


其实 Rest-assured 是有请求日志的,可以通过在 given()和 then()后面加上.log().all()来打印全部的日志信息:那么问题来了,如何将这打印出来的日志信息都"转移"到 Allure 报告中呢?并且能和用例一一对应起来,然后就开始了探索之路~


首先来看一下 Allure 报告可以如何展示日志,在学习 Allure 的过程中发现 Allure 有添加附件展示的功能,那么我就直接想到将日志能存入文件然后添加到报告附件不就可以了吗?由此,Allure 端的解决方向确定。接下来就是要想法办将 Rest-assured 产生的日志存入文件了;整体思路:【Rest-assured 打印日志】- 【Rest-assured 日志存入文件】- 【文件以附件形式传入 Allure】- 【Allure 展示附件日志】


先看一下 Allure 添加附件的两种方法:@Attachment:在方法上添加注解 @Attachment,方法的返回值就会作为附件上传,可添加展示文本和附件类型


@Attachment(value = "Page screenshot", type = "image/png")


Allure.addAttachment:通过 addAttachment 方法指定要添加的附件和展示信息


public static void addHttpLogToAllure() {try {Allure.addAttachment("接口请求响应日志",new FileInputStream("src/main/resources/test.log"));} catch (FileNotFoundException e) {e.printStackTrace();}}


由于在框架中,我已经进行了封装,每个接口请求后都会返回 response 信息。所以一开始我想着从拿到 response 信息进行存储,查阅官方文档,寻找 response 信息获取的相关 API,发现 response.asString();可以获取到 json body 的信息,就先尝试使用。


//// Get the response body as a Stringresponse.asString();


// Get all headersHeaders allHeaders = response.getHeaders();// Get a single header value:String headerName = response.getHeader("headerName");


// Get all cookies as simple name-value pairsMap<String, String> allCookies = response.getCookies();// Get a single cookie value:String cookieValue = response.getCookie("cookieName");


// Get status lineString statusLine = response.getStatusLine();// Get status codeint statusCode = response.getStatusCode();


先创建方法,用于接收 response 获取信息 @Attachment("响应报文")public static String respondBody(Response response) {


boolean prettyFormat = true;JSONObject jsonObject = JSONObject.parseObject(response.asString());String responseBody = JSONObject.toJSONString(jsonObject,prettyFormat);

return responseBody;
复制代码


}


再创建方法,用于接收请求信息,由于我的所有请求信息都传入了一个 Restful 对象中且未找到 Rest-assured 关于请求信息直接获取的 API,这里我就直接取 Restful 对象 Restful 对象:import lombok.Data;import java.util.HashMap;


@Datapublic class Restful {


public String url;public String method;public HashMap<String,Object> header = new HashMap<>();public HashMap<String,Object> query = new HashMap<>();public HashMap<String,Object> pathQuery = new HashMap<>();public String body;
复制代码


}


@Attachment("请求信息")public static String requestBody(Restful restful) {//报告展现请求信息 return restful.toString();}


最后创建一个总的接收方法加入请求流程中,在每个请求结束后获取日志信息进行附件添加 public static void getRequestAndRespondBody(Restful restful, Response response){requestBody(restful);respondBody(response);}


从结果可以看到请求和响应报文已经成功展示,说明这种实现的思路的可行性,只是展示的日志信息还不满意,还是先想要全部的请求和响应信息且是格式化后的,不仅仅只有报文,继续探索~


在研究过程中发现 RestAssured 提供了 logConfig 方法,可以将原本在 Console 中打印的信息指定格式化输出到文件中,具体用法如下(这里指演示重点实现原理部分,其余封装细节太冗余就不展示了):


WriterOutputStream 用到依赖如下:<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.4</version></dependency>


在请求中设置 log 打印方式和存储路径 public void addLogToFile(){try (FileWriter fileWriter = new FileWriter("src/main/resources/test.log");PrintStream printStream = new PrintStream(new WriterOutputStream(fileWriter), true)) {RestAssured.config = RestAssured.config().logConfig(LogConfig.logConfig().defaultStream(printStream));given().XXX.log().all().when().XXXthen().log().all();} catch (IOException e) {e.printStackTrace();}}


创建附件添加方法加入请求流程中,在每个请求结束后获取日志信息进行附件添加 public static void addHttpLogToAllure() {try {Allure.addAttachment("接口请求响应日志",new FileInputStream("src/main/resources/test.log"));} catch (FileNotFoundException e) {e.printStackTrace();}}


整体的流程思路就是:@Testvoid testAllureReport(){addLogToFile();addHttpLogToAllure();}


在下面展示的用例中有 2 个接口请求,可以看到分别记录展示了,且格式与 Console 中格式化打印的保持一致。


Rest-assured 提供了过滤器 Filters,利用它可以串改请求,设置鉴权信息,过滤 log 等,具体的可在官网中进行学习研究,这里主要用到 RequestLoggingFilter()和 ResponseLoggingFilter() 来实现我们的需求。RequestLoggingFilter() 和 ResponseLoggingFilter() 可以将所有的请求和响应的 log 进行打印,而我们想要的是将 log 存入文件,因此还要借助方法 logRequestTo(PrintStream stream),指定 log 的格式化输出到文件中:


FileWriter fileWriter = null;try {fileWriter = new FileWriter("src/main/resources/test.log");} catch (IOException e) {e.printStackTrace();}PrintStream printStream = new PrintStream(new WriterOutputStream(fileWriter), true);RestAssured.filters(new RequestLoggingFilter().logRequestTo(printStream),new ResponseLoggingFilter().logResponseTo(printStream));


附件添加复用上述的方法:


AllureAttachment.addHttpLogToAllure();


结果依然实现了我们的需求


点击下方链接免费领取:性能测试+接口测试+自动化测试+测试开发+测试用例+简历模板+测试文档

http://qrcode.testing-studio.com/f?from=infoQ&url=https://ceshiren.com/t/topic/22265

用户头像

社区:ceshiren.com 微信:ceshiren2021 2019.10.23 加入

微信公众号:霍格沃兹测试开发 提供性能测试、自动化测试、测试开发等资料,实时更新一线互联网大厂测试岗位内推需求,共享测试行业动态及资讯,更可零距离接触众多业内大佬。

评论

发布
暂无评论
干货 | REST-assured 获取日志到文件并结合 Allure 报告进行展示_霍格沃兹测试开发学社_InfoQ写作社区