Application of JsonSchema in interface testing

Part 01

What is JSON Schema? 

JSON Schema is a specification for describing and validating JSON data structures. It provides a structured way to define the expected structure, type, format, constraints, etc. of JSON data. Through JSON Schema, we can ensure that the received JSON data conforms to the expected specification and verify its validity.

Here is a short JSON Schema example:

Part 02

Common keywords and descriptions of JSON Schema  

JSON Schema provides a series of keywords for describing and validating the structure and constraints of JSON data. Here are some common JSON Schema keywords and their descriptions:

By using these keywords, we can define data structures that meet expectations, and perform validity verification and constraint checks on the data. In the interface test , the reasonable application of these keywords can improve the accuracy and reliability of the test, and ensure that the data returned by the interface meets the expected requirements.

Part 03

Application of JSON Schema in interface testing 

Postman is a powerful and common interface testing tool that provides many functions and features. The following will introduce how to apply JSON Schema in Postman for interface testing.

1) Define JSON Schema: Before starting the interface test, you first need to define JSON Schema to describe the structure and constraints of the data returned by the interface. A JSON file can be created using any text editor and defines the JSON Schema as required by the interface.

The JSON Schema above defines an object that contains three properties: age, name, and email. Each attribute has a specific type and constraints.

· The age attribute is a numeric type and must be greater than or equal to 0.

·  The name attribute is a string type.

·  The email attribute is a string type and should conform to the email format.

In addition, the JSON Schema uses the required keyword to specify the required attributes, that is, age, name, and email are all required attributes, and the lack of any one will cause the verification to fail.

2) Create a request: Open Postman and create a new request, enter the requested URL, request method and other necessary parameters. Make sure to select the correct request method (GET, POST, PUT, etc.) and the correct URL.

3) Add a test script: Select the "Tests" tab in the request, and add JavaScript code there as a test script. The test script will be used to verify whether the data returned by the interface complies with the JSON Schema definition.

Here we use the pm.response.json() method to get the JSON data returned by the interface and compare it with the predefined JSON Schema. Use the tv4.validate() method to verify whether the jsonData conforms to the schema definition. The assertion will return true if the data matches the Schema.

4) Send request and run test: Now, you can click Send request and run the test script on the response. The test script will verify whether the data returned by the interface conforms to the JSON Schema definition, and feedback the corresponding test results according to the results.

By using JSON Schema in Postman for interface testing, we can easily verify whether the data returned by the interface meets expectations, verify whether the integrity, type, and format of the data returned by the interface are correct, and effectively ensure the accuracy of data transmission and interaction sex and consistency.

In addition, in addition to using JSON Schema in daily interface testing, we also use it in automated testing . There are open source third-party JSON Schema validation libraries in various languages, such as gojsonschema in Go language and jsonschema in Python .

Finally, I would like to thank everyone who has read my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, you can take it away if you need it:

These materials should be the most comprehensive and complete preparation warehouse for [ software testing ] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey, and I hope it can help you! Partners can click the small card below to receive 

Guess you like

Origin blog.csdn.net/okcross0/article/details/132473800