【问题标题】:How to store logs generated by curl , using the library CurlRestAssuredConfigFactory如何使用库 CurlRestAssuredConfigFactory 存储 curl 生成的日志
【发布时间】:2021-09-28 22:22:42
【问题描述】:

我正在使用库 CurlRestAssuredConfigFactory ,并使用以下代码

   private final Options options = Options.builder().useLogLevel(Level.INFO).build();
   private final RestAssuredConfig config = CurlRestAssuredConfigFactory.createConfig(options);

RequestSpecification spec = getRequestSpecification(RestAssured.given());
    return spec.config(config).when().get(this.url).then().extract().response();

执行最后一段代码后,会生成带有完整 URI 和标头的 curl 日志,我希望存储这些日志并将它们传递到报告中。 请帮忙

【问题讨论】:

    标签: java api curl rest-assured


    【解决方案1】:

    如果您使用 Allure 进行报告,我建议您使用 allure-rest-assured 库来存储和查看直接附加到报告中相应测试的日志。

    https://mvnrepository.com/artifact/io.qameta.allure/allure-rest-assured/2.13.2

    只需将此依赖项添加到您的项目中,然后在 ReqestSpecification 的 1 行中用作过滤器:

      private static RequestSpecification getReqSpec() {
        return new RequestSpecBuilder()
          .addFilter(new AllureRestAssured())
          .setBaseUri(BASE_URI)
          .addHeader("Authorization", "Bearer " + getAuthToken())
          .setAccept(ContentType.JSON)
          .setContentType(ContentType.JSON)
          .build();
      }
    

    然后:

    • 运行您的测试
    • 生成并打开 Allure 报告
    • 查看所需测试的附件

    【讨论】:

    • 不,他要保存 cUrl 日志,而不是请求或响应日志。
    • 嗨,非常感谢,但我从 git repo 本身的自述文件中得到了答案 github.com/dzieciou/curl-logger#custom-curl-handling final List curls = new ArrayList(); CurlHandler handler = new CurlHandler() { @Override public void handle(String curl, Options options) { curls.add(curl); } }; List handlers = Arrays.asList(handler); CurlRestAssuredConfigFactory.createConfig(handlers)
    【解决方案2】:
    https://github.com/dzieciou/curl-logger#custom-curl-handling
    
    final List<String> curls = new ArrayList<>();
    CurlHandler handler = new CurlHandler() {
      @Override
      public void handle(String curl, Options options) {
        curls.add(curl);
      }
    };
    List<CurlHandler> handlers = Arrays.asList(handler);
    CurlRestAssuredConfigFactory.createConfig(handlers)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 2020-07-30
      • 2019-02-07
      • 1970-01-01
      • 1970-01-01
      • 2018-11-28
      相关资源
      最近更新 更多