This is an introduction to testing and automation in Postman. Complete this entire course to equip yourself with necessary skills needed to test and automate APIs in Postman.
FORK the following collection to create a copy in your own workspace.
The collection will walk you through writing scripts to test your response data in Postman, passing data between requests using variables, validating responses against schema, as well as automating your testing using dynamic faker data and the collection runner, defining control flow, and running collections on the command line with Newman.
This collection uses a mock API with a few demo endpoints that return order data. We will use these endpoints to model a typical workflow so that you can go on to apply what you've learned when you're working with real-world APIs.
In the next section, we'll send our first request.
Open the first request, check out the docs on the right, and Send! The first request returns a JSON response structured like this:
{
"orderReference": "773a6237-d84f-45e6-bc30-67206345a8a4"
}
Open the Tests for this request. We are primarily going to be working in this tab for each request. The Tests script is where you write JavaScript to execute when your request response is received.
You can also write Pre-request Scripts to execute before a request is sent, and can add scripts to collections and foldersâthese will execute for every request contained inside.
The Tests tab contains some comments indicating the different tests and other processing we're going to add during the session. Let's handle the first oneâwe're going to need the response JSON data pulled into the script in a way that we can process, so save it as a variable:
const response = pm.response.json();
Let's write this out to the console to verify we have it (you can also use console.log
, console.warn
, and console.error
):
console.info(response);
Try sending the request and checking the console!
Before we move on let's save data from the response to a variable so that we can use it in another request. We'll use the response JSON variable we created in JS and store the orderReference
property as a global Postman variable (which is scoped to the workspace you're in):
pm.globals.set("orderRef", response.orderReference);
You can retrieve the global variables in your code using pm.globals.get
.
Send the request again and check the global variables via the little eye button at the top rightâthe orderRef
var should now have a value (and we can access it in other requests).
Now let's add a basic test to check we have a success status code of 200 OK
âthe test name string will be output with the test result, so make sure yours are meaningful enough to be useful when you're testing (you can either copy this or grab it from the snippets to the right of the Tests input):
pm.test("Status code is 200", () => {
//test syntax uses chai.js
pm.response.to.have.status(200);
});
With your test code added, Send and check out the Test Results. Then try making the test fail by changing the 200
to 400
ânotice that the result includes extra info indicating why the test failed.
Let's do a test that digs into a bit more detail nextâwe'll check that the response contains a particular property, and that it is a string. We can add both assertions to the same test and if any one fails the whole test will fail.
pm.test("orderReference exists", () => {
//property is in the response received
pm.expect(response).to.have.property("orderReference");
//property is a string
pm.expect(response.orderReference).to.be.a("string");
});
Send and check out the Test Results.
Save this request, then open the next request Get product code
, open its docs to the right, and Send it.
Send the request and read on here.
This request returns an array of productsâwe're going to script some processing on the array, filtering to find a particular item, and saving the sku
(a product code) data to the global variables.
The request you sent to the API received a JSON response that looked something like this (depending on the parameter you sent):
results": [
{
"name": "iPhone 12 Pro Blue",
"sku": "2020/Iph/12/Blu",
"color": "blue"
},
{
"name": "iPhone 12 Pro Red",
"sku": "2020/Iph/12/Red",
"color": "red"
},
{
"name": "Samsung Galaxy S",
"sku": "2020/Sam/GS/Blu",
"color": "gray"
},
{
"name": "Samsung Galaxy Note20 Ultra",
"sku": "2020/Sam/GN20/Red",
"color": "red"
},
{
"name": "Samsung Galaxy S20+",
"sku": "2020/Sam/SGS20P/Magenta",
"color": "red"
}
]
In the Tests tab you'll see comments again for each step. First get the response array in a variable and write the length out to the consoleâSend and check the console:
const phones = pm.response.json().results;
console.info("Phones returned: " + phones.length);
Let's filter the array to find a product with a particular property. We'll filter based on the color
property and just use the first valid result (feel free to also add a console statement to see what's in the variable):
const redPhone = phones.filter((phone) => phone.color === "red")[0];
Save the sku
property of the first red iPhone you can find to a global Postman variable.
You will be able to use the SKU in the request body of the 3. Send order
request instead of the hardcoded value.
Let's have a look at a basic structure of a test:
pm.test("Some test", () => {
pm.expect(1).to.eql(2);
});
Now that you have a single item from the response filtered, add a test to check that it is a JSON object, and that it contains property with a particular value (âred'):
pm.test("Phone found", () => {
pm.expect(redPhone.color).to.eql("red");
pm.expect(redPhone).to.be.an("object");
});
Send and check out the Test Results. Try making it fail too, e.g. by changing the color text value from red
to blue
.
Save this request. Open the next request 3. Send order
, check out the docs, and Send.
Send the request and read on here.
This request sends an object representing the new order to create, and returns an order confirmation. We are going to use dynamic data in the request body, carry out some preprocessing before the request runs, and test the response.
The request returns JSON with the following structure:
{
"created": true,
"orderId": {{$timestamp}}
}
Take a look in the request Body to see the JSON data we're sending to create an order. It should look something like this:
{
"orderRef": "f7032ebd-9ed2-4010-aab2-d7672f68e070",
"customer": "Acme Inc",
"sku": "2020/Iph/12/Blu",
"deliveryDate": "2021-01-15"
}
Remember that in your assignment for the last request, you saved an SKU to a variableânow you can set the Body sku
in this request to use the variable instead of the hardcoded value.
Tip: You can enter a variable reference between the quotes in the body just like you do in the Postman request UI fields.
Your next assignment is to parse the response body of this request.
Set a global variable with the value of the property orderId
from the response, so that we can reference it in another request.
Send the request and check the global variable values using the eye buttonâSave this request.
You will be able to use the variable in the next request 4. Get order
, as a path parameter instead of the hardcoded value.
When you send data to an API in Postman, you can generate values when the request runs using dynamic variables. Edit the value of the Body data customer
property to send a random company nameâinside the quotes, start typing {{$
to see the available dynamic variables. Choose randomCompanyName
âthe reference is exactly like any other variable but with the $
character at the start, like this: {{$randomThing}}
Send the request a few times, checking the Console to see what Postman sent each time (open the POST
request entry > Request Body to see the JSON).
For the deliveryDate
, we're going to calculate a date before the request sends, and set it to the var so that the request sends it. In Pre-request Script, add the following processing to calculate a date, setting it for two weeks from today, then saving it to a variable:
const deliveryDate = new Date();
deliveryDate.setDate(deliveryDate.getDate() + 14);
console.log(deliveryDate.toISOString().substr(0, 10));
pm.globals.set("deliveryDate", deliveryDate.toISOString().substr(0, 10));
Send the request, then check out the Request Body in the Console again, and take a look at the variable values via the eye button.
For this request we're expecting a 201 Created
status code. For your next assignment, add a test that verifies the status code of the response.
For your final assignment, test that the response includes confirmation of the order successâin the created
and orderId
properties (one should be true, and the other should be a number).
Send and check the Test Resultsâas always, make your test fail also! â ď¸
Save your request and open the next one 4. Get order
.
Send the request and read on here.
This request retrieves the order sent by the POST
request. This time we're going to test that the response validates against a schema, then automate our tests using the collection runner.
The request returns JSON with the following structure:
{
"orderId": {{orderId}},
"customer": "Acme Inc",
"sku": "2020/Iph/12/Red"
}
We're going to specify a schema to validate the response JSON against. The schema will be defined as a JSON object inside the script, and will match the response structure aboveâwe will write a script to check that the response has the same structure and properties.
â Remember that one of your assignments in the previous request was to save the order variable from the responseânow alter the value you're sending to the path parameter here to use the variable instead of the hard-coded value.
In Tests, create an object to represent the schema we expect the order data to match:
const schema = {
type: "object",
properties: {
orderId: {
type: "number",
},
customer: {
type: "string",
},
sku: {
type: "string",
},
},
required: ["orderId", "customer", "sku"],
};
Now add code to check the response against the schema (we use expect
again but this time with jsonSchema
):
const response = pm.response.json();
pm.test("Schema is valid", () => {
pm.expect(response).to.have.jsonSchema(schema);
});
Send the request and check the Test Resultsâremember to also make sure it fails e.g. if you change one of the schema
type
values.
Finally let's check what happens if no order ID is passed to the request. Click the eye button and edit, then delete the value of the orderId
variable so that it's empty, and Send again before reading on.
Since you didn't pass an order ID, you got a 404
response containing an error message.
The request returns the following Body structure when no order is specified:
{
"message": "Not found!"
}
We already specified a schema to test successful responses against, but now let's test the error response against a different schema.
In Tests, create an object to represent the schema we expect the order data to match:
const errorSchema = {
properties: {
message: {
type: "string",
},
},
};
if (pm.response.code === 404)
pm.test("Error response is valid", () => {
pm.expect(response).to.have.jsonSchema(errorSchema);
});
Send the request and check out the Test Resultsâand you know the drill by now, edit your test code to make sure it fails!
This isn't the most efficient test code we could use because we've just tacked on the error schema test at the endâyou could restructure the code in a more sensible way, but for now add a conditional before the test on the successful schema:
if(pm.response.code===200)
We've carried out processing on individual requests and saved data so that we can pass values between requestsâbut we can do much more to automate our testing.
When you use the Postman Collection Runner, you can run the requests in a sequence and add logic to your scripts to control the flow of execution.
In the Tests for the 4. Get order
request, add this code to end execution after this request, which will mean that the runner stops here.
postman.setNextRequest(null);
You can create loops and conditional workflows by passing the request name as a string to the setNextRequest
method.
Save the request, then open the collection overview by selecting it on the leftâhit Run. Run the collection with the default options to see the requests execute in sequence.
Take a look at the runner output and remember how the requests are saving response data that subsequent requests useâthis way we can pass data between requests. Click the requests in the runner output display to drill down into detail about what was sent.
You can set collection runs up to happen on a schedule using Monitors. Open Monitors on the left, and create a new one. Give your monitor a name, select the collection, and choose a frequency. You will receive automated updates on any fails in your monitoring runs and can also access them inside Postman. Note that it may take some time for results to appear.
Open the final request "Complete training" and check out the docs for instructions!
We've been able to cover the following:
If you've completed all the steps, you should have a Postman Collection consisting of requests, variables, and tests in your workspace.
If you want to learn more about testing and automation in Postman, check out the following resources. The sky is the limit!