You can add tests to a gRPC request before invoke, when the client receives a message, or after response. All hooks are available for all gRPC requests regardless of the method type (unary, client streaming, server streaming, or bidirectional streaming).
When you select Invoke, tests run before, during, or after the request is invoked. Once you invoke your gRPC request, updates to scripts in the On message tab won't take effect until the next time you invoke the request. If you select Cancel, the request and any further script runs are stopped.
Validate responsesTo validate the data returned by a request, you can use the pm.response
object in a test. Define tests using the pm.test
function. Provide a name and function that returns a boolean (true
or false
) value to indicate if the test passed or failed. Use Chai.js BDD syntax and pm.expect
in your assertions to test the response detail.
The first parameter for the .test
function is a text string that appears in the test result output. Use this to identify your tests and communicate the purpose of a test to anyone viewing the results.
For example, enter the following in the Post-response tab of a request to test if the response status code is 200
:
pm.test("Status test", function () {
pm.response.to.have.status(200);
});
Select Send to run your request, then select the Test Results tab in the response. The tab header displays how many tests passed and how many ran in total. You can also select the Filter Results dropdown list to view the test results by type: Passed, Skipped, and Failed.
If the request returned a 200
status code, the test passes. To find out what happens with a different status code, change the expected status code in your post-response script and run the request again.
Using the pm.expect
syntax gives your test result messages a different format. Experiment with the options to get the output you find most useful.
Fork the Intro to writing tests collection to import templates containing some example post-response scripts into Postman and experiment with the code.
Your code can test the request environment, as in the following example:
pm.test("environment to be production", function () {
pm.expect(pm.environment.get("env")).to.equal("production");
});
You can use different syntax variations to write your tests in a way that you find readable, and that suits your application and testing logic. For example:
pm.test("response should be okay to process", function () {
pm.response.to.not.be.error;
pm.response.to.have.jsonBody("");
pm.response.to.not.have.jsonBody("error");
});
Your tests can validate request responses using syntax that you tailor to the response data format. For example:
pm.test("response must be valid and have a body", function () {
pm.response.to.be.ok;
pm.response.to.be.withBody;
pm.response.to.be.json;
});
Test collections and folders
You can add post-response scripts to a collection, a folder, or a single request within a collection. A post-response script associated with a collection will run after every request in the collection. A post-response script associated with a folder will run after every direct child request in the folder. This enables you to reuse commonly run tests after requests. The run order for each request will be collection tests, folder tests and then request tests.
You can also store post-response scripts in the Package Library. This enables you to maintain commonly used scripts and tests in a single location, share them with your team, and reuse them in your internal workspaces.
Adding scripts to collections and folders enables you to test the workflows in your API project. This helps to ensure that your requests cover typical scenarios, providing a reliable experience for application users.
To add or edit collection and folder tests, select a collection or folder in the sidebar, then select the Scripts > Post-response tab.
When you run a collection, the collection runner displays the results for all tests. The test results include the response time in milliseconds and details about whether a specific request in the collection passed or failed its tests.
Write tests using PostbotYou can write scripts to control the order in which your requests run using branching and looping.
You can use Postman's AI assistant Postbot to write tests for your requests. Tell Postbot what to do in natural language, and Postbot generates post-response scripts for you. Use Postbot to add a new set of tests, visualize responses, save a field from a response, or fix your existing tests.
To write tests with Postbot, do the following:
Open a request and select Send so it has a response.
Select the Scripts > Post response tab.
(Optional) Select code you'd like Postbot to help with.
Select Open Postbot in the code editor. You can also select Control+Option+P or Ctrl+Alt+P to open Postbot.
Tell Postbot what you want to do and select Enter.
You can also work with Postbot in the following ways:
Postbot can offer suggestions based on your request and the response data. Select the down arrow (â) on your keyboard and select one of Postbot's suggestions.
Postbot understands context. Select part of a script or the response body, then tell Postbot what you want to do or change based on the selection.
You can also select part of a JSON response, right-click, then select Use Postbot. You can tell Postman to generate tests based on the selection. For example, go to a response, select a property, and ask Postbot to generate a test to verify that it exists. Or select specific values in a response and ask Postbot to visualize only that data.
Postbot also powers autocomplete in the post-response script editor. If youâve enabled Postbot, youâll see code suggestions as you type. Select the Tab key to accept the suggestion. If youâve already paid for Postbot, you get unlimited suggestions and acceptances. For everyone else, feature usage is capped at 200 suggestion acceptance events per month.
Add documentation to post-response scriptsTo learn more about what you can do with Postbot, go to About Postbot.
Postman supports JSDoc for documenting JavaScript functions in your post-response scripts. Documentation added to your functions using JSDoc will display in a popup window when you call your functions. You can use the official JSDoc documentation to learn how to add documentation to your post-response scripts.
The following example has documentation for the logger
function using JSDoc. The documentation explains what the function does, and defines what the data
parameter is used for and that it accepts a string data type.
function logger (data) {
console.log(`Logging information to the console, ${data}`)
}
Debug your tests
If you're having trouble with your tests, do the following:
After writing your first tests in Postman, you can write more complex tests and use them with other Postman tools.
pm
object, check out some post-response script examples and visit the Postman JavaScript reference.RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4