当前位置:网站首页>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.
边栏推荐
猜你喜欢
那些让电子工程师崩溃瞬间,你经历了几个呢?
测开:项目管理模块-项目curd开发
DC-4靶场搭建及渗透实战详细过程(DC靶场系列)
在竞争白热化的电商行业,链动2+1为什么还有企业在用
种草一个让程序员男友编程时,记住一辈子的 IDEA 神仙插件!
Shenzhen Offline Registration|StarRocks on AWS: How to conduct rapid and unified analysis of real-time data warehouses
如何利用 Flutter 实现炫酷的 3D 卡片和帅气的 360° 展示效果
探索性测试的概念及方法
Windows 安装PostgreSQL
Dialog manager in the fourth chapter: the dialog message loop
随机推荐
unity2D横板游戏教程6-敌人AI以及受击动画
【Harmony OS】【ARK UI】ets使用startAbility或startAbilityForResult方式调起Ability
Test drive: project management module - curd development project
【Harmony OS】【FAQ】鸿蒙问题合集1
5.回顾简单的神经网络
closures in js
2022 the first of the new league henan (4) : zhengzhou university of light industry G - maze
【Harmony OS】【ARK UI】ETS 上下文基本操作
社交电商:链动2+1模式,为什么能在电商行业生存那么久?
接口测试如何准备测试数据
【精讲】利用原生js实现todolist
IDEC和泉触摸屏维修HG2F-SS22V HG4F软件通信分析
13.机器学习基础:数据预处理与特征工程
12.机器学习基础:评估机器学习模型
install ambari
自组织是管理者和成员的双向奔赴
DFS's complement to pruning
三丁基-巯基膦烷「tBuBrettPhos Pd(allyl)」OTf),1798782-17-8
10.预测房价:回归问题
数据库基本概述与SQL概述