当前位置:网站首页>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】
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-assured json-schema-validator 3.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>
边栏推荐
猜你喜欢

DSPE-PEG-Silane, DSPE-PEG-SIL, phospholipid-polyethylene glycol-silane modified silica particles

在VMware上安装win虚拟机

LoRa Basics无线通信技术和应用案例详解

knn到底咋回事?

Visual studio 2022 debugging skills introduction

Don't tell me to play, I'm taking the PMP exam: what you need to know about choosing an institution for the PMP exam

APP automation test framework - UiAutomator2 introductory

Word第一页空白页怎么删除?删除Word第一页空白页方法教程

消防安全培训|“蓝朋友”,开课了!

Sudoku | Backtrack-7
随机推荐
ACM MM 2022 | Cloud2Sketch: 长空云作画,AI笔生花
CVPR22 Oral | shunt through multi-scale token polymerization from attention, code is open source
cad图纸怎么复制到word文档里面?Word里插CAD图怎么弄?
RHEL7系统修复rm -rf /boot /etc/fstab
上海控安SmartRocket系列产品推介(三):SmartRocket iVerifier计算机联锁系统验证工具
MySQL:错误1153(08S01):得到的数据包大于“ max_allowed_packet”字节
Tensorflow中使用convert_to_tensor去指定数据的类型
TF使用constant生成数据
Technology Sharing | How to Handle Header Cookies in Interface Automation Testing
4D Summary: 38 Knowledge Points of Distributed Systems
微软word怎么转换成pdf文件?微软word转换为pdf格式的方法
什么是IDE(集成开发环境)?
C语言预处理命令是什么?
论文解读(DropEdge)《DropEdge: Towards Deep Graph Convolutional Networks on Node Classification》
字符串哈希(2014 SERC J题)
linux定时执行sql文件[通俗易懂]
PMP daily practice | didn't lost a 8.9 (including agile + multi-select)
什么是源文件?
Unity2D_线框材质
The round functions in the np, ceil function and floor function