python接口自动化-断言神器Jsonschema实战演示

断言神器Jsonschema实战演示

待测接口【查虚拟用户信息】:

https://fakerapi.it/api/v1/persons?_locale=en_US

返回数据【返回了10个虚拟用户信息】:

{
	"status": "OK",
	"code": 200,
	"total": 10,
	"data": [{
		"id": 1,
		"firstname": "Lawrence",
		"lastname": "Wiza",
		"email": "bluettgen@gmail.com",
		"phone": "+9133037867451",
		"birthday": "1990-01-30",
		"gender": "male",
		"address": {
			"id": 0,
			"street": "29403 Casimer Common",
			"streetName": "Celia Coves",
			"buildingNumber": "53647",
			"city": "Lehnertown",
			"zipcode": "44546-6952",
			"country": "Iraq",
			"county_code": "VA",
			"latitude": -60.958705,
			"longitude": 95.722736
		},
		"website": "http:\/\/weissnat.org",
		"image": "http:\/\/placeimg.com\/640\/480\/people"
	}, {
		"id": 2,
		"firstname": "Frederik",
		"lastname": "Langworth",
		"email": "alex63@hotmail.com",
		"phone": "+2107743874660",
		"birthday": "2017-06-29",
		"gender": "male",
		"address": {
			"id": 0,
			"street": "9650 Kyler Club Suite 327",
			"streetName": "Pouros Common",
			"buildingNumber": "73768",
			"city": "East Alexys",
			"zipcode": "47791-9319",
			"country": "Saint Vincent and the Grenadines",
			"county_code": "TH",
			"latitude": 33.975616,
			"longitude": -16.268378
		},
		"website": "http:\/\/feeney.org",
		"image": "http:\/\/placeimg.com\/640\/480\/people"
	}, {
		"id": 3,
		"firstname": "Autumn",
		"lastname": "Brown",
		"email": "rosenbaum.sarah@bergnaum.biz",
		"phone": "+4973358445279",
		"birthday": "1991-01-15",
		"gender": "female",
		"address": {
			"id": 0,
			"street": "387 Marcelle Radial Apt. 808",
			"streetName": "August Brook",
			"buildingNumber": "396",
			"city": "Florenceshire",
			"zipcode": "01214-3585",
			"country": "Sweden",
			"county_code": "ZM",
			"latitude": 51.50018,
			"longitude": 121.697669
		},
		"website": "http:\/\/okon.info",
		"image": "http:\/\/placeimg.com\/640\/480\/people"
	}, {
		"id": 4,
		"firstname": "Elijah",
		"lastname": "Walker",
		"email": "vincent39@gmail.com",
		"phone": "+8266714390476",
		"birthday": "1986-10-22",
		"gender": "male",
		"address": {
			"id": 0,
			"street": "86742 Ledner Burgs",
			"streetName": "Armani Points",
			"buildingNumber": "83028",
			"city": "Savionfurt",
			"zipcode": "14329",
			"country": "Saint Barthelemy",
			"county_code": "CD",
			"latitude": 46.993589,
			"longitude": 71.978266
		},
		"website": "http:\/\/kunze.net",
		"image": "http:\/\/placeimg.com\/640\/480\/people"
	}, {
		"id": 5,
		"firstname": "Casimer",
		"lastname": "Hessel",
		"email": "ghyatt@lehner.net",
		"phone": "+5007692591147",
		"birthday": "1954-04-03",
		"gender": "male",
		"address": {
			"id": 0,
			"street": "620 Parisian Place Apt. 366",
			"streetName": "Bailey Springs",
			"buildingNumber": "1995",
			"city": "North Victoriaberg",
			"zipcode": "92345-3091",
			"country": "Cayman Islands",
			"county_code": "MR",
			"latitude": -61.264908,
			"longitude": 118.779855
		},
		"website": "http:\/\/prohaska.com",
		"image": "http:\/\/placeimg.com\/640\/480\/people"
	}, {
		"id": 6,
		"firstname": "Dianna",
		"lastname": "Kuphal",
		"email": "sigmund39@yahoo.com",
		"phone": "+2361567863712",
		"birthday": "1975-07-28",
		"gender": "female",
		"address": {
			"id": 0,
			"street": "4160 Sylvester Junctions Apt. 214",
			"streetName": "Everardo Springs",
			"buildingNumber": "50064",
			"city": "North Hiltonfort",
			"zipcode": "64534-0311",
			"country": "Oman",
			"county_code": "FI",
			"latitude": 45.528019,
			"longitude": -122.378923
		},
		"website": "http:\/\/oconnell.com",
		"image": "http:\/\/placeimg.com\/640\/480\/people"
	}, {
		"id": 7,
		"firstname": "Drew",
		"lastname": "Kovacek",
		"email": "kreiger.yessenia@dicki.net",
		"phone": "+5498975090296",
		"birthday": "1935-08-26",
		"gender": "female",
		"address": {
			"id": 0,
			"street": "294 Russel Squares Apt. 358",
			"streetName": "Goldner Vista",
			"buildingNumber": "39777",
			"city": "East Kadin",
			"zipcode": "12816-1318",
			"country": "Oman",
			"county_code": "SC",
			"latitude": 11.544548,
			"longitude": 150.734884
		},
		"website": "http:\/\/will.info",
		"image": "http:\/\/placeimg.com\/640\/480\/people"
	}, {
		"id": 8,
		"firstname": "Dewitt",
		"lastname": "Bernhard",
		"email": "mozell40@hotmail.com",
		"phone": "+4106258963070",
		"birthday": "1948-11-10",
		"gender": "male",
		"address": {
			"id": 0,
			"street": "20079 Gerhard Prairie",
			"streetName": "Littel Summit",
			"buildingNumber": "304",
			"city": "Imogeneberg",
			"zipcode": "11183",
			"country": "Gabon",
			"county_code": "FI",
			"latitude": -60.239963,
			"longitude": 13.021135
		},
		"website": "http:\/\/kertzmann.com",
		"image": "http:\/\/placeimg.com\/640\/480\/people"
	}, {
		"id": 9,
		"firstname": "Polly",
		"lastname": "Windler",
		"email": "anais.mann@hotmail.com",
		"phone": "+2464981550239",
		"birthday": "2022-01-10",
		"gender": "female",
		"address": {
			"id": 0,
			"street": "98742 Kailey Street Apt. 700",
			"streetName": "Barrows Estates",
			"buildingNumber": "4713",
			"city": "East Effietown",
			"zipcode": "57710",
			"country": "United Kingdom",
			"county_code": "AU",
			"latitude": 61.120647,
			"longitude": -67.709427
		},
		"website": "http:\/\/kutch.com",
		"image": "http:\/\/placeimg.com\/640\/480\/people"
	}, {
		"id": 10,
		"firstname": "Talia",
		"lastname": "Emard",
		"email": "corwin.christiana@mueller.com",
		"phone": "+6175621695654",
		"birthday": "2011-04-05",
		"gender": "female",
		"address": {
			"id": 0,
			"street": "3650 Marley Avenue Apt. 473",
			"streetName": "Sigmund Manor",
			"buildingNumber": "6684",
			"city": "New Gwendolynport",
			"zipcode": "30982",
			"country": "Dominican Republic",
			"county_code": "ZW",
			"latitude": -28.466573,
			"longitude": -73.658631
		},
		"website": "http:\/\/kub.net",
		"image": "http:\/\/placeimg.com\/640\/480\/people"
	}]
}

针对以上response结构,定义JSON Schema模版

define_my_json_schema = {

	"$schema": "https://json-schema.org/draft/2020-12/schema",
	"required": ["status", "code", "total", "data"],
	"type": "object",
	"properties": {
		"status": {
			"type": "string",
			"pattern": "OK"
		},
		"code": {
			"type": "number",
			"minimum": 200,
			"maximum": 204
		},
		"total": {
			"type": "integer"
		},
		"data": {
			"type": "array",
			"items": {
				"type": "object",
				"properties": {
					"id": {
						"type": "integer"
					},
					"firstname": {
						"type": "string"
					},
					"lastname": {
						"type": "string"
					},
					"email": {
						"type": "string"
					},
					"phone": {
						"type": "string"
					},
					"birthday": {
						"type": "string"
					},
					"gender": {
						"type": "string"
					},
					"address": {
						"type": "object"
					},
					"website": {
						"type": "string"
					},
					"image": {
						"type": "string"
					},

				}
			}
		}
	}
}

使用validate方法进行验证:

from jsonschema import validate
validate(response.json(), define_my_json_schema)
"""不报错即检验通过"""

附上完整的测试代码:

from jsonschema import validate
from requests import request


define_my_json_schema = {

	"$schema": "https://json-schema.org/draft/2020-12/schema",
	"required": ["status", "code", "total", "data"],
	"type": "object",
	"properties": {
		"status": {
			"type": "string",
			"pattern": "OK"
		},
		"code": {
			"type": "number",
			"minimum": 200,
			"maximum": 204
		},
		"total": {
			"type": "integer"
		},
		"data": {
			"type": "array",
			"items": {
				"type": "object",
				"properties": {
					"id": {
						"type": "integer"
					},
					"firstname": {
						"type": "string"
					},
					"lastname": {
						"type": "string"
					},
					"email": {
						"type": "string"
					},
					"phone": {
						"type": "string"
					},
					"birthday": {
						"type": "string"
					},
					"gender": {
						"type": "string"
					},
					"address": {
						"type": "object"
					},
					"website": {
						"type": "string"
					},
					"image": {
						"type": "string"
					},

				}
			}
		}
	}
}



if __name__ == "__main__":
    res=request(method="get", url="https://fakerapi.it/api/v1/persons?_locale=en_US")
    validate(res.json(), define_my_json_schema)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木法星人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值