Swagger + Excel Sheets, a wonderful way of validating REST APIs
Tags: testing, productivity, tutorial, showdev
Published: 2019-12-30 13:29:33

Swagger Files (aka OpenAPI Specification) is the most popular way for documenting API specifications and Excel sheet provides an easy and simple way of writing structured data. Anybody can write data in excel sheet irrespective of their programming skills. Introducing vREST NG (An enterprise ready application for Automated API Testing), which combines the power of both to make your API Testing experience more seamless. The approach is also known as Data Driven Testing.

Data Driven testing is an approach in which test data is written separately from the test logic or script.

So, this is how the process looks like:

![Swagger + Excel Sheets](https://thepracticaldev.s3.amazonaws.com/i/4pukd5se0boywpqzgfh9.png)

vREST NG uses swagger files to generate all of the test logic and sample test data CSV files. vREST NG reads test data from the CSV files and iterate over the rows available in the CSV files and run the iterations one by one. Today in this post, we will look at the following in detail:

1. How you may generate the test cases by using the swagger files.
2. How you may feed the test data to those generated test cases through an excel sheet.


## How to perform Data Driven API Testing in vREST NG

To elaborate the process, I will take a sample test application named as contacts application which provides the CRUD APIs. I will guide you through the following steps:

1. Setup the test application
2. Download and Install vREST NG Application
3. Perform Data Driven API Testing in vREST NG


### Setup the Test Application:
You may skip this step if you want to follow the instructions for your own test application.

Otherwise, just download the sample Test Application from this [repository link](https://github.com/dheerajaggarwal/contacts-vrest-ng-ddt). You will also need to install the following in order to setup this sample test application:

* NodeJS (Tested with v10.16.2)
* MongoDB (Tested with v3.0.15)

To setup this application, simply follow the instructions mentioned in the [README](https://github.com/dheerajaggarwal/contacts-vrest-ng-ddt) file of the repository.

### Download and Install vREST NG Application
Now, simply download the application through [vREST NG website](https://ng.vrest.io/) and install it. Installation is simpler but if you need OS specific instructions, then you may follow this [guide link](https://ng.vrest.io/docs/app/installation.html).

After installation, start the vREST NG Application and use **vREST NG Pro** version when prompted in order to proceed further.

Now first setup a project by dragging any empty directory from your file system in the vREST NG workspace area. vREST NG will treat it as a project and store all the tests in that directory. For more information on setting up project, please read this [guide link](https://ng.vrest.io/docs/app/new-user.html).

For quick start, if you don't want to follow the whole process and just want to see the end result. They may download and add this [**project directory**](https://github.com/dheerajaggarwal/contacts-vrest-ng-ddt/tree/master/test/contacts-api-tests) in vREST NG application directly.

### Performing Data Driven API Testing in vREST NG
vREST NG provides a quick 3 step process to perform data driven API Testing:

(a) Import the Swagger File

(b) Write Test Data in CSV Files

(c) [Optional] Setup Environment

Now, we will see these steps in detail:

#### (a) Import the Swagger File
To import the Swagger file, simply click on the Importer button available in the top left corner of the vREST NG Application.

![vREST NG Importer button](https://thepracticaldev.s3.amazonaws.com/i/6gvh7cz0200m0mocpi5v.png)

An import dialog window will open. In this dialog window:

1. Select "Swagger" as Import Source
2. Tick the option `Generate Data Driven Tests`. If this option is ticked then vREST NG Importer will generate the data driven test cases for each API spec available in the swagger file.
3. Provide the swagger file. For this demonstration, we will use the swagger file from the test application repository. [Download Swagger File](https://raw.githubusercontent.com/dheerajaggarwal/contacts-vrest-ng-ddt/master/swagger.yaml)

The dialog window will look something like this. Now, click on the Import button to proceed further.

![vREST NG - Importer Dialog](https://thepracticaldev.s3.amazonaws.com/i/ma1b3wheg8maa4xvfjw0.png)

The import process has done the following things so far:

1. It has generated a test case for each API spec available in the Swagger or OpenAPI file. And test suites will be generated against each tag available in the swagger file.

    ![Generated Tests from Swagger / OpenAPI File](https://thepracticaldev.s3.amazonaws.com/i/r84pabqt9ldw0d2q4n1m.png)

2. It has automatically created the sample CSV files against each test case with desired columns according to your swagger file as shown in the following image.

    ![vREST NG - Generated CSV File](https://thepracticaldev.s3.amazonaws.com/i/0lu2udtgwel5uj2at0t3.png)

    We will discuss in detail on how you may fill this excel sheet later in this post.

3. The generated CSV files are also automatically linked as shown in the following image.

    ![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/ruhrppvf3eme1jvpnqi8.png)

    So, before every test case execution, the test case will read the data from the linked CSV file and converts it into JSON format and store it in a variable named as data. Now the test case will iterate over the data received and run the iterations. So, if you make a change in CSV file, just run the test case again. Test Case will always pick up the latest state of the CSV file. **No need to import again and again**.

4. It has automatically inserted some variables in the API request params as per the API definitions available in the swagger file. These variables value will picked up from the linked CSV file automatically.

    ![vREST NG - Generated Request Params and Auto Linking with Excel Sheet](https://thepracticaldev.s3.amazonaws.com/i/9w4ic6s1r8ck2xuih8p3.png)

5. It has automatically added the response validation logic as well. In the following image, status code assertion is used to validate the status code of the API response. Text Body with Default Validator assertion compares the expected response body with the actual response body. Text body with Default Schema Validator assertion validates the API response through the JSON schema.

    The expected status code will be picked up from the linked CSV file.

    ![vREST NG - Generated Assertions from Swagger file](https://thepracticaldev.s3.amazonaws.com/i/ojp74bg918vx2fxekpev.png)

    And the expected response body will also be picked up from the linked CSV file.

    ![vREST NG - Expected Body is auto binded from Excel Sheet](https://thepracticaldev.s3.amazonaws.com/i/6pfme5u9uqsblruo6luz.png)

    And the expected schema name is also picked up from the linked CSV file.

    ![vREST NG - Expected Schema is auto binded from Excel Sheet](https://thepracticaldev.s3.amazonaws.com/i/5x1q6ixmrp31synhaq8o.png)

6. It has imported all the swagger schema definitions in the Schemas section available in the Configuration tab.

    ![vREST NG - Imported schema definitions from Swagger File](https://thepracticaldev.s3.amazonaws.com/i/goj9lu8n41j7pl5qqr11.png)

    You may refer these schema definitions in the Expected Schema tab as discussed earlier. And in the CSV file, you will need to just specify the respective schema name for the test iterations in the **expectedSchema** column.

#### (b) Write Test Data in CSV Files
As we have already seen the data file generated from the import process. Let me show you the generated file again for the Create Contact API:

![vREST NG - Generated CSV File](https://thepracticaldev.s3.amazonaws.com/i/0lu2udtgwel5uj2at0t3.png)

In this sample file, you may add test data related to various iterations for the Create Contact API. In the iterationSummary column, simply provide the meaningful summary for your iterations. This iteration summary will show up in the Results tab of the vREST NG Application. You will need to fill this test data by yourself. You may even generate this test data through any external script.

Now, let's add some test iterations in the linked CSV file.

![vREST NG - Adding test data in excel sheet](https://thepracticaldev.s3.amazonaws.com/i/ristvieurigner2vkksh.png)

With the above CSV file, we are checking two test conditions of our Create Contact API:

1. When the name field is empty
2. And when the name field length is greater than the limit of 35 characters.

In the above CSV file, we have intentionally left the expectedBody column blank. We don't need to fill this column. We can generate this column's value via the vREST NG Application itself.

Before executing the test case, we need to configure the baseURL variable of your test application in the Configuration tab like this:

![vREST NG - Setting up environment variables](https://thepracticaldev.s3.amazonaws.com/i/wjk4aw0ipismhzh1te9v.png)

In this section, you can configure your various environments like prod, dev, staging and their configurations using variables.

Now, let's execute this test in vREST NG Application. Both the iterations are failed because expected response body doesn't match with the actual response body as shown in the following image:

![vREST NG - Execution and Diff report](https://thepracticaldev.s3.amazonaws.com/i/z5evbs0gddnkrtmwwnr2.png)

Now, click on button "Copy Actual to Expected" for each iteration. vREST NG will directly copy the actual response body to expectedBody column in the linked CSV file.

Now after this operation, if you look at the CSV file again. You can see that vREST NG has filled the expectedBody column for you as shown in the following image.

![vREST NG - Excel Sheet after `Copy to Actual Response` Operation](https://thepracticaldev.s3.amazonaws.com/i/gfeizkqmlz6fvx1q4ubp.png)

Note: If you have opened this CSV file in Microsoft Excel then you will need to close the file and open it again in order to reflect the changes. But some code editors e.g. VS Code automatically detect the changes on the file system and reflect it in real time.

Now, if you execute the test again, you can see that the test iterations are now passing.

![vREST NG - Successful Execution after copy operation](https://thepracticaldev.s3.amazonaws.com/i/ss8l27tgofiugnxpdxtx.png)

You may also see the expected vs actual response for the selected test iteration:

![vREST NG - Expected vs Actual Response](https://thepracticaldev.s3.amazonaws.com/i/f61rd0qbdlvf429jvbov.png)

And you may see the execution details of the selected iteration by going to Execution Tab:

![vREST NG - Execution Details](https://thepracticaldev.s3.amazonaws.com/i/2o6a0s8vjwaul4kvy95v.png)

So, in this way, you may execute test iterations for an API via CSV file. Just add iterations in your CSV file and run it in the vREST NG Application directly. No need to import again and again. It all just work seamlessly. So, it increases your test efficiency and productivity drastically.

#### (c) [Optional] Setup Environment
For the generated steps, you may also need to set the initial application or DB state before executing your tests. So that you can perform the regressions in automated way. Some use cases of setting up initial state can be:

1. Restoring the database state from the backups
2. Execute an external command or script
3. Invoke a REST API to setup the initial state

Now let's see how you may execute an external command or restore the database state from backup. Suppose for our sample test application, we already have taken a database dump and we would like to restore that dump before executing the test case. As we are using MongoDB for our sample test application, so we can use mongorestore command for our purposes.

You may specify the command as shown in the following image:

![vREST NG - Test Setup via executing command](https://thepracticaldev.s3.amazonaws.com/i/pwlisytl65ousbgcj24o.png)

The above command will restore the database from the backup which is already there in vREST NG Project directory >> [dumps folder](https://github.com/dheerajaggarwal/contacts-vrest-ng-ddt/tree/master/test/contacts-api-tests/dumps).

Note: Make sure the mongorestore command is in your PATH, otherwise you will need to provide full path of the command.

I would like everybody to have your feedback on this approach whether you find it useful or not for your API testing needs. Do like if you find this post helpful. Feel free to [contact me](https://www.linkedin.com/in/aggarwaldheeraj/) in case if you need any help or if you want to use the vREST NG Pro version for free.