Java 获得;卷曲;每次放心测试的操作和未通过测试时在控制台中打印

Java 获得;卷曲;每次放心测试的操作和未通过测试时在控制台中打印,java,rest,curl,testing,rest-assured,Java,Rest,Curl,Testing,Rest Assured,我需要从每个放心测试中获取“CURL”操作,并在未通过测试时在控制台中打印。有可能吗 对于AxSample,转换: given().relaxedHTTPSValidation(). auth().basic("name", "password"). param("grant_type","client_credentials"). when(). post("https://test_url/oauth/token"). then(). statusCode(20

我需要从每个放心测试中获取“CURL”操作,并在未通过测试时在控制台中打印。有可能吗

对于AxSample,转换:

 given().relaxedHTTPSValidation().
   auth().basic("name", "password").
   param("grant_type","client_credentials").
 when().
   post("https://test_url/oauth/token").
 then().
   statusCode(200);


包括以下依赖项

马文:

<dependency>
  <groupId>com.github.dzieciou.testing</groupId>
  <artifactId>curl-logger</artifactId>
  <version>1.0.4</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.6.6</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.0.9</version>
</dependency>
输出:

[main] DEBUG curl - **
curl 'https://jsonplaceholder.typicode.com/posts' 
-H 'Accept: */*' 
-H 'Content-Type: application/json; charset=UTF-8' 
-H 'Host: jsonplaceholder.typicode.com' 
-H 'Connection: Keep-Alive' 
-H 'User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_181)' 
--data-binary '{"emailAddress":"sample@domain.com"}' 
--compressed 
-k 
-v**

PASSED: sampleAPITest

另一种方法是解析
log().all()的输出

注意:我没有在这里共享代码,因为它是Apache2.0许可的,这与不同。

基于此,我们完全实现了它

聚甲醛

在运行终端上生成此位:

如果触发测试后无法立即在运行终端上看到它,或者如果需要将其记录到标准系统输出或.log文件中,请按照下面的说明在test/resources文件夹下创建logback.xml文件,这可能会对您有所帮助

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
    </encoder>
</appender>

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>mylog.log</file>
    <append>true</append>
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%-5level] %logger{15} - %msg%n%rEx</pattern>
    </encoder>
</appender>

<logger name="curl" level="DEBUG">
    <appender-ref ref="FILE"/>
</logger>


我没有尝试过,但链接中的细节与您的提问非常相似-
[main] DEBUG curl - **
curl 'https://jsonplaceholder.typicode.com/posts' 
-H 'Accept: */*' 
-H 'Content-Type: application/json; charset=UTF-8' 
-H 'Host: jsonplaceholder.typicode.com' 
-H 'Connection: Keep-Alive' 
-H 'User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_181)' 
--data-binary '{"emailAddress":"sample@domain.com"}' 
--compressed 
-k 
-v**

PASSED: sampleAPITest
<http.client.version>4.5.3</http.client.version>
<curl.logger.version>1.0.5</curl.logger.version>

<dependency>
   <groupId>org.apache.httpcomponents</groupId>
   <artifactId>httpclient</artifactId>
   <version>${http.client.version}</version>
</dependency>
<dependency>
   <groupId>com.github.dzieciou.testing</groupId>
   <artifactId>curl-logger</artifactId>
   <version>${curl.logger.version}</version>
</dependency>
public class Requests {
    public String baseUrl;
    public RequestSpecification getRequestSpecification(String authorizationToken) {
        /** Enables printing request as curl under the terminal as per https://github.com/dzieciou/curl-logger */
        Options options = Options.builder()
                .printMultiliner()
                .updateCurl(curl -> curl
                        .removeHeader("Host")
                        .removeHeader("User-Agent")
                        .removeHeader("Connection"))
                .build();
        RestAssuredConfig config = CurlLoggingRestAssuredConfigFactory.createConfig(options);
        baseUrl = Constants.getEndpoint();
        RestAssured.baseURI = baseUrl;
        RequestSpecification rq = given()
                .config(config)
                .contentType(ContentType.JSON)
                .contentType("application/json\r\n")
                .header("Accept", "application/json").and()
                .header("Content-Type", "application/json")
                .header("Authorization", authorizationToken)
                .when()
                .log()
                .everything();
        return rq;
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
    </encoder>
</appender>

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>mylog.log</file>
    <append>true</append>
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%-5level] %logger{15} - %msg%n%rEx</pattern>
    </encoder>
</appender>

<logger name="curl" level="DEBUG">
    <appender-ref ref="FILE"/>
</logger>