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:


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:

    "content": "My first blog post :)"

This endpoint will return a “Location” header, with the URL of the newly created blog post:

Let’s use the test script feature to extract this header and store it:

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.

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:

  "content": "My first blog post, updated :)"

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:

tests["Has correct updated text"] = 
  responseBody.has("My first blog post, updated :)");

6. Delete the blog post

Change the GET to DELETE to remove the blog post from the server.

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

tests["Status code is 404"] = 
    responseCode.code === 404;

A 404 response code indicates that the entity we requested was not found.

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).