当前位置:网站首页>技术分享 | 接口自动化测试之JSON Schema模式该如何使用?
技术分享 | 接口自动化测试之JSON Schema模式该如何使用?
2022-08-08 19:39:00 【叶赫那拉 赫敏】
JSON Schema 模式是一个词汇表,可用于注释和验证 JSON 文档。在实际工作中,对接口返回值进行断言校验,除了常用字段的断言检测以外,还要对其他字段的类型进行检测。对返回的字段一个个写断言显然是非常耗时的,这个时候就需要一个模板,可以定义好数据类型和匹配条件,除了关键参数外,其余可直接通过此模板来断言,JSON Schema 可以完美实现这样的需求。
JSON Schema 官网:
环境准备
安装 JSON Schema 包
- Python 版本
pip install jsonschema
- Java 版本
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>3.0.1</version>
</dependency>
JSON Schema 的使用
JSON Schema 模板生成
首先要借助于 JSON Schema tool 的网站 JSON Schema Tool json 字符串复制到页面左边,然后点击 INFER SHCEMA,就会自动转换为 schema json 文件类型,会将每个地段的返回值类型都设置一个默认类型,在 pattern 中也可以写正则进行匹配。
点击“设置”按钮会出现各个类型返回值更详细的断言设置,这个就是 schema 最常用也是最实用的功能。也可以对每种类型的字段最更细化的区间值校验或者断言,例如长度、取值范围等。
点击复制按钮,可以将生成的 schema 模板保存下来。
实战练习
接下来会发起一个 post 请求,验证响应值中的 url 字段与 origin 字段是否都为 string 类型。
Python版本
import requests
from jsonschema import validate
def test_schema():
schema = {
"type": "object",
"properties": {
"url": {
"type": "string"
},
"origin": {
"type":"string"
}
}
}
r = requests.post("https://httpbin.ceshiren.com/post")
validate(instance=r.json(), schema=schema)
如果将 origin 的 type 写成 number ,则会出现报错:
import requests
from jsonschema import validate
def test_schema():
schema = {
"type": "object",
"properties": {
"url": {
"type": "string"
},
"origin": {
"type":"number"
}
}
}
r = requests.post("https://httpbin.ceshiren.com/post")
validate(instance=r.json(), schema=schema)
返回报错信息
> raise error
E jsonschema.exceptions.ValidationError: 'xxx.xxx.xxx.xxx' is not of type 'number'
E Failed validating 'type' in schema['properties']['origin']:
E {'type': 'number'}
同理,若将 url 的 type 改为 number,也会有报错提示。
> raise error
E jsonschema.exceptions.ValidationError: 'https://httpbin.ceshiren.com/post' is not of type 'number'
E Failed validating 'type' in schema['properties']['url']:
E {'type': 'number'}
Java 版本
JsonValidator.json 文件中存放校验文件,校验响应值中的 url 字段与 origin 字段是否都为 string 类型,文件内容为:
"type": "object",
"properties": {
"url": {
"type": "string"
},
"origin": {
"type":"string"
}
}
}
同 Python 版本一致,以下代码校验响应值是否符合 JsonValidator.json 文件中规定的格式要求。
import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;
import static io.restassured.RestAssured.*;
public class Requests {
public static void main(String[] args) {
//定义请求头信息的contentType为application/json
given().when().
post("https://httpbin.ceshiren.com/post").
then().assertThat().
body(matchesJsonSchemaInClasspath("JsonValidator.json"));
}
}
边栏推荐
- Codeforces Round #712 (Div. 2)(CD)
- Ansible自动化运维工具(二)playbook剧本
- PyTorch入门:(二)Tensorboard的使用
- Mei cole studio OpenHarmony equipment development training notes - the first learn notes
- openEuler 资源利用率提升之道02:典型应用下的效果
- nyoj714 Card Trick (The 6th Henan Province Programming Contest)
- 能力一般,却可以大厂随便横跳?强在哪里?
- nyoj 712 Exploring treasure
- Salesforce开发之 apex操作批准过程(Approval Process)
- 黑猫带你学Makefile第5篇:Makefile中函数的使用
猜你喜欢
随机推荐
n个数取出r个数排列
nyoj714 Card Trick (The 6th Henan Province Programming Contest)
书法家唐效奇
即将开幕!阿里云飞天技术峰会邀您一同探秘云原生最佳实践
laravel run scheduler command on weekdays (except holidays)
From interview to autism, five rounds of interviews for byte software testing post, four hours of soul torture...
C language elementary - structure
智驾科技完成C1轮融资,此前2轮已融4.5亿元
网络工程师怎么系统性学习?这份网工资料包帮你解决
Group DETR:分组一对多匹配是加速DETR收敛的关键
小白如何购买基金产品?
FastDFS distributed file system
虚假信息处理最新有何进展?KDD2022《打击错误信息和应对媒体偏见》教程,161页ppt
Ansible自动化运维工具(二)playbook剧本
期货开户哪家公司好,要正规安全的
如何在EasyDSS中使用ffmpeg实现点播视频的拼接与合成?
hdu1495 非常可乐 (广搜)
Ability in general, but it can be large horizontal jump freely?Where is the better?
Salesforce开发之 如何实现DML操作时,当前用户跳过验证规则(Validation Rule)
Mei cole studio OpenHarmony equipment development training notes - the first learn notes