welcome

Reusable Automation Test Scripts for API testing

In the API Testing world, if an automated testing tool doesn’t provide reusability then you will pay a great cost while maintaining the test suites over some time. Reusability and maintainability are two sides of the same coin. If reusability decreases then maintainability also decreases and vice versa. If you have a small number of APIs/test cases then reusability might not be a concern for you, but if your whole architecture is based on how to create APIs, the testing tool then reusability plays a great role while creating the automation test suites for those APIs. It is very important to think about this dimension while choosing the API automation framework/tool. We should follow the DRY (Do not repeat yourself) principle.

Reusability can be done at various levels while doing the automation test. e.g.

  • While doing scenario-based testing, repeated test cases can be defined once and reused at multiple places.
  • While doing the API testing through swagger schemas, if multiple APIs return the same type of response, then we should not repeat the schema validation logic or schema multiple times to validate the API response.
  • For validating the positive and negative scenarios of an API, the only thing that changes is the test input data. In this case, we may reuse the whole test logic.
  • Response Validation logic which is common among several APIs can also be reused.

In vREST NG, the issue of reusability and maintainability is on top priority for us so that a great amount of effort can be saved later while maintaining the test testing suites. We have seen a lot of failed automation test efforts just due to this issue.

Next, we will see, how vREST NG handles the reusability concern.

Reusing the same test cases across scenarios

There are various test testing scenarios in which the same set of test cases or steps are repeated. It should be possible to have a single repository of test cases to avoid the duplication of requests in each workflow. Consider the following scenarios:

Scenarios:

  • Scenario 1
    1. Login to Application
    2. Create a record
    3. Update the record validating the condition 1
  • Scenario 2
    1. Login to Application
    2. Create a record
    3. Update the record validating the condition 2

In the above automation test scenarios, Ist and IInd step of each scenario are duplicates. In vREST NG, instead of duplicating the steps, we can maintain a global list of steps and reuse the same steps by referencing them. So, our solution will look like this:

Global List:

  1. Login to Application
  2. Create a record
  3. Update the record validating the condition 1
  4. Update the record validating the condition 2

Scenarios:

  • Scenario 1
    1. Reference to Login to Application
    2. Reference to Create a record
    3. Reference to the record validating the condition 1
  • Scenario 2
    1. Reference to Login to Application
    2. Reference to Create a record
    3. Reference to Update the record validating the condition 2

Reusing the Swagger/OpenAPI schema definitions

The way, Swagger/OpenAPI provides us the ability to define the schema definitions globally and we may reference those schema definitions in our API specs. We may even further break our schema definitions into small reusable chunks and cross-reference them. It should be possible to reuse the schemas in the same way. In vREST NG, we maintain a global list of schema definitions as it is defined in the swagger spec.

The benefits we gain with this approach are:

  1. Importing of swagger spec becomes easier.
  2. APIs test cases can be easily validated using the swagger schema references.
  3. It further enables us to sync the swagger schema easily over some time to incorporate the swagger schema changes.

For more information, please read my blog post on “Easily validate your REST APIs by using the swagger definitions“.

Reusing the validation logic for positive and negative scenarios

scenario

While validating an API's positive and negative scenarios, the validation logic is mostly the same. The only thing that changes is the test input data. Negative scenarios mostly share a single JSON schema or a small set of JSON schemas. How do we handle such a scenario?

In vREST NG, we provide data-driven testing to cleanly handle this scenario. The test data is completely separated from the validation logic. This approach offers various benefits as compared to the traditional approach of API Testing.

  1. Reduction in the number of test cases
  2. Less technical knowledge is required to write test cases
  3. Highly maintainable test cases
  4. Enables the option to generate Test Data via some library or script

For more information on this approach, please read this section of the vREST NG Guide on data-driven testing.

Also, you may visit this sample test data repository for data-driven testing.

Let us see, how it works. Consider various scenarios of the Update Contact API test case as below. Suppose this API accepts input for fields name, email, designation, organization.

  1. Validate whether the Update Contact API should return an error when the name field is blank
  2. Validate whether the Update Contact API should return an error when the name length is greater than the permissible length limit
  3. Validate whether the Update contact API should return an error when the designation length is greater than the permissible length limit
  4. Validate whether the Update contact API should return an error when the organization length is greater than the permissible length limit
  5. Validate whether the Update Contact API should return an error when setting an invalid email for a contact
  6. Validate whether Update Contact API by ID works properly with all valid details

The above list can be very exhaustive when we apply various permutations and combinations on our API input domain. If you closely look at the above test cases then you can see that the test logic is mostly the same, the only thing that changes is test input data.

api

With data-driven testing, we can write this test input data and partial validation logic into an excel sheet and feed that excel sheet input to our test API to validate various scenarios.

application

Figure: Demonstrates the close integration between Excel sheet and vREST NG Application

vREST NG provides powerful integration between the excel sheet and the application. You will just need to write your test data in the excel sheet and instantly you may run the scenarios in vREST NG Application. Even you don’t need to upload the excel sheet at all after each change or event at the beginning. vREST NG even further writes the test logic (expected response body) for you automatically in the excel sheet.

Common Response Validation Logic

There are several situations in which test cases share a common validation logic and various utility functions are also required to generate the test input data. So, we should be able to reuse the validation logic or generic utility functions.

In vREST NG, in most of the situations, you will not need to write a single line of code. But there may be some complex situations in which custom response validation is necessary to cater your specific validation needs. We provide users the ability to write custom response validators which can also be reused across test cases. Users can also write custom utility methods which can also be reused across test cases.

Finally, Reusability is a major concern for vREST Team. We try to build each new feature with this concern in mind and try to solve the issue at its core. We designed vREST NG with these things in mind to make your API testing experience more pleasant. Just visit vREST NG website and try out vREST NG and let us know your feedback and also enlighten us to improve reusability further.

Learn how vREST NG prioritizes efficiency and maintainability, offering a seamless experience for creating and maintaining automation test suites with us. Elevate your API testing with Optimizory today!

Have any queries?

Please send a mail to support@optimizory.com to get in touch with us.