当前位置:网站首页>Interface Test Framework Practice (4) | Get Schema Assertion
Interface Test Framework Practice (4) | Get Schema Assertion
2022-08-03 04:34:00 【Yehenara Hermione】
JSON Schema introduction and installation
JSON Schema is a format for describing the structure of JSON data, and a JSON Schema schema is a vocabulary.Through JSON Schema, you can annotate JSON fields and field data types and other information.
In actual work, the assertion check is performed on the interface return value. In addition to the assertion detection of common fields, the types of other fields are also checked.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 directly asserted through this template. Json Schema can be perfectly implemented.such demand.By verifying the JSON Schema, you can determine whether the Response conforms to the contract.
A JSON-formatted data, usually composed of one or more of the following data types:
- string
- Numeric(integer, number)
- object
- array
- boolean
- null
The above 6 data types in JSON Schema have corresponding attributes to describe them.
Json Schema official website: http://json-schema.org/implementations.html 8
Environmental preparation
Install the JSON Schema package:
pip install jsonschema
Use of JSON Schema
JsonSchema template generation
- First, use the Json Schema Tool website:
https://www.jsonschema.net/ 11
Copy the returned JSON string to the left side of the page, then click INFER SHCEMA, it will be automatically converted to the Schema JSON file type, and the return value type of each field will be set to a default type; you can also write regular patterns in the patternto match:
- 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, and it can also verify or assert the most detailed interval value for each type of field., such as length, value range, etc.
- Click the Copy button to save the generated Schema template.
JSON Schema verification case
The following is an example of JSON Schema, which only has two important fields name and price.This schema specifies that name must be of type string and price must be of type number.Use JSON Schema for validation, use the validate method, and enter a data whose name is Eggs
and price is 34.99
for validation:
def test_schema(self):schema = {"type": "object","properties": {"price": {"type": "number"},"name": {"type":"string"},},}validate(instance={"name": "Eggs", "price": 34.99},schema=schema)
If the number is written as a string, an error will occur:
>>> from jsonschema import validate>>> schema = {... "type": "object",... "properties": {... "price": {"type": "string"},... "name": {"type":"string"},... },... }>>> validate(instance={"name": "Eggs", "price": 34.99},schema=schema)
Return error message:
Traceback (most recent call last):File "", line 1, in File "/Users/lixu/Library/Python/3.7/lib/python/site-packages/jsonschema/validators.py",\line 934, in validateraise errorjsonschema.exceptions.ValidationError: 34.99 is not of type 'string'
Above, JSON Schema, as a data verification tool for interface testing, makes the work more efficient, and also better implements interface monitoring, so that you can know where there is a problem and correct it in time.More will be discussed in subsequent chapters.
边栏推荐
猜你喜欢
随机推荐
Jmeter 模拟多用户登录的两种方法
数据库基本概述与SQL概述
EssilorLuxottica借助Boomi的智能集成平台实现订单处理的现代化
v-on指令:为元素绑定事件
js中的闭包
BIOTIN ALKYNE CAS:773888-45-2价格,供应商
Redis缓存雪崩、缓存穿透、缓存击穿
Record some bugs encountered - when mapstruct and lombok are used at the same time, the problem of data loss when converting entity classes
Live | StarRocks technology insider: low base dictionary global optimization
刚上线就狂吸70W粉,新型商业模式“分享购”来了,你知道吗?
v-text指令:设置标签内容
打破传统电商格局,新型社交电商到底有什么优点?
移动流量的爆发式增长,社交电商如何选择商业模式
接口测试框架实战(二)| 接口请求断言
Browser listens for tab closing
Shell条件语句判断
path development介绍
工程水文学试题库
I ported GuiLite to STM32F4 board
测试人员的价值体现在哪里