First in a series focusing on using JMeter for functional testing of an API.
Let’s get started!
In our first tutorial we will learn how to setup a very basic HTTP Request Sampler to make a call to an API running on the internet. I will introduce JMeter test plans, thread groups, HTTP Request Samplers, and View Results Tree Listeners.
If you are not familiar with these topics, please take some time to read up on them before proceeding with this tutorial. I will be assuming a certain level of familiarity with the topics.
- HTTP Methods : We will be using GET and POST throughout the tutorials, so it would be good to have an understanding of what these are and how they are used. You can read a good summary here.
- JSON: All our communication with the API in these tutorials will be in JSON. A good introduction can be read here.
Functional Testing 01: My First API Call
In this tutorial we will be creating a very simple test to perform a GET call against an API that will return some basic information about the API itself. we will create a new test plan in JMeter, add a thread group to contain our test, add an HTTP Sampler to configure how the API call should be made, and then run the test and view the results of the API call in a Results Tree.
- Start JMeter
JMeter creates a new test plan for us. A test plan is simply the file within which we are going to configure all our tests.
- Right-click on the test plan in the browser on the left and select Add > Threads (Users) > Thread Group
A thread group in JMeter represents a sets of users running a set of tests. In order to run any test it must be in a Thread Group. Thread groups will become more important when we move on to load or performance testing an API. For now we will simply use it as a means of organizing our tests and leave each thread as representing one single user. So let’s change it Name to “My First Test” but we will make no changes to the defaults for the rest of the Thread Group.
Now that we have a thread group we can create our test within it.
- Right-click on the test plan and select Add > Sampler > HTTP Request
The HTTP Request Sampler is what we will use to make our request to the API on the internet.
- Configure the HTTP Request Sampler
Name: The Sampler name is how we will see the Sampler displayed in the browser on the left and in any results. Set Name to “GET API Info“.
Server Name or IP: This is the base URL for the API. This is the beginning of the URL at which you will access the API. Set it “www.scottmollon.com“.
Port Number: Sometimes a specific port is needed to access an API. This is used to construct a valid URL for the request we are going to make. Set it to “41431“.
Protocol: This will be set to “https” or “http” depending on what the API you are connecting to requires. This is used to construct a valid URL for the request we are going to make. Set it to “http“.
Path: The Path is the remainder of the URL path to API resource you wish to access. In this case we will give it the path to the Info resource in the API. Set Path to “/learnjmeter/api/info“.
Method: The Method species the HTTP method to use when sending the request to the API. In this case we are doing a simple Read request of the API info, so we will set Method to “GET“.
We now have an HTTP Request Sampler that’s ready to run. It has been configured to send a GET request to http://www.scottmollon.com:41431/learnjmeter/api/info
But there’s one last thing we need to do in order to see the results of running the test.
- Right-click on the test plan and select Add > Listener > View Results Tree
The View Results Tree gives us an easy way to view our requests and the results of our API calls. I like to keep mine at the top of the browser to make it easy to find.
- Now we are ready to run! Click the green arrow in the toolbar or select Run > Start from the menu. Very quickly you should see the test name appear in the Text section of the View Results Tree. Hopefully the test name is green meaning it has finished successfully. If for any reason the test fails the test name will be red.
- Click the Get API Info in the View Results Tree.
- Select the Request tab to the right of it.
Here we can see information about the request that we made. We can see the request was GET http://www.scottmollon.com:41431/learnjmeter/api/info
- Select the Response tab.
This tab contains information about the response to request from the server. Here we can see that the server responded with a JSON object containing the name of the API and the current version.
Congratulations! You just made your first API call with JMeter!