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

那些让电子工程师崩溃瞬间,你经历了几个呢?

修饰生物素DIAZO-生物素-PEG3-DBCO|重氮-生物素-三聚乙二醇-二苯基环辛炔

自考六级雅思托福备战之路

接口测试实战| GET/POST 请求区别详解

测试人员的价值体现在哪里

Record some bugs encountered - when mapstruct and lombok are used at the same time, the problem of data loss when converting entity classes

Concepts and Methods of Exploratory Testing

Where is the value of testers

普乐蛙VR台风体验馆厂家VR防震减灾模拟VR沉浸式体验设备

I ported GuiLite to STM32F4 board
随机推荐
11.机器学习基础:机器学习的四个分支
安装ambari
Windows 安装PostgreSQL
自组织是管理者和成员的双向奔赴
接口测试框架实战(一) | Requests 与接口请求构造
接口和协议
redis键值出现 xacxedx00x05tx00&的解决方法
rosbag工具plotjuggler无法打开rosbag的问题
DDL操作数据库、表、列
6.神经网络剖析
Record some bugs encountered - when mapstruct and lombok are used at the same time, the problem of data loss when converting entity classes
mysql 创建索引的三种方式
How to use the interface management tool YApi?Beautiful, easy to manage, super easy to use
"Obs" start pushing flow failure: the Output. The StartStreamFailed call process
工程制图-齿轮
数据库基本概述与SQL概述
传统企业如何转型社交电商,泰山众筹的玩法有哪些?
7.Keras开发简介
在竞争白热化的电商行业,链动2+1为什么还有企业在用
leetcode刷题学习之路