This article was written by Appurva Murawat.
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
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
Get off the blocks in no time
Let’s start by writing our first test, so you can quickly see things in action:
Go to the After response script of your gRPC request.
From the list of Snippets ** on the right, choose **Status code is 0.
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
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 recevied', 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.
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
The post Testing gRPC APIs with Postman was written by Appurva Murawat and appeared first on Postman Blog.
Top comments (1)
[[..PingBack..]]
This article is curated as a part of #68th Issue of Software Testing Notes Newsletter.