Swagger in conjunction with OpenAPI is a way for REST API’s to be build, documented and consumed. It is defined in either YAML or JSON. OpenAPI and Swagger are both open source, and their use is commonplace amongst development teams and can make the life of a performance tester a lot easier as it allows us to build JMeter tests directly from the definition of the endpoints.
The object of this post is to look at how we can produce JMeter tests directly from a Swagger definition. If you are unfamiliar with Swagger it is worth spending some time reading the online overview as it will provide a good understanding of how the specification works.
Now clearly in order for you to leverage the benefits of building performance tests from Swagger definitions you are going to need to have your development teams use it to describe the API contract for service they are producing, but for the purposes of this post we will use a test definition called Swagger Petstore.
The Petstore Test Definition
The Petstore is a set of example API’s which demonstrate how, if your organisation uses Swagger, you may see your application REST API’s defined with examples of the verbs POST, PUT, GET and DELETE.
Now you may be familiar with using Swagger but if you are not, we will run through a simple example using the Swagger Petstore.
While this is an example site any APIs defined by your development teams using the OpenAPI specification will work in a similar fashion albeit with endpoints and payloads that they define.
We can see that the specification is broken down into pet, store and user.
If we select the second option down under pet.
And we select Try it out and leave the payload as it is and select Execute we see a response like this.
Which gives us a 200 response and an id for our newly created pet.
This is just a stubbed response and normally there would be a database behind it to manage the data.
Building our test
We have had a brief look at OpenAPI and Swagger but now we come to the subject of this post, to build a JMeter test from the OpenAPI specification.
We need to download the OpenAPI Generator CLI artifact, and this page provides all the details you need to perform the download for the platform you are testing on.
For the purposes of this post, we have downloaded the JAR file into a location on its own as this will allow us to create the JMeter tests in the same folder.
If we now open a command line prompt and we are going to use the following command:
java -jar .\openapi-generator-cli.jar generate -i https://petstore.swagger.io/v2/swagger.json -g jmeter
Which is going to generate our JMeter tests for us based on the .json definition of the Petstore.
The location of the .json is defined at the top of the Petstore page.
If we now execute this command.
If we now look in the directory we copied the .jar file to we can see that 3 .jmx files have been created along with 3 .csv files.
We can see that we have a test for pet, store and user which mirrors the way these services are defined in the specification.
Let’s take a quick look at one of the JMeter tests produced.
Effectively each endpoint has a ThreadGroup produced for it.
The comma separated files that are produced allow you to set your data up for use in the test that’s been produced.
These comma seperated files contain a heading for each define parameter in the test and looks like.
Hopefully this has given you an insight into how easy it is to produce JMeter tests from OpenAPI specifications as well as providing you an overview of Swagger and OpenAPI if you were not familiar with it.