当前位置:网站首页>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 jsonschemaUse 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.
边栏推荐
猜你喜欢

BIOTIN ALKYNE CAS:773888-45-2价格,供应商

【生物素叠氮化物|cas:908007-17-0】价格_厂家

接口管理工具YApi怎么用?颜值高、易管理、超好用

How many moments have you experienced the collapse of electronic engineers?

接口测试框架实战 | 流程封装与基于加密接口的测试用例设计

Dialog manager in the fourth chapter: the dialog message loop

OpenFOAM提取等职面并计算面积

UV 裂解的生物素-PEG2-叠氮|CAS:1192802-98-4生物素接头

工程制图第九章作业

Where is the value of testers
随机推荐
浏览器监听标签页关闭
【翻译】开发与生产中的Kubernetes修复成本对比
DC-4靶场搭建及渗透实战详细过程(DC靶场系列)
工程制图点的投影练习
js的垃圾回收机制
2022河南萌新联赛第(四)场:郑州轻工业大学 E - 睡大觉
社交电商如何做粉丝运营?云平台怎么选择商业模式?
OpenFOAM extracts equivalency and calculates area
Shell编程的条件语句
UV 裂解的生物素-PEG2-叠氮|CAS:1192802-98-4生物素接头
C#异步和多线程
MySQL 出现 The table is full 的解决方法
数字化时代,企业如何建立自身的云平台与商业模式的选择?
I ported GuiLite to STM32F4 board
Can Oracle EMCC be installed independently?Or does it have to be installed on the database server?
Where is the value of testers
6.神经网络剖析
v-text指令:设置标签内容
"Obs" start pushing flow failure: the Output. The StartStreamFailed call process
计组错题集