logo logo

Using Test Scripts in Postman

Using Test Scripts in Postman

Good tests often perform several checks against an application. So far, we have seen how to make calls to different API endpoints with Postman. This is great if you are doing exploratory testing and want to manually verify the results, but if you are setting up test automation you will need some automated checks present that can verify if you are getting the correct results or not. Let’s take a look at the Tests tab and see what checks we can add in Postman.

Postman Tutorial Chapters

  1. Overview – The Ultimate Postman Tutorial for API Testing
  2. Chapter 1Getting started with Postman for API Testing
  3. Chapter 2 – Understanding API Authorization Options in Postman
  4. Chapter 3 – Using Postman for Automation Testing
  5. You’re here → Chapter 4 – Using Test Scripts in Postman
  6. Chapter 5 – Running Postman in CI using Newman
  7. Chapter 6 – Creating Mocks with Postman

Postman Test Scripts

If you go to the jsonplaceholder request that you made in previous parts of this tutorial, you can see that there is a tab to click on called Tests.

Postman Tutorial Chapter 4 - Postman Test Scripts

Once you are there, you can add test to the request. Test scripts in Postman are written in JavaScript, but if you don’t know any JavaScript, don’t despair, Postman has some built in code snippets to get you started. On the right hand side you will see a bunch of different snippets to choose from. Scroll down a little and you will see one called Status code: Code is 200. This is a good check to start with on an API call. Successful API calls should return a 200 code, and so as long as we are sending a call that we expect to succeed, we should expect it to have a 200 return code. Click on the link for that snippet and you will see that it has added a test that looks like this:

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

Now, if you send off this request it will send the request as it did before, but once the response comes back it will check if the response code was 200. After sending the request you can see the test results, if you scroll down, under the Test Results tab.

Postman Tutorial Chapter 4 - Postman Test Results

Go ahead and change the expected status in the test to 400 instead of 200 and send the request again. Now under the test results you will see a failure telling you that it expected to have a 400 code but got a 200 instead. Now that you know what the passed and failed results will look like in Postman, you can change the test back to properly check for a 200, so it passes in the future.

There are a number of other snippets that can help you get started. For example, you could use the Response body: JSON value check to check for a certain value being in your the JSON in the response body. When you click on this snippet it gives you a template, but you will need to modify it to test the particular response you are looking at. In our case, let’s verify the userId in our response. So the starting code for the snippet looks like this:

pm.test("Your test name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.value).to.eql(100);
});

We need to modify the expect to look for things that are in the jsonplace holder response. So instead of checking for jsonData.value we will check for jsonData.userId and instead of expected the value to equal 100 we will expect it to equal 1 since that is the user ID of the person who wrote the post we are requesting data for. We will also want to rename the test to something meaningful for what we are looking at. If we put all that together we end up with this.

pm.test("UserId is 1", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.userId).to.eql(1);
});

Sending the request will now result in this check being run as well. There are many other snippets available to explore and you can also create your own as well.

Pre-request Scripts

Tests are run after the request has been sent, but sometimes you need to create some data or variables before you run a test. In that case, you can use the pre-request scripts tab. These scripts are also written in JavaScript but, as the name implies, they are executed before the request is sent. There are a number of reasons you might want to do something like this, but let’s take a look at a specific example.

In the current test that we are looking at, we are checking that the userId is 1, but in this API only posts 1 – 10 have a userId of 1 and the posts 11-20 have a userId of 2 and so on. How can we make sure our posts line up with the proper users when we are testing? Well, we could use a pre-request script to check what post_id we are going to use and then set the expected userId to the correct value based on that.

Using the Get a variable snippet we can get the post_id variable and then we can check its value and using the Set an environment variable snippet we can set the value of the userId environment variable to the correct value. This would result in code that looks like this:

var post_id_string = pm.variables.get("post_id");
var post_id_int = parseInt(post_id_string)
if (post_id_int <= 10){
    pm.environment.set("userId", 1);
}
if (10 < post_id_int && post_id_int <= 20){
    pm.environment.set("userId", 2);
}
if (20 < post_id_int && post_id_int <= 30){
    pm.environment.set("userId", 3);
}
if (30 < post_id_int && post_id_int <= 40){
    pm.environment.set("userId", 5);
}
//...and so on until 100

Since variables are stored as strings, we need to use parseInt() to convert the post_id into a number and then we can check the range it is in an set the userId variable in the environment to the correct value.

Now we just have to go to the test and tell it to use the userId variable that we are setting in the pre-request script. We can get that variable out of the environment and then just change the check to use that variable instead of just having it hard coded to one.

pm.test("Check UserId", function () {
    var jsonData = pm.response.json();
    var test_val = pm.environment.get("userId");
    pm.expect(jsonData.userId).to.eql(test_val);
});

Now you can run the test with different values of post_id (try values like 20 or 35) and it will check that the userId is correct.

Ready for Automating Requests

Now that you know how to setup tests for the calls in Postman and now that you know how to use variables and scripts to keep your tests tidy, you are ready to automate the execution of the tests. Using scripts like this can be a bit overwhelming if you haven’t worked with JavaScript much before, but as you can see they can add a lot of power and flexibility to your tests as well. This kind of power and flexibility is important when running automated tests and in the next chapter of this tutorial we will look at how you run these tests in CI systems using Newman.

💫 TAKE ME TO CHAPTER #5 💫

About the author

Dave Westerveld

Dave Westerveld is an experienced tester who has been involved in various aspects of the testing role. As a strong exploratory tester, he has learned how to leverage many different tools to enhance his testing powers. He has also been involved in many automation projects including building out new automation frameworks. In addition he has helped transition several large and expensive automation suites into lighter weight, higher value systems. A speaker at several conferences, Dave also blogs about his thoughts and experiences at offbeattesting.com

Leave a Reply

FacebookLinkedInTwitterEmail