Write tests for gRPC requests using JavaScript assertions in Postman

You can write tests for your gRPC request using scripts. Depending on the logic and how you want to get the results, there are various ways in which the test assertions can be structured. This section will cover some of the most common ways to write assertions, along with an extensive list of examples explaining how to use pm.* APIs to write tests.

Testing status code

You can use the statusCode property available over pm.response to test the status code of the response.

pm.test('Status code is 0', () => {

You can also assert the same using the pm.expect method.

pm.test('Status code is 0', () => {

You can use the pm.response.to.be.ok as a shorthand to test if the status code is 0.

Testing response time

For a request with unary method, you can assert the response time:

pm.test('Response time is below 200ms', () => {

  // or

  // Using pm.expect

For requests with streaming methods, pm.response.responseTime denotes the total duration for that request execution.

Testing metadata

To check if a response metadata is present:

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

  // Using pm.expect

You can also assert the value of the metadata:

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

  // Using pm.expect

Similar assertions can be written for request metadata using the pm.request object.

Testing response trailers

To check if a response trailer is present:

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

  // Using pm.expect

You can also assert the value of the trailer:

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

Testing responses

In the case of multiple response messages (a request with the server or bidirectional streaming method), the tests in this section check all the messages for the given assertion. For a request with the unary or client streaming method where there is only one response message, the assertion is tested on that single message only.

Also, when writing assertions using pm.response.messages.to.*, you will be asserting on an array of message content and not the complete pm.response message object.

You can test the assertions in this section on request messages as well using the pm.request object.

Testing existence of a message

To test the existence of a response message (strictly):

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

Testing for a message with specific property

You can assert that the given object's properties are a subset of any messages received as a response:

pm.test('User details are updated successfully', () => {
    action: 'update-user-details',
    status: 'success'

By default, pm.response.messages.to.include() has .deep applied to it.

Testing for a common property across all messages

To check if a common property exists in all the received messages:

pm.test('All users have "company" in their profile', () => {

You can assert the value as well of the common property:

pm.test('All users are in same company', () => {
  pm.response.messages.to.have.property('company', 'XYZ');

By default, pm.response.messages.to.have.property() has .deep and .nested applied to it.

Testing messages against a JSON Schema

You can assert that the received messages match the given JSON Schema:

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

pm.test('All response messages have correct username', () => {

pm.test('Assert on a specific message', () => {

Working with a stream of messages

The examples below show how to work with a stream of messages and write assertions on them.

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 });

Last modified: 2022/09/15