Postman gRPC 測試示例

2023-04-04 09:32 更新

您可以使用腳本為 gRPC 請求編寫測試。根據(jù)邏輯和您希望如何獲得結果,可以通過多種方式構建測試斷言。本節(jié)將介紹一些最常見的斷言編寫方法,以及解釋如何使用pm.* API編寫測試的大量示例。

測試狀態(tài)碼

您可以使用pm.responsestatusCode上可用的屬性來測試響應的狀態(tài)代碼。

pm.test('Status code is 0', () => {
  pm.response.to.have.statusCode(0);
});

您還可以使用pm.expect方法斷言相同的內(nèi)容。

pm.test('Status code is 0', () => {
  pm.expect(pm.response.statusCode).to.equal(0);
});
您可以使用pm.response.to.be.ok作為簡寫來測試狀態(tài)代碼是否為 0。

測試響應時間

對于具有一元方法的請求,您可以斷言響應時間:

pm.test('Response time is below 200ms', () => {
  pm.response.to.have.responseTime.below(200);

  // or
  pm.response.to.have.responseTime.not.above(200);

  // Using pm.expect
  pm.expect(pm.response.responseTime).to.be.below(300);
});

對于使用流方法的請求,pm.response.responseTime表示該請求執(zhí)行的總持續(xù)時間。

測試元數(shù)據(jù)

檢查是否存在響應元數(shù)據(jù):

pm.test('"content-type" is present in response metadata', () => {
  pm.response.to.have.metadata('content-type');

  // Using pm.expect
  pm.expect(pm.response.metadata.has('content-type')).to.be.true;
});

您還可以斷言元數(shù)據(jù)的值:

pm.test('"content-type" response metadata is "application/grpc"', () => {
  pm.response.to.have.metadata('content-type', 'application/grpc');

  // Using pm.expect
  pm.expect(pm.response.metadata.get('content-type')).to.equal('application/grpc');
});

可以使用pm.request對象為請求元數(shù)據(jù)編寫類似的斷言。

測試響應預告片

檢查是否存在響應預告片:

pm.test('"grpc-status-details-bin" is present in response trailers', () => {
  pm.response.to.have.trailer('grpc-status-details-bin');

  // Using pm.expect
  pm.expect(pm.response.trailers.has('grpc-status-details-bin')).to.be.true;
});

您還可以斷言預告片的價值:

pm.test('"grpc-status-details-bin" response trailer is "dummy-value"', () => {
  pm.response.to.have.trailer('grpc-status-details-bin', 'dummy-value');

  // Using pm.expect
  pm.expect(pm.response.trailers.get('grpc-status-details-bin')).to.equal('dummy-value');
});

測試反應

在多響應消息的情況下(服務器請求或雙向流方法),本節(jié)中的測試檢查給定斷言的所有消息。對于只有一個響應消息的一元或客戶端流方法的請求,斷言僅在該單個消息上進行測試。

此外,在使用 編寫斷言時pm.response.messages.to.*,您將對一組消息內(nèi)容而不是完整的pm.response消息對象進行斷言。

您可以使用對象測試本節(jié)中關于請求消息的斷言pm.request。

測試消息的存在

要測試響應消息的存在(嚴格):

pm.test('Correct user details are received', () => {
  pm.response.to.have.message({
    userId: '123',
    name: 'John Doe',
    email: 'john@example.com',
    phone: '+1-555-555-5555',
    age: 30,
    company: 'XYZ'
  });
});

測試具有特定屬性的消息

您可以斷言給定對象的屬性是作為響應收到的任何消息的子集:

pm.test('User details are updated successfully', () => {
  pm.response.messages.to.include({
    action: 'update-user-details',
    status: 'success'
  });
});
默認情況下,pm.response.messages.to.include()已.deep應用于它。

測試所有消息的公共屬性

檢查所有接收到的消息中是否存在公共屬性:

pm.test('All users have "company" in their profile', () => {
  pm.response.messages.to.have.property('isActive');
});

您也可以斷言公共屬性的值:

pm.test('All users are in same company', () => {
  pm.response.messages.to.have.property('company', 'XYZ');
});
默認情況下,pm.response.messages.to.have.property()已.deep應用于.nested它。

針對 JSON 模式測試消息

您可以斷言接收到的消息與給定的 JSON 模式匹配:

const schema = {
  type: "object",
  properties: {
    username: {
      type: "string",
      pattern: "^[a-z0-9_-]{3,16}$"
    }
  }
};

pm.test('All response messages have correct username', () => {
  pm.response.messages.to.have.jsonSchema(schema);
});

pm.test('Assert on a specific message', () => {
  pm.expect(pm.response.messages.idx(10).data).to.have.jsonSchema(schema);
});

使用消息流

下面的示例展示了如何處理消息流并在其上編寫斷言。

pm.test('Should receive keep-alive message roughly every 5 seconds', () => {
  const keepAliveMessage = pm.response.messages.filter({
    data: {
      type: 'keep-alive'
    }
  });

  for (let i = 1; i < keepAliveMessage.length; i++) {
    const time1 = keepAliveMessage[i-1].timestamp;
    const time2 = keepAliveMessage[i].timestamp;

    pm.expect(time2-time1).to.be.within(4800, 5200);
  }
});
pm.test('Every request message should have a corresponding response message', () => {
  pm.request.messages.each((reqMsg) => {
    pm.response.messages.to.include({ id: reqMsg.data.id });
  });
});


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號