• info@helpingtesters.com
  • helpingtesters

API Testing using JMeter

April 12, 2017 JMeter Tutorial
api testing tools, jmeter load testing, Jmeter, API, API test, api testing

Before moving to API Testing with JMeter let us have a brief introduction of API. API stands for application programming interface. Basically, it is something which allows one piece of software to interact with other software. To speak plainly, it is a messenger that takes a request from one system to another system, tell it what it wants and get the response back to the requesting system.

  • Think of API as a waiter in a restaurant.
  • Imagine yourself sitting in that restaurant with a menu in your hand.
  • Consider Kitchen as a system which will serve the order of your menu. But there needs to be middleman which will serve your menu from kitchen to your table.
  • The waiter serves as the middleman or rather say, messenger which serves the ordered food from kitchen to your table.
  • In the same way, API acts as a messenger which takes the request from system A, go to system B, tell system B what system A wants, take the expected response of the request and carries it back to system A.

Let us take a real API example. There are many websites available for booking flight ticket, which does not belong to particular Flight Company, but even then these websites are able to interact with particular flight database. Now, it is quite important to find out how it happens. It is possible through APIs. These Flight companies provide APIs to many websites for booking a ticket and for getting the response of the query from these websites, in order to promote its business, which in turn provide some margin of profit to these website companies. Thus, we see how important API is and how much it is useful to provide access to service by other websites.

Installation Of Jmeter

Now after having a little walkthrough of API, let us see how JMeter is set up and installed, to test API.

  • Go to the link and select zip file of JMeter setup as below:
    http://jmeter.apache.org/download_jmeter.cgi

  • Once downloaded, keep the folder in a drive of your choice. I have kept in D drive. Unzip it.
  • After Unzipping, go to the following folder D://apache-jmeter-3.1/bin
  • Click on Jmeter.bat file available in this folder, to run Jmeter.

  • JMeter window gets launched

  • Alternatively, you can do the same through Command Prompt, after following the below given steps:
  • Open Command Prompt
  • Change directory to D://apache-jmeter-3.1/bin
  • Run Jmeter.bat command

  • Now JMeter is ready for use.

API Testing Using Jmeter

Now we are ready to create a simple API testing test plan using Jmeter. Go to Apache-JMeter window.

Step1

  • Add a thread Group and rename it as per your wish.
  • Right click on Test Plan and go to Add –> Thread(Users) –> Thread, Group

  • Here it is being renamed as My First API Test Plan.

Step2

  • Add HTTP Request from a sampler.
  • Right click on Thread name and go to Add –> Sampler –> HTTP Request

  • It can be named as per the name of one’s choice. Here it is being renamed as Country API Testing.

Now time to API Testing….
Following is REST web-service API to get a list of all Countries
http://services.groupkt.com/country/get/iso2code/IN

  • This API returns Country name and Alpha codes of the Country after passing country code in URL. For example IN for India, AF for Afganistan.

  • One can get list of all country names and country code using the following API
    http://services.groupkt.com/country/get/all

Step 3

  • Now next step is to break this API to use it in Jmeter
  • Protocol: HTTP
    IP: http://services.groupkt.com
    Path: /country/get/ iso2code/IN
    Types of Request: Get

Step 4

  • So now that we have broken all the components of Rest API request, we will use it in HTTP request of Jmeter, like as shown below

Step 5

  • So now that we have filled the HTTP request of Jmeter as required, we are ready to run the test.
  • But before running the test, we require to add a listener, which will show request and response.
  • Add a Listener.Right click on Test Plan –> Select Listener –> Add View Results Tree Listener.

Step 6

  • Now we are ready to run our first API testing test case.

  • After running the test, we are able to see the request and response. Green sign on Listener indicate that test is the success.

  • See the Request data

  • See the Response data

  • Now let us add an assertion to our test to verify if our test is getting expected output or not.

Step 7

  • So now we will put assertion to our test, for that Right click on test Plan and from the assertion list select Listener as Response Assertion.

  • From the response select text “India” and add to Response Assertion

  • Now we are ready to run the Test Plan again.

  • Green sign indicates that Test Case is Pass.

  • Write Pakistan in Response assertion which is not there in the response and see the result

  • And the result is Fail


This was basic API testing test plan with Assertion.

About the author

RamPrakash Singh author

Ram Prakash has worked in various domains of testing including security, performance, security testing and automation testing. Including several tools like QTP, selenium, LoadRunner,JMeter, VSTS Coded UI, soap UI, Burp Suite etc.

3 Comments

Kailyn

May 23, 2017 at 5:45 am

Thanks for writing such a nice article on this topic.

kannaki.k

April 11, 2018 at 7:43 am

Its very useful and explain very clearly.Thank u so much.

ben

June 21, 2018 at 5:51 am

Great tutorial. I got some insight about API testing.

Leave a Reply

Your email address will not be published.