- In the Postman app, click the New+ button, tab over to Templates, and find "Intro to writing tests - with examples".
- Import the Intro to writing tests collection using the Run in Postman button.
- Observe various aspects of the collection, including the web documentation.
- Create a new environment (to store our data), and then use the dropdown to select this new environment.
- Under the Integration tests folder and Using variables sub-folder, send the request
Bitcoin exchange rate
and inspect the response. - Review the JavaScript code under the Tests tab, and observe how to get and set variables in script areas.
- Send the request
Echo the exchange rate
, and observe how to get variables in text areas.
- Run the folder Using variables in the collection runner, and observe the execution order.
- Write JavaScript code under the Tests tab to establish conditional logic to terminate a collection run.
- Run the folder again, and observe the execution order.
let jsonData = pm.response.json();
let bitcoinRate = jsonData.bpi.USD.rate_float;
if (bitcoinRate < 8000) {
// proceed to the next request
} else {
// stop right there
postman.setNextRequest(null);
}
- In the ShipEngine Walkthrough collection, plan what data to extract from the response body (to pass to a subsequent request).
- Create an environment, and select it as your active environment.
- Under the Tests tab of your request, write the code to parse the response body and set an environment variable.
let jsonData = pm.response.json();
pm.environment.set("nameOfVariable", valueOfVariable);
- Duplicate the request
Echo the exchange rate
from the Intro to writing tests collection, rename the request, and drag the request into the ShipEngine Walkthrough collection. - Update the data to be sent in the request
Echo
, and review the syntax of getting and setting variables in text vs. script areas. - Using the collection runner, run the ShipEngine Walkthrough collection with your newly added request.
- Writing advanced scripts and tests
- Running a collection with the collection runner
- Using lots and lots of variables
- Intro to writing tests - with examples template
- Extract data to chain requests template
- Postman sandbox docs
- Working with data files: ramen template
- Dynamic variables in mock servers template
Continue to Part 3: Liftoff