当前位置:网站首页>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.
边栏推荐
- 工程制图-齿轮
- 计组错题集
- t conditional judgment statement and if loop
- 接口测试框架实战(三)| JSON 请求与响应断言
- 13.机器学习基础:数据预处理与特征工程
- Interface test practice | Detailed explanation of the difference between GET / POST requests
- Where is the value of testers
- 2022 the first of the new league henan (4) : zhengzhou university of light industry G - maze
- 技术分享 | 接口自动化测试中如何对xml 格式做断言验证?
- Shell编程的条件语句
猜你喜欢
OpenFOAM extracts equivalency and calculates area
深圳线下报名|StarRocks on AWS:如何对实时数仓进行极速统一分析
私域流量引流方法?分享购火爆的商业模式,你值得拥有
Redis缓存雪崩、缓存穿透、缓存击穿
接口测试框架实战(四)| 搞定 Schema 断言
移动流量的爆发式增长,社交电商如何选择商业模式
Kotlin-Flow常用封装类:StateFlow的使用
常见荧光染料修饰多种基团及其激发和发射波长数据一览数据
Concepts and Methods of Exploratory Testing
IDEC和泉触摸屏维修HG2F-SS22V HG4F软件通信分析
随机推荐
4.深度学习的几何解释与梯度的优化
【HMS core】【Ads Kit】华为广告——海外应用在国内测试正式广告无法展示
MySQL 出现 The table is full 的解决方法
接口和协议
12.机器学习基础:评估机器学习模型
私域流量时代来临,电商企业如何布局?
2022河南萌新联赛第(四)场:郑州轻工业大学 E - 睡大觉
RequestContextHolder
汇编题答案
OpenFOAM extracts equivalency and calculates area
3.张量运算
表的创建、修改与删除
索引创建、删除与使用
JS底层手写
Redis连接不上的报错解决方案汇总
在竞争白热化的电商行业,链动2+1为什么还有企业在用
1.一个神经网络示例
Oracle EMCC可以独立安装吗?还是必须安装到数据库服务器上?
计网试卷概念
Test drive: project management module - curd development project