获取“CURL"来自每个 Rest Assured 测试的操作并在未通过测​​试时在控制台中打印 [英] Get the "CURL" operation from each Rest Assured test and print in the console when not pass the test

查看:40
本文介绍了获取“CURL"来自每个 Rest Assured 测试的操作并在未通过测​​试时在控制台中打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从每个 Rest Assured 测试中获取CURL"操作,并在未通过测​​试时在控制台中打印.有可能吗?

I need Get the "CURL" operation from each Rest Assured test and print in the console when not pass the test. It's possible?

例如,转换:

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

curl --user name:passwoed -k -X POST \
https://test_url/oauth/token \
-H 'cache-control: no-cache' \
-H 'content-type: application/x-www-form-urlencoded' \
-d grant_type=client_credentials

推荐答案

包括以下依赖项.

马文:

<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>

配置后,使用以下代码在控制台输出中记录 curl 请求.

Once configured use the following piece of code to log the curl request in the console output.

package com.api.test;

import static org.hamcrest.CoreMatchers.is;

import org.json.JSONObject;
import org.testng.annotations.Test;

import com.sample.model.EnrollEmail;
import com.github.dzieciou.testing.curl.CurlLoggingRestAssuredConfigFactory;

import io.restassured.RestAssured;
import io.restassured.config.RestAssuredConfig;
import io.restassured.http.ContentType;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;

/**
 * @author vamsiravi
 *
 */
public class Sample {

    @Test(enabled=true)
    public void sampleAPITest() {

        RestAssuredConfig config = CurlLoggingRestAssuredConfigFactory.createConfig();  

        EnrollEmail email = new EnrollEmail();
        email.setEmailAddress("sample@domain.com");
        RestAssured.given().config(config).contentType(ContentType.JSON).body(email).when().post("https://jsonplaceholder.typicode.com/posts").then()
        .assertThat()
        .statusCode(201);   

    }
}

输出:

[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

这篇关于获取“CURL"来自每个 Rest Assured 测试的操作并在未通过测​​试时在控制台中打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆