How to write automated tests for APIs with Postman – Part 2

In the last tutorial we discussed how to set up Jetpacks and write a basic test for a sample API endpoint. The test checked the response status code and the Content-Type header against known values.

In this tutorial, we'll write a more comprehensive test. We'll use the super-useful JSONBlob API. This API lets you create, update, and delete content on the server. We'll use it to mimic a blog. This mirrors common API usage scenarios and will be helpful in demonstrating how Postman can make things easier for you.

Do remember that the API is only for demo purposes and does not follow the best security practices.

1. Create an environment

In this tutorial, we'll use environment variables. Create an environment with the following values:

url: https://jsonblob.com
Screen Shot 2016-06-27 at 16.46.44

Select the environment in the environment list to activate it. We'll use test scripts to fill this environment with more values.

2. POST request to create a new blog post

To create a new blog post, we'll need to send a POST request to {{url}}/api/jsonBlob with the following request body:

Screen Shot 2016-06-27 at 18.50.07

This endpoint will return a "Location" header, with the URL of the newly created blog post:
Screen Shot 2016-06-27 at 16.49.24

Let's use the test script feature to extract this header and store it:
Screen Shot 2016-06-27 at 16.49.31

Save this request to a collection.

3. Validate that the post was created
We can now make a GET call to the saved URL to make sure the blog post was created. Set the URL to {{blogLink}}, and add a test script to validate the contents.
Screen Shot 2016-06-27 at 17.39.57

4. Update the blog post

We can update existing blog posts using the PUT method. Set the URL to {{blogLink}}, since we know the blog post we want to update. Let's update the blog content to:

Screen Shot 2016-06-27 at 18.20.31

5. Verify that the contents have been updated

We can use a modified version of the GET request we created in step 3. All we need to do is change the test script to:

Screen Shot 2016-06-27 at 17.39.53

6. Delete the blog post

Change the GET to DELETE to remove the blog post from the server.
Screen Shot 2016-06-27 at 16.57.27

7. Verify that the blog post has been deleted

Now that we've deleted the post, let's verify that the blog URL no longer returns any content. Use the same URL as the previous GET requests, and change the test script to

A 404 response code indicates that the entity we requested was not found.
Screen Shot 2016-06-27 at 16.57.30

Running the request says that everything went well. Postman would notify you of errors if something did not go as per expectations. To check for Javascript errors in your code, you can open the Chrome Console and monitor the logs.

You can download this collection here.

In the next tutorial, we'll look at the collection runner and how we can run this entire collection in one click using the Collection Runner.

The "How to write automated test with Postman" series

  1. Part 1
  2. Part 2 (this article)
  3. Part 3

Update: See how to write tests using the newer PM API (known as the pm.*API).