How to create a Keyword Driven Automation framework for REST API testing using JMeter
- Sarath Babu V S

- Oct 30, 2020
- 2 min read
Testing REST APIs plays a major role in the overall testing pyramid.

As seen in the pyramid, more tests are carried at REST API level as compared to UI tests.
And automating those REST API test scenarios would give a good ROI apart from being stable and quick to run when compared to UI tests.
This blog presents a scenario where JMeter tool was used to automate REST API testing with a Keyword Driven approach that enables adding/removing test cases in CSV without worrying about how the tests are coded/automated. So, once the scenario is scripted, new tests can be added or updated easily by anyone without coding/jmeter knowledge
Step 1. Prepare a CSV that has the necessary input for each of the API call

Above example shows,
Each row represents a test case with needed test inputs
TestCase id listed in first column - this will help when correlating with output report
Transaction column - notes the particular API call/action we are interested in
Remaining columns - represent the necessary test input for that transaction
Step 2. Translating the test case into Jmeter script

Above screenshot shows the jmeter script details
Here, we read the test case from CSV (one row at a time), build the necessary API call by replacing input values from CSV into an API template, call the necessary API and extract the response values/store them in a file.
Read test case from CSV - this reads each test case (one row at a time) from the csv file
API templates - here we define the API request body by parameterizing the values, so that it is used as a template

In above screenshot, processIdParameter is a value that will be replaced at run time
with value from CSV
If controller - based on a flag, a particular API call would be made
BeanShell samplers (Construct request body) - read the templates and update the parameterized values with CSV values to construct the request body
HTTP sampler - makes the actual API request
Regular Expression Extractor - extract needed values from response

Save Responses to a file - saves responses to a file
Conclusion:
By following above pattern of scripting, we could automate any REST API testing in such a way that, once scripted, the only activity would be add/update the CSV file and run the script

Comments