Site icon Vinsguru

JMeter – REST API Testing – A Complete Data-Driven Approach

In this article, I would like to show you a data-driven approach for REST API testing. If you are new to JMeter/REST API Testing, I would like to read this article first to get some idea.

Goal:

Our aim here is to come up with a framework to test REST API with different HTTP Methods. This test will be driven through a spreadsheet and the spreadsheet will contain all the input parameters, API URL, HTTP Method, request JSON, text response should be used for assertion etc.

By using one single HTTP Sampler, we would like to send different types of request GET / POST / PUT / PATCH / DELETE. Even the HTTP Request body data will be added dynamically at run time.

JSON Server:

I am going to use this JSON-Server for this testing purpose.  You can quickly set this up and running within a minute. You could also use your application APIs if you have.

Once it is installed, start the server using below command.
json-server --watch db.json

My db.json looks like this.

{
   "books":[

   ],
   "comments":[

   ],
   "profile":{
      "name":"typicode"
   }
}

Test Scenarios:

Lets assume, we would like to execute these scenarios as part of our REST API functional testing. Test description provides a high level idea about the test case.

If all requests are actually same and only the data is different as shown below, we could easily do a data-driven testing in JMeter with 1 HTTP Sampler & a CSV DataSet Config.

But in our case, We have 1 GET request, then 5 POST requests, then 1 GET request…etc. We send different types of request. So, we might end up creating our Test Plan as shown here

What will happen if we need to test thousands of scenarios!!? How can we maintain such a huge JMeter test?

Data-Driven Testing:

In order to completely drive the testing through a spreadsheet, Lets move any data which could vary among these HTTP requests to the spreadsheet as shown here after carefully analyzing these requests.

{
  "title": 	"${title}",
  "author": "${author}"
}
{
  "title": 	"${title}",
  "author": "${author}",
  "price": "$10.00"
}
{
  "title": "${title}"
}

As we have moved all the variables to the spreadsheet, now by having only one HTTP Sampler in JMeter test and by setting the HTTP Sampler properties/attributes at run time, we could run the entire test.

JMeter Test Plan:

 

 

The below statements change the current sampler HTTP Method.

def httpMethod = vars.get("http.method");
sampler.setMethod(httpMethod);

We use below statements to change the HTTP Body. We read the input.json file then we replace the any variable with the corresponding data

def dataToBePosted = new CompoundVariable(new File(vars.get("jmeter.test.home") + vars.get("input.json")).text).execute();
def arg= new HTTPArgument("", dataToBePosted, null, true);
arg.setAlwaysEncoded(false);
sampler.getArguments().addArgument(arg);

Download:

You can check the JMeter test plan here in GitHub.

Summary:

By moving all the variables to a spreadsheet and with 1 HTTP Sampler, we are able to test different types of requests. Adding any new tests to this test plan is very easy. As you already know, we have to insert one more row in the spreadsheet with the enough information for the new test. By looking at the spreadsheet, you can easily understand our coverage. Any update to the test data would also be easy with this approach instead of updating each and every individual HTTP Requests in JMeter.

 

Happy Testing & Subscribe 🙂

 

Share This:

Exit mobile version