如何登录RestAssured Request和Response信息ExtentReports日志(

2019-09-26 08:14发布

我放心了,TestNG的基础的自动化套件,它使用程度的报告。 我需要一种方法来使从静止又向信息记录到报告多大程度上所有日志。

我使用登录放心请求和响应到文件和控制台log4j的,可以将这些日志被送入程度的报告?

有没有办法做到这一点?

Answer 1:

下面的示例代码生成的程度报告和日志的详细信息。 请不是你需要以下的依赖,以及成功执行。

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
</dependency>
<dependency>
<groupId>com.relevantcodes</groupId>
<artifactId>extentreports</artifactId>
<version>2.41.2</version>
</dependency>

<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>3.0.6</version>
</dependency>

确保上述相关性在POM更新

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.StringWriter;

import org.apache.commons.io.output.WriterOutputStream;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;

import io.restassured.RestAssured;
import io.restassured.filter.log.RequestLoggingFilter;
import io.restassured.response.Response;

/**
 * @author vamsir
 *
 */

public class SampleExtentReport {

    static StringWriter requestWriter;
    static PrintStream requestCapture;
    static ExtentTest test;
    static ExtentReports extentReports;

    @BeforeSuite
    public void init(ITestContext context) {


        extentReports = new ExtentReports(System.getProperty("user.dir") + File.separator + "reports" + File.separator
                +this.getClass().getSimpleName().toString() + ".html");

    }



    @Test
    public static void sampleTest() {

        test = extentReports.startTest("Get Sample Test");

        requestWriter = new StringWriter();
        requestCapture = new PrintStream(new WriterOutputStream(requestWriter));

        Response response = RestAssured.given().filter(new RequestLoggingFilter(requestCapture)).and().baseUri("https://jsonplaceholder.typicode.com").and().basePath("/todos/1").when().get();

        requestCapture.flush();
    System.out.println("Request: "+requestWriter.toString());
    System.out.println("Response: "+response.asString());
    test.log(LogStatus.INFO, "Request : "+ requestWriter.toString());
    test.log(LogStatus.INFO, "Response : " + response.asString());

        extentReports.endTest(test);

    }


    @AfterMethod
    public void getResult(ITestResult result) throws IOException {
        if (result.getStatus() == ITestResult.FAILURE) {
            test.log(LogStatus.FAIL, result.getThrowable());
        }
    }

    @AfterSuite
    public void end() {

        extentReports.flush();
        extentReports.close();

    }

}

下面是我使用上述代码段接收到的输出样本:

[RemoteTestNG] detected TestNG version 6.14.3
Request: Request method:    GET
Request URI:    https://jsonplaceholder.typicode.com/todos/1
Proxy:          <none>
Request params: <none>
Query params:   <none>
Form params:    <none>
Path params:    <none>
Headers:        Accept=*/*
Cookies:        <none>
Multiparts:     <none>
Body:           <none>

Response: {
  "userId": 1,
  "id": 1,
  "title": "delectus aut autem",
  "completed": false
}
PASSED: sampleTest

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================


文章来源: How to log RestAssured Request and Response information to ExtentReports logs