How To Test Rest Service With Jmeter
In this article, I would similar to bear witness you a data-driven approach for Residual API testing. If you are new to JMeter/REST API Testing, I would like to read this commodity first to get some idea.
Goal:
Our aim here is to come up with a framework to test Remainder API with different HTTP Methods. This test will be driven through a spreadsheet and the spreadsheet volition comprise all the input parameters, API URL, HTTP Method, asking JSON, text response should be used for assertion etc.
By using one single HTTP Sampler, we would like to ship different types of request GET / POST / PUT / PATCH / DELETE. Even the HTTP Asking torso data volition be added dynamically at run time.
JSON Server:
I am going to employ this JSON-Server for this testing purpose. You tin speedily ready this up and running within a minute. Y'all could also use your application APIs if y'all have.
In one case it is installed, offset the server using below control.
json-server --watch db.json
My db.json looks similar this.
{ "books":[ ], "comments":[ ], "profile":{ "name":"typicode" } } Examination Scenarios:
Lets assume, we would like to execute these scenarios as role of our Balance API functional testing. Test description provides a high level idea most the test case.
If all requests are actually aforementioned and only the information is unlike as shown beneath, we could easily practice a data-driven testing in JMeter with 1 HTTP Sampler & a CSV DataSet Config.
Merely in our case, We take ane Get request, and so five Post requests, and so 1 Go request…etc. We transport different types of request. So, nosotros might finish upward creating our Test Plan as shown here
What volition happen if we need to test thousands of scenarios!!? How can we maintain such a huge JMeter test?
Data-Driven Testing:
In club to completely drive the testing through a spreadsheet, Lets move whatever data which could vary amidst these HTTP requests to the spreadsheet as shown hither afterwards carefully analyzing these requests.

- #1 – A simple GET asking to fetch all the items from the books category. For the first test case, nosotros would not accept any data. So, it should respond with empty list.
- #ii-#6 – We add together new books with different titles and authors using Mail requests. The input.json for these test cases would be as shown hither. We would be replacing the title and author at run time with the corresponding data in the spreadsheet.
{ "title": "${title}", "author": "${author}" } - #7 & #9 – Unproblematic Go requests for specific book details to check if the POST / PUT requests worked fine.
- #8 – A PUT asking to simply supersede the electric current item in the DB with the given data.
{ "championship": "${championship}", "writer": "${writer}", "price": "$10.00" } - #10 – A PATCH request to specifically update a data of the electric current item in the DB.
{ "title": "${championship}" } - #11 #12 – A DELETE requests to remove an item from the DB.
Equally we have moved all the variables to the spreadsheet, now by having only ane HTTP Sampler in JMeter test and past setting the HTTP Sampler backdrop/attributes at run time, we could run the entire test.
JMeter Test Program:
- I add aCSV Data Set up Config – to read the test scenarios and input parameters.
- Add a HTTP Sampler and update the details. Let the Method be with some default option. Too add a Header Managing director withContent-Blazon as application/json
- Add together a JSR223 – PreProcessor. This should be responsible for changing the HTTP Method based on the value in the spreadsheet. Information technology likewise sets the HTTP Request body information for Mail / PUT / PATCH requests.

The below statements change the current sampler HTTP Method.
def httpMethod = vars.get("http.method");
sampler.setMethod(httpMethod);
We use below statements to change the HTTP Body. We read the input.json file and so we replace the whatever variable with the corresponding data
def dataToBePosted = new CompoundVariable(new File(vars.go("jmeter.test.home") + vars.get("input.json")).text).execute();
def arg= new HTTPArgument("", dataToBePosted, cypher, true);
arg.setAlwaysEncoded(false);
sampler.getArguments().addArgument(arg);
- Concluding stride would be to add together exclamation.
- Run the test plan.
Download:
You can check the JMeter test plan hither in GitHub.
Summary:
Past moving all the variables to a spreadsheet and with one HTTP Sampler, we are able to test unlike types of requests. Adding whatever new tests to this test plan is very like shooting fish in a barrel. Every bit y'all already know, nosotros have to insert one more row in the spreadsheet with the plenty information for the new test. By looking at the spreadsheet, you can hands sympathise our coverage. Whatsoever update to the test data would as well be easy with this approach instead of updating each and every private HTTP Requests in JMeter.
Happy Testing & Subscribe 🙂
How To Test Rest Service With Jmeter,
Source: https://www.vinsguru.com/jmeter-rest-api-testing-a-complete-data-driven-approach/
Posted by: lukensorms1986.blogspot.com

0 Response to "How To Test Rest Service With Jmeter"
Post a Comment