Reading from CSV file in Jmeter ( Using “CSV Data Set Config”)

Sometimes, it may be necessary to run performance tests on parameterized data or perform data-driven testing using JMeter. In such cases, we can leverage CSV files to accomplish this. By storing the required data in a CSV file, we can easily read it and execute our tests on various data sets as per our needs. This allows us to efficiently conduct tests on diverse data without creating separate tests for each data set. This post will focus on how to read the data from the CSV file in Jmeter.

Let’s create a simple test where we will execute the “https://jsonplaceholder.typicode.com/posts/1” API, and later, we will parameterize the postID ( https://jsonplaceholder.typicode.com/posts/${postID} ) to fetch the details of various posts.

We will divide our article into the following points –

Let’s look at the above points one by one.

Executing the API
Set Up a Test Plan
  • Launch JMeter.
  • Go to the “File” Menu and select “New”.
  • This will create a new test plan to be used in our example.
Create a test plan in jmeter
Create a Thread Group
  • Right-click on the Test Plan.
  • To create a Thread group, simply go to “Add > Threads (Users) > Thread Groups”.
Create a Thread group in jmeter

The Thread Group is used to execute a specific number of tests. For simplicity, we will keep it set at 1. Please refer to the image below for configurations.

Threads set to 1
Add an HTTP sampler

We will use an HTTP sampler to execute a dummy API (https://jsonplaceholder.typicode.com/posts/1). Create an HTTP sampler by right-clicking on the Thread Group and selecting “Add” > “Sampler” > “HTTP Request”.

Create HTTP sampler in jmeter

We will hit the GET request on the “https://jsonplaceholder.typicode.com/posts/1” API. So, we will add it to the HTTP sampler by following the below steps.

  • Please enter “https” in the input box that is labelled “Protocol[https]:“.
  • Enter “jsonplaceholder.typicode.com” in the input box labelled “Server Name or IP”.
  • By default, the HTTP request value is set to GET. Since we are only executing a GET request, we won’t be changing it.
  • Set the Path to “/posts/1“.
executing api using http sampler
Add “View Results Tree” listener

View Results Tree listener is one of the most used listeners in Jmeter. It provides detailed information about server responses during performance testing.

Add the “View Results Tree” listener by right-clicking on the HTTP request and selecting “Add” > “Listener” > “View Result Tree”.

View Result Tree
Run Test and monitor results

Run the test case by clicking on the green start button and analyze the results in the “view result tree” listener.

Click on Run button

“View result tree” displays the request and response.

Result of "View Result Tree"
Parameterize the PostID using the CSV file

Let’s fetch the details of various posts by parameterizing the postID and reading it from a CSV file.

CSV file

Our file name path is “D:/Demo.csv”

We need to use the “CSV Data Set Config” Config Element to read data from a CSV file in JMeter. Let’s use it to read postIDs from our CSV file.

Add “CSV Data Set Config”
  • Right-click on the Thread Group
  • Select “Add” > “Config Element” > “CSV Data Set Config”
Adding "CSV Data Set Config"

You will see the below screen after adding the “CSV Data Set Config” Config Element.

CSV Data Set Config

New Element will, by default, be added at the end. Drag the “CSV Data Set Config” Element just above the HTTP Request Sampler.

Test Plan structure
Parameterizing the PostID
  • Select the required CSV file by clicking on the “Browse” button. In our case, it was “Demo.csv”.
Click on Browse Button
  • Enter the “postID” as the variable name in the “Variable Names” field. The values from the CSV file will be stored in the postID variable.
Enter variable Name
Use the variable name in the HTTP sampler

We can use the ${postID} variable in our Test plan to retrieve details for posts 1, 2, and 3 using the HTTP sampler.

Change the path in the HTTP sampler from “/posts/1” to “/posts/${postID}”.

Using postID variable

and set the loop count in the Thread Group to “3”.

Setting the loop count to 3

And finally, click on the “Start” button to run the test.

Start button

This will execute the API three times with the postID values of 1, 2, and 3.

Go to the View Results Tree and click on the HTTP requests executed.

Parameterization Result

The two remaining requests were carried out using postID 2 and 3.

So, this is how we can fetch the data from the CSV file and use it to execute the tests.

Dig Deeper into the “CSV Data Set Config”

If you recall, we had three rows in the CSV file and set the loop count in the thread group to three. However, what would have occurred if the loop count in the thread group did not match the total number of rows in the CSV file?

This will lead to two scenarios –

  • Loop count is greater than the total number of rows in the CSV file.
  • Loop count is less than the total number of rows.

Let’s look at each of these scenarios one by one.

Loop count is greater than the total number of rows

If the loop count is 5 and the total number of rows in the CSV file is 3, then during the 4th iteration, Jmeter will return to the 1st row of the CSV file to retrieve its value. Similarly, during the 5th iteration, it will retrieve the value from the 2nd row of the CSV file. This means that the CSV file will be reused from the beginning every time it is fully consumed.

Let’s see with an example. Keep the Loop count in the thread group as 5 and use the same CSV file with 3 rows.

Setting the loop count to 5

Now, run the test again.

“View Results Tree” will display 5 runs. Click on the 4th run to see that the postID is set to 1 for the request execution.

Showing 4th request
Loop count is less than the total number of rows

In this situation, if the CSV file contains more rows than the specified number of iterations, the iterations will only pick their corresponding rows from the CSV file. For instance, if the loop count is set to 2 and the CSV file has 3 rows, then the first two iterations will only pick from the first two rows of the CSV file, and the last row will not be used.

Using headers of CSV file as the variable names

If you recall, we previously added the variable name “postID” in the “Variable Names” section of the “CSV Data Set Config”. However, if we include a header in the CSV file, it is not necessary to manually add the variable name in the “Variable Names” section. JMeter will automatically recognize the corresponding column’s header as the variable name to be used in the Test Plan.

See our updated CSV file.

Updated CSV file

and now, remove the variable name from the “Variable Names” Section.

Empty "Variable Names" field

When you run the test plan again, you will see that the postID variable is correctly assigned with respective values.

Stopping execution if the CSV file is fully consumed

When the loop count exceeded the number of rows in the CSV file, the file was reused from the beginning. However, we could also choose to stop the execution once the CSV file is fully consumed.

To achieve the task, you need to set “Recycle on EOF ?” as “False” and “Stop thread on EOF ?” as “True”.

Setting field values

After setting the Thread Loop Count to 5, run the test plan again. You will see in the “View Results Tree” that only 3 executions occurred.

This is it. We hope that you have liked the article. If you have any doubts or concerns, please write to us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *