< Previous section: Prepare for launch
Now that we've tried out collection runs, variables, and test snippets, let's explore how to pass data between requests and change the order of execution.
- In the Postman app, click New+, open the Templates, and find "Intro to writing tests - with examples".
- Import the Intro to writing tests collection using the Run in Postman button.
- This collection has a variety of test types organized into folders. It's a great place to explore for future learning around tests.
Open the collection web documentation for future reference.
Test scripts let you pass data around in Postman, so you can chain tests together with shared data.
- Create an environment to use for your tests. In the top right click the eye button—the environment quick look. In the Environments section, click Add. Give your environment the name
workshop
, click Add, and close the Manage Environments modal. - Use the dropdown at the top right to select the
workshop
environment you just created. With this selected, any requests we run will reference variables in this environment. - In the collection
Integration tests
folder >Using variables
sub-folder, send the requestBitcoin exchange rate
and inspect the response. - Check out the JavaScript code under the request Tests tab—it demonstrates how to get and set variables from a script using the
.set
method. It's saving a var to the environment. Open the environment quick look—it should be in there now because we ran the request. - Open the request
Echo the exchange rate
. Notice that the URL references thebitcoinRate
variable set in the previous request tests. Send the request to see the response. The request used the value returned from the previous one.
Your scripts can alter the flow of request execution when you use the collection runner.
- Import another collection from API Network, search "learn" again and select the learning profile from before, this time importing "API Learner".
- Open the Runner and select the
API Learner
collection. Run the collection and notice the flow of execution. All four requests should run. - Open the first request
GET data
and Send it. The response includes a fieldmessage
and a text string value. - Select the Tests tab. Enter some JavaScript to test the length of the
message
(the conditional test should return true):
let jsonData = pm.response.json();
let responseField = jsonData.message;
//check the length of the text
if (responseField.length < 10) {
//note - setnextrequest only works when you use collection runner
postman.setNextRequest(null);
}
- Make sure you Save the request so that your code will execute when you run the collection.
- Open the Runner again and select the API Learner collection. Run the collection—it should run the GET request followed by the PUT request, skipping the requests in-between.
- Alter the code to stop execution after the first request by passing
null
to thesetNextRequest
method:
if (responseField.length > 10) {
postman.setNextRequest(null);
}
- Save the request. Open the Runner and select the API Learner collection again. Start the run—it should stop after one request.
- Optional: try changing the conditional test to return false and running the collection again (e.g. change > to <)—all requests should run.
You can pass data between requests even if they are not in the same collection. Let's try that using environments.
- Open the Intro to Writing Tests collection >
Integration Tests
>Bitcoin exchange rate
request again. In the Tests remember that it sets thebitcoinRate
environment variable. Make sure theworkshop
environment is selected. - Open the API Learner collection >
POST data
request. - In the Body of the request, add a new field, with the value referencing the environment variable:
{% highlight javascript%} {% raw %} { "name": "sue", "value": "{{bitcoinRate}}" } {% endraw %} {% endhighlight %}
- Send the request—you will see the variable value set by the request in the other collection was sent to this request via the environment.
Variable scopes determine where variable values are available within Postman, as well as which one takes precedence if you different vars with the same name.
In this section we tried out:
- used the collection runner
- chained requests
- altered execution order
- passed data between requests
- set variables from scripts
- accessed variables in the request body
Go to kahoot.it and enter the pin shown on the screen—answer the questions as quick as you can to win an Amazon gift card!
Check out the docs:
And more templates you can try out in Postman:
Next up: Liftoff