Testing gRPC APIs with Postman

Avatar

Testing an API ensures that it consistently delivers the necessary functionality, performance, reliability, and security you need. It gives you the confidence to develop and ship faster, speeding up discoverability by API consumers. But it can be tedious, inefficient, and, more importantly, unreliable to manually test each time you add a new feature or fix a bug in your APIs. That’s what we’ve worked to solve for Postman’s gRPC support.

Postman announced first-class support for gRPC at the beginning of this year and followed it up with loads of improvements based on some truly fantastic feedback we received from our gRPC community on GitHub. And now, with the latest release, we’ve supercharged our gRPC support by adding the capability of writing test specifications for all your use cases. We hope these improvements will make your workflow even more efficient by allowing easier development and testing of your APIs.

New yet familiar

Postman strives to provide the most straightforward and powerful user interfaces so that you can focus on the actual task without thinking too hard about navigating new territory. We bring this philosophy to the new gRPC testing interface as well. If you have ever used Postman to debug and write tests for your REST APIs, guess what, those are the same concepts you need to know for writing tests for your gRPC requests in Postman. If you’re a newcomer, don’t worry about it, we’ve got you covered: check out the Postman Learning Center to get started.

To add a test, open your gRPC request and switch to the Scripts tab. You can use Postman’s robust Javascript-based scripting environment to author test suites. You can add two different scripts:

  • Before invoke: executes before invoking the method and establishing a connection with the server
  • After response: executes once the current transaction is complete (i.e., connection with the server is closed)

    Navigating to scripts in the gRPC request
    Navigating to scripts in the gRPC request

Once you’ve written the tests (more on that in a moment), you can observe the test assertions summary beside your response(s) under the Test results tab:

Invoking a gRPC request with tests
Invoking a gRPC request with tests

Get off the blocks in no time

Let’s start by writing our first test, so you can quickly see things in action:

  1. Go to the After response script of your gRPC request.
  2. From the list of Snippets on the right, choose Status code is 0.
  3. Click Invoke.

There you have it, your first test!

Both before invoke and after response scripts come with pre-curated lists of commonly used code snippets that get added to your scripts when selected, helping you start quickly.

Authoring after response script using a snippet
Authoring after response script using a snippet

Speed and accuracy superpower

Snippets are excellent for primary use cases, but as you can imagine, there might be more complex and custom scenarios that you want to test. For that, you can use the complete range of pm.* APIs to craft your scripts. Check out the below tests to get a taste of all the things that you can do:

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

pm.test('Correct user details are received', function () {
  pm.response.to.have.message({ id: "123", name: "John" });
});

You can even validate the messages against a given JSON schema, allowing you to enforce a number of custom rules:

const schema = { 
  type: "object", 
  properties: { 
    name: { 
      type: "string", 
      pattern: "^[a-zA-Z0-9_]*$", 
      maxLength: 64 
    } 
  }, 
  required: ['name'] 
}; 

pm.test('All response messages have valid name', function () { 
  pm.response.messages.to.have.jsonSchema(schema); 
});

As engineers, we understand that it might not always be efficient for you to refer to the documentation—that’s where the autocompletion for Javascript and the pm.* APIs in our script editor comes in, empowering you with all the information you need to author your scripts swiftly and accurately. Hopefully, this saves you some trips to Stack Overflow as well.

Autocompletion in gRPC scripts
Autocompletion in action

Leave nothing to chance

All the features and workflows mentioned above extend to unary, client streaming, server streaming, and bidirectional streaming gRPC methods alike. This makes it easier to have test specifications for all your gRPC APIs in a single place—Postman!

More to come

The ability to test your gRPC APIs is a powerful addition to the Postman API Platform, and it’s another way we’re working to help users throughout the API lifecycle. And gRPC support continues to evolve. Keep an eye out for more upcoming releases, including:

  • Saving examples for gRPC request
  • Automatic mock servers for all imported Protobuf APIs
  • Automating your gRPC testing workflows using Postman’s Collection Runner

Stay tuned to the Postman blog for more updates.

Watch and learn

Try Postman now

What do you think about this feature? Tell us in a comment below. You can also give product feedback through our Community forum and GitHub repository.

Comment

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.

8 thoughts on “Testing gRPC APIs with Postman

  • Avatar

    Is this supported to run via Newman? If not, is that on the timeline?

    • Avatar

      Hey John! Newman doesn’t support gRPC yet. You can follow the thread here for further updates on this.

  • Avatar

    any timelines on when gRPC can be used with collection runner?

    • Avatar

      We don’t have an exact timeline, but we have this on our upcoming roadmap, and you should hear some updates on this soon 🙂.

      Can you also raise a feature request for the same here with your use-case, if possible?

  • Avatar

    How can I export the grpc requests ‘collection’?

  • Avatar

    Can we run grpc api support collection runner using postman .

  • Avatar

    Is grpc support collection runner and execution vis jenkins

  • Avatar

    I like to use the runner and performance test option for a gRPC collection, but don’t see that option. Is the runner and performance function not available for gRPC API’s?