Schema Validation is something that is the first thing that comes to mind when it comes to the structural validation of your API’s responses. There are multiple ways of validating your API’s response and one of them is the schema validation.
If you are not familiar with the concept of a Schemas and how to validate your API’s responses against a Schema, then don’t worry you have landed in the right place. Firstly, let’s try to understand the most basic question of all…
To be put in the simplest form, a schema is nothing but a specification, on the basis of which a piece of data, written in any format (like, XML, JSON), is structured. It is a kind of document that defines the expected data type and format of each field of your data.
The only characteristic of a schema that makes it so useful is that it exists in both Machine-Readable and Human-Readable format. Thus it is a kind of document that can be understood by you and also by some software. So, what does this imply, then?
This means that if you have a schema for your data, then you have the opportunity to validate your data against this schema. Let me give you an example of whatever you have just read.
Let’s say that we have the following JSON data:
Then we can say that the following is the schema of the data above:
You can try it out on your own, just go to jsonschema.net and you will be able to generate the schema of any JSON data. If the meaning of a schema is understood, then let’s move to the question that follows what we just saw.
Now that you have understood what a schema is, then let’s try to find out how it is used. Following are some of the most important uses of a Schema:
Well, there are many ways of performing Schema Validation on your API’s response, but we will be seeing how you can do the same in vREST NG (follow this link to download vREST NG).
Once you are inside vREST NG, you can validate your API test case’s responses against JSON schema. You can define this JSON schema in,
Note: Both APIs consumes the same JSON schema as the response structure of both APIs are same.
Now if you execute these two test cases, then both will pass. Now if we have hundreds of such test cases, then we will be defining the same schema in each test case. To overcome this issue we may define the schema globally in Project Configuration tab and re-use the same schema in our test cases.
In Configuration >> Schema Section, you may define all your schemas related to your APIs. Then you may use them in your test cases for response validation. In your test cases, you just need to refer the global definitions defined in Schemas section. This section is also useful when you import test cases via swagger definitions.
Let’s define our Contact schema in Configuration >> Schemas Section as shown below:
Now, we may use this schema in our test cases by referencing it in Expected Schema tab of our test cases as shown below:
By now, you are aware of what is a schema and how to perform Schema Validation, which is a major part of API Testing. There are other ways to structurally validate your API Responses, like validating them against Swagger Spec. Follow this link to understand how you can Import Swagger Spec into vREST NG, to generate test cases for your API.