You can write tests for your gRPC requests using scripts. Depending on the logic and how you want to get the results, there are various ways to structure the test assertions. This section covers some common ways to write assertions, along with a list of examples explaining how to use pm.* APIs to write tests.
You can use the statusCode
property available over pm.response to test the status code of the response.
pm.test('Status code is 0', () => {
pm.response.to.have.statusCode(0);
});
You can also assert the same using the pm.expect method.
pm.test('Status code is 0', () => {
pm.expect(pm.response.statusCode).to.equal(0);
});
You can use the
pm.response.to.be.ok
as shorthand to test if the status code is 0.
For requests with a unary method, you can assert the response time:
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);
});
For requests with streaming methods, pm.response.responseTime
denotes the total duration for the request's execution.
To check if response metadata is present:
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;
});
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
pm.expect(pm.response.metadata.get('content-type')).to.equal('application/grpc');
});
Similar assertions can be written for request metadata using the pm.request object.
To check if a response trailer is present:
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;
});
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
pm.expect(pm.response.trailers.get('grpc-status-details-bin')).to.equal('dummy-value');
});
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.
To test the existence of a response message (strictly):
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'
});
});
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', () => {
pm.response.messages.to.include({
action: 'update-user-details',
status: 'success'
});
});
By default,
pm.response.messages.to.include()
has.deep
applied to it.
To check if a common property exists in all the received messages:
pm.test('All users have "company" in their profile', () => {
pm.response.messages.to.have.property('isActive');
});
You can also assert the value of a 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.
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.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);
});
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: 2024/06/24