Swagger (now known as OpenAPI) is a well-known framework for describing the API specifications. Swagger removes ambiguity among multiple teams (e.g. frontend team, backend team, testing team) by having the proper API specification. And development and testing teams can work in parallel by referring the same spec.
Today in this post, we will look at how the swagger spec also helps us in making the automated API validation process easier. We have developed a tool named as vREST NG for validating the REST APIs and it also imports the swagger specification and makes the validation process against the imported schema much easier.
Let us see how you may validate your REST APIs by using swagger specification.
Step 1: Download vREST NG
Download the vREST NG version. vREST NG provides ability to import the swagger/OpenAPI specification to generate the test cases. If you download the Pro version then it will allow you to generate the data driven tests as well. Please click on the link below to download the vREST NG.
Step 2: Download the Swagger specification file
Write down your API specification in Swagger Editor and download the swagger specification file either in JSON or YAML format. I hope this step is pretty much clear. For this post, I will use the famous pet store swagger file.
Step 3: Import Swagger specification in vREST NG
To import swagger specifications in vREST NG, follow the steps below:
- Open vREST NG Application
- Create an empty directory anywhere on your system and add as a project in vREST NG.
- Click on that project. It will automatically move you to the Test Cases Tab.
- Click on
Importer
button available on the top left side of the application.
- After clicking, a popup window will appear to import the Swagger API Specifications. In the pop-up window, provide the import source as Swagger and select the desired swagger file which you want to import. In our case the file is petstore-swagger.json.
- Now click on
Import
button to import this swagger specification.
Now let us look at the data imported by vREST NG:
- Test Cases
vREST generates the test cases automatically for each API. Tags are converted into the Test Suites automatically.Figure: Test Cases generated from Swagger Specification - Request Parameters
vREST also extracts the query or form parameters and even generates the sample request body from the schema available in the Swagger specification.Figure: Query Parameters extracted from Swagger Specification - Assertions
vREST also generates the assertions for each API spec. In the following screenshot, there are two assertions generated by vREST.- The first assertion is validating the status code of the API response.
- The second assertion is validating the API response schema against the schema defined in the swagger specification.
Figure: Assertions generated by vREST Swagger Importer And the schema is imported in the “Expected Schema” tab. You may notice that references are still maintained and are referring to global schemas available under the
Configuration
tab.Figure: Expected Schema imported from Swagger Specification
- Schemas
Swagger definitions are imported into theConfiguration tab
>>Schemas Section
. Which test cases can reference as we have seen in theAssertions
section above. So it improves the reusability because all the schema definitions can be defined globally and you may reuse them in your test cases.Figure: Schemas imported from Swagger Specification
Finally, We would like to have your feedback regarding the Swagger Import feature supported by vREST NG. To try out, just visit vREST NGÂ website to download the tool and let us know what do you think about this. This tool is designed to be enterprise-ready and after several years of experience and hard-learned lessons in the domain of API Testing. In future posts, I will discuss, how you may incrementally update the tests and/or schemas whenever there is some change in swagger specification.