Write tests for gRPC requests using JavaScript assertions in Postman
You can write tests for your gRPC requests using scripts. Depending on your test logic and how you want to get the results, there are various ways to structure the test assertions in a test script. This page provides script examples for common gRPC testing scenarios in Postman.
The examples on this page use gRPC-specific properties of the pm.response object, such as pm.response.metadata, pm.response.trailers, and pm.response.messages. These properties are available only in scripts for gRPC requests.
For assertions that work with any request type, such as checking value types, array contents, or object properties, see Postman test script examples.
Test the status code
gRPC has its own set of status codes, where 0 indicates a successful response. You can use the statusCode property available over pm.response to test the status code of the response.
You can also assert the same using the pm.expect method.
You can use the pm.response.to.be.ok as shorthand to test if the gRPC status code is 0.
Test the response time
For a request with a unary method, you can assert the response time:
For requests with streaming methods, pm.response.responseTime denotes the total duration for that request execution.
Test metadata
To check if response metadata is present:
You can also assert the value of the metadata:
Similar assertions can be written for request metadata using the pm.request object.
Test response trailers
To check if a response trailer is present:
You can also assert the value of the trailer:
Test responses
For requests with multiple response messages, such as those using a bidirectional streaming method, the tests in this section check all messages for the given assertion. For requests with a single response message, like those using unary or client streaming methods, the assertion is tested only on that message.
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.
Test the existence of a message
To test the existence of a response message (strictly):
Test for a message with a specific property
You can assert that the given object’s properties are a subset of any messages received as a response:
By default, pm.response.messages.to.include() has .deep applied to it.
Test for a common property across all messages
To check if a common property exists in all the received messages:
You can also assert the value of a common property:
By default, pm.response.messages.to.have.property() has .deep and .nested applied to it.
Test messages against a JSON Schema
You can assert that the received messages match the given JSON Schema:
Work with a stream of messages
The examples below show how to work with a stream of messages and write assertions on them.