当前位置:网站首页>Technology Sharing | How to use the JSON Schema mode of interface automation testing?

Technology Sharing | How to use the JSON Schema mode of interface automation testing?

2022-08-09 23:13:00 hog_ceshiren

Original link

The JSON Schema schema is a vocabulary that can be used to annotate and validate JSON documents.In actual work, to perform assertion verification on the interface return value, in addition to the assertion detection of common fields, the types of other fields should also be detected.It is obviously very time-consuming to write assertions to the returned fields one by one. At this time, a template is needed to define the data type and matching conditions. Except for the key parameters, the rest can be asserted directly through this template. JSON Schema can be perfectly implemented.such demand.

JSON Schema official website:

http://json-schema.org/implementations.html

Environment preparation

Install the JSON Schema package

  • Python Version
pip install jsonschema
  • Java version
io.rest-assuredjson-schema-validator3.0.1

Use of JSON Schema

JSON Schema template generation

First, use the website https://www.jsonschema.net/ of the JSON Schema tool, copy the returned json string to the left side of the page, and then click INFER SHCEMA, it will be automatically converted to the schema json file type, and theThe return value type of each lot is set to a default type, and a regular pattern can also be written in the pattern to match.
[External link image transfer failed, the source site may have anti-leech mechanism, it is recommended to save the image and upload it directly (img-K12bEfqU-1659923704774)(upload://rWFXH62wcBJrNH0xcCf6O7w1UJA.png)]

Click the "Settings" button to display more detailed assertion settings for each type of return value. This is the most common and practical function of schema.You can also check or assert the most refined interval value of each type of field, such as length, value range, etc.

Click the copy button to save the generated schema template.

Practical practice

Next, a post request will be initiated to verify that both the url field and the origin field in the response value are of type string.

Python version

import requestsfrom jsonschema import validatedef test_schema():schema = {"type": "object","properties": {"url": {"type": "string"},"origin": {"type":"string"}}}r = requests.post("https://httpbin.ceshiren.com/post")validate(instance=r.json(), schema=schema)

If the type of origin is written as number , an error will occur:

import requestsfrom jsonschema import validatedef test_schema():schema = {"type": "object","properties": {"url": {"type": "string"},"origin": {"type":"number"}}}r = requests.post("https://httpbin.ceshiren.com/post")validate(instance=r.json(), schema=schema)

Return error message

> raise errorE jsonschema.exceptions.ValidationError: 'xxx.xxx.xxx.xxx' is not of type 'number'E Failed validating 'type' in schema['properties']['origin']:E {'type': 'number'}

Similarly, if the type of the url is changed to number, an error message will also appear.

> raise errorE jsonschema.exceptions.ValidationError: 'https://httpbin.ceshiren.com/post' is not of type 'number'E Failed validating 'type' in schema['properties']['url']:E {'type': 'number'}

Java version

The verification file is stored in the JsonValidator.json file to verify whether the url field and the origin field in the response value are both of type string. The content of the file is:

 "type": "object","properties": {"url": {"type": "string"},"origin": {"type":"string"}}}

Consistent with the Python version, the following code verifies whether the response value conforms to the format requirements specified in the JsonValidator.json file.

import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;import static io.restassured.RestAssured.*;public class Requests {public static void main(String[] args) {//Define the contentType of the request header information as application/jsongiven().when().post("https://httpbin.ceshiren.com/post").then().assertThat().body(matchesJsonSchemaInClasspath("JsonValidator.json"));}}

️ Copy the "link below" to improve the core competitiveness of the test!

Hello, if you like this article, remember to click "Like"!Your support is very important~() PS: If you have any questions, please contact us

More technical articles to share and free materials to receivep>

原网站

版权声明
本文为[hog_ceshiren]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091951025109.html