1

我有基于 Rest Assured-TestNG 的自动化套件,它使用范围报告。我需要一种方法来使来自 Rest Assured 的所有日志也登录到范围报告中。

我正在使用 log4j 将 Rest Assured 请求和响应记录到文件和控制台中,这些日志可以输入到范围报告中吗?

有什么办法吗?

4

1 回答 1

2

以下示例代码生成范围报告并记录详细信息。请注意,您还需要以下依赖项才能成功执行。

<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
===============================================
于 2019-03-15T19:51:39.623 回答