如何在超测失败时打印请求日志(如请求 url、请求正文、请求 queryParam)?

Posted

技术标签:

【中文标题】如何在超测失败时打印请求日志(如请求 url、请求正文、请求 queryParam)?【英文标题】:How to print the request logs (like request url,request body, request queryParam) on supertest failure? 【发布时间】:2021-08-14 22:53:33 【问题描述】:

我想在 Supertest 预期失败的失败详细信息下方打印或控制台日志 对于以下成功请求,无需在成功时打印任何内容

const result = await request(dummy_url).get("repos/Microsoft/TypeScript/pulls")
      .set("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:87.0) Gecko/20100101 Firefox/87.0")
      .expect(200)
      .then(response => 
        console.log("success");
        console.log(response);
        return response;
      ).catch(error => 
        console.log("error");
        // console.log(error);
        return 2;
      )
    console.log(result)
    done();

如果失败,假设我将 url 修改为 dummy 而不是 pull, 我需要知道请求 url、path parma 和 request body(如果有的话),

目前尝试以上述方式添加它只会出现以下错误

Error: expected 200 "OK", got 404 "Not Found"
        at Object.<anonymous> (/Users/thoughtworks/projects/api-test-supertest-jest-typescript/__tests__/github-routes/jest.test.ts:40:8)
        at /Users/thoughtworks/projects/api-test-supertest-jest-typescript/node_modules/jest-jasmine2/build/queueRunner.js:45:12
        at new Promise (<anonymous>)
        at mapper (/Users/thoughtworks/projects/api-test-supertest-jest-typescript/node_modules/jest-jasmine2/build/queueRunner.js:28:19)
        at /Users/thoughtworks/projects/api-test-supertest-jest-typescript/node_modules/jest-jasmine2/build/queueRunner.js:75:41
        at processTicksAndRejections (node:internal/process/task_queues:96:5)
    ----
        at Test.Object.<anonymous>.Test._assertStatus (/Users/thoughtworks/projects/api-test-supertest-jest-typescript/node_modules/supertest/lib/test.js:296:12)
        at /Users/thoughtworks/projects/api-test-supertest-jest-typescript/node_modules/supertest/lib/test.js:80:15
        at Test.Object.<anonymous>.Test._assertFunction (/Users/thoughtworks/projects/api-test-supertest-jest-typescript/node_modules/supertest/lib/test.js:311:11)
        at Test.Object.<anonymous>.Test.assert (/Users/thoughtworks/projects/api-test-supertest-jest-typescript/node_modules/supertest/lib/test.js:201:21)
        at localAssert (/Users/thoughtworks/projects/api-test-supertest-jest-typescript/node_modules/supertest/lib/test.js:159:12)
        at fn (/Users/thoughtworks/projects/api-test-supertest-jest-typescript/node_modules/supertest/lib/test.js:156:5)
        at Test.callback (/Users/thoughtworks/projects/api-test-supertest-jest-typescript/node_modules/superagent/src/node/index.js:902:3)
        at fn (/Users/thoughtworks/projects/api-test-supertest-jest-typescript/node_modules/superagent/src/node/index.js:1130:18)
        at IncomingMessage.<anonymous> (/Users/thoughtworks/projects/api-test-supertest-jest-typescript/node_modules/superagent/src/node/parsers/json.js:19:7)
        at Stream.emit (node:events:365:28)

注意事项, 我在 Typescript 中使用它,在 async 函数中,认为这不是一个阻塞器。

【问题讨论】:

【参考方案1】:

经过多次尝试和尝试,我想出了一个记录请求详细信息的函数,我将 supertest 的响应与预期的状态代码一起传递。 失败时,该函数将记录请求和响应的详细信息,如路径参数、查询参数、请求正文

import SuperTest from "supertest";
export const checkStatusCode = (res: any, expectedStatus: any = 200): SuperTest.Response => 
  if (res.status === expectedStatus) 
    return res
  ;
  const error = res.error;
  const reqData = JSON.parse(JSON.stringify(res)).req;
  throw new Error(` 
  request-method  : $JSON.stringify(reqData.method) 
  request-url     : $JSON.stringify(reqData.url)
  request-data    : $JSON.stringify(reqData.data)
  request-headers : $JSON.stringify(reqData.headers)
  reponse-status  : $JSON.stringify(res.status)
  reponse-body    : $JSON.stringify(res.body)
  `
  );
;

在 jest 测试文件中的使用


describe("Jest - Api - user", () => 
  it("Verify POST ", async () => 
    const res = await request(url.someurl)
      .post("/dummy")
      .set("Authorization", authToken)
      .send(updateThirdParty)
    checkStatusCode(res, 200)
  )
)

该解决方案的灵感来自supertest github issue 中的建议之一。 感谢sakovias。 注意:这是记录数据的函数,我们仍然可以将其作为期望本身的包装器,我将作为单独的线程发布。

【讨论】:

以上是关于如何在超测失败时打印请求日志(如请求 url、请求正文、请求 queryParam)?的主要内容,如果未能解决你的问题,请参考以下文章

Jmeter-仅在请求或断言失败时打印日志

有没有办法在超测中设置默认值?

在 Jest 中测试失败时如何打印请求和响应?

如何分析IIS7的失败请求跟踪日志

关于后台log中奇怪的url请求

如何根据失败百分比在日志字段中创建警报?