Anastasia Golovkova is a Quality Assurance specialist with variety of skills in automation testing and 2 years of experience in IT.

Learn JMeter in 5 Hours

Start Learning
Slack

Test Your Website Performance NOW!

May 15 2017

REST API Testing - How to Do it Right

 

REST is a software architecture style, commonly used for web services. Due to its popularity,  you will probably need to load test RESTful APIs at some point. API testing can be done with Apache JMeter™. Read this blog post to find out how.

 

REST and RESTful APIs - An Introduction

 

REST (Representational State Transfer) is a software design pattern typically used for web applications. We can think about REST as a software architecture style. The basic idea of REST is treating objects on the server-side (e.g rows in a database table) as resources that can be created or destroyed. When we say RESTful APIs we are referring to the web services implementation of REST architecture.

 

The most basic way of thinking about RESTful is as a way of formatting the URLs of your web applications. For example, if your resource was called "articles", then:

  • /articles - How a user accesses ALL posts.
  • /articles/:id -How a user accesses and views an individual post which was retrieved based on their unique id.
  • /articles/new How a form for creating a new post is displayed.

 

RESTful API defines a set of functions that developers can use to perform requests and receive responses via HTTP protocol, such as GET, POST, PUT and DELETE.

  • Sending a GET request to /users would retrieve the users from  the database level.
  • Sending a POST request to /users would create a new user on the database level.
  • Sending a PUT request to /users/:id would update the attributes of a given user, again identified by a unique id.
  • Sending a DELETE request to /users/:id would delete a given user, again identified by a unique id.

 

How to Load Test RESTful APIs with JMeter

 

Load testing RESTful APIs can be easily done in JMeter with the HTTP Request Sampler.

 

Our working environment:

  • Ubuntu 16.04
  • JMeter 3.2
  • RESTful API available at jsonplaceholder.typicode.com. It’s very simple, but it’s enough for our work and for practicing. If you need to load test you need to add your own server’s name.

 

1. Add a Thread Group

 

Right Click ->  Add- > Thread Group

 

Here we defined “Number of Threads = 50” and “Loop Count = 5”. This allows us to simulate 50 different requests for 5 times. I named the Thread Group “REST Example”.

 

Add a thread group. API testing

 

2. Add a HTTP Request.

 

Right Click on REST Example -> Add -> Sampler -> HTTP Request.

 

Add a HTTP Request. API testing

 

3. Fill in the necessary values:

  • Name - the name of the current sampler
  • Protocol - by default this is HTTP, but it can also be HTTPS or FILE
  • Server - the IP address or domain name of the server. This field is required
  • Port - the web server the port listens to. By default: 80
  • Method - GET, POST, PUT, DELETE, etc. This field is required
  • Path - the path to the resource. If this requires string parameters in the query, add them below in the “Send Parameters With the Request” section
  • Content encoding - if you are using POST, PUT, PATCH or FILE, there is a character encoding to be used

 

POST

 

We will post data as a JSON object. JSON, or JavaScript Object Notation, is a way to store information in an organized, easy-to-access manner. In a nutshell, JSON gives us a human-readable collection of data that we can access in a logical manner.

 

4. Set a Content-Type header.

 

Right click on Test Plan -> Add -> Config element -> HTTP Header Manager

 

5. Add a “Content-Type” field equal to “application/json”, as shown in the picture below:

 

Add a “Content-Type” field equal to “application/json”

 

Important! Our RESTful API in this blog post is so simple that we do not need to send headers, and you can skip this step. But in other cases, this step is necessary.

 

5. Add a HTTP Request Sampler to our REST Example thread group.

 

Right click on REST Example -> Add -> Sampler -> HTTP Request

 

6. Fill in the relevant fields:

  • Name - POST method
  • Server - jsonplaceholder.typicode.com
  • Method - POST
  • Path - “/articles
  • Parameters-Post Body - add the body request

 

{
    title: 'foo',
    body: 'blazemeter',
    userId: 1
}

 

You should now see this window:

 

Add a HTTP Request Sampler to our REST Example thread group. API testing

 

7. Add a Listener to see the Request response.

 

Right Click on REST Example -> Add -> Listener -> View Results Tree

 

It will look like this:

 

Add a Listener to see the Request response. API testing

 

GET

 

8. To check the GET method let’s create a new HTTP Request sampler and fill in following credentials:

  • Name - GET method
  • Server - jsonplaceholder.typicode.com
  • Method - GET
  • Path - “/articles

 

GET method, API testing

 

9. In the View Results Tree you can see all the posts received by the GET method:

 

In the View Results Tree you can see all the posts received by the GET method. API testing

 

Response Assertions

 

10. If you need to verify that the post is presented in your received response, use the Response Assertion.

 

Right Click on GET method -> Add -> Assertions -> Response Assertion

 

11. Leave this window’s settings as default. You only need to configure the “Patterns to Test” field, like below, according to the response you want to test.

 

response assertions, API testing

 

How does the Response Assertion work?

 

In responses, different posts have different ids, titles and bodies. The response assertion looks through the whole response and looks for matches between the patterns you wrote down and what the response contains. If there is a match, the test will pass. If not - test will not pass.

 

In this example I’ve taken the title of a post at our server. It is equal to “qui est esse”. Since there is a match, the test passes.

 

Response Assertion, API testing, jmeter

 

But, if we change our pattern to “blazemeter title”:

 

response assertion api load testing

 

Then you can see, that our GET method tests doesn’t pass.

 

assertion failure message API testing

 

The Assertion failure message: Test failed: text expected to contain /blazemeter title/

 

That’s it! You have now learned all the necessary information to create your own RESTful API tests. To learn more about performance testing with JMeter check out this free 5 day online JMeter training course.

 

To learn about load testing with BlazeMeter, which enhances JMeter abilities, request a demo or put your URL or JMX file in the box below and your test will start in just minutes.

     
arrow Please enter a valid URL

Interested in writing for our Blog?Send us a pitch!

We're working on starting your first test

Testing 20 Virtual Users

Starting your test in around 2-4 minutes. Your report will appear once we've gathered the data.

0

Status: Preparing Your Test

Your email is required to complete the test. If you proceed, your test will be aborted.