ApiFrame--json和api断言

json介绍

json 介绍

jq:   --linux平台   | 官方文档:https://github.com/stedolan/jq
jsonPath: 文档:https://goessner.net/articles/JsonPath/
jsonschema生成器:https://app.quicktype.io/ | https://www.jsonschema.net/login

json使用

1. 前后端分离的系统,当某个接口变更时,接口的所有关联系统都需要变更,使用json对象可以解决这个问题。
   前后端传递json对象,各系统获取接口对象,对应系统关注自己的变更,其他系统一般不需要修改。
3. mysql5.7+支持json类型字段, 并可以快速从json串中解析数据

json和python dict区别

python dict: python词典
json:  json schema      schema: 规范

定位

领域DSL
xmlxpath
htmlcss selector + xapth
textregex
jsonjsonPath
assertionhamcrest

json和jsonpath

linux jq

curl '$(url)' -H 'xx'| jq '.data'
{
  "status": 7,
  "statusDesc": "入驻成功",
  "remark": ""
}

******---******

curl '$(url)' -H 'xx'| jq '.data.statusDesc'

"入驻成功"

python json

在json模块有4个方法

import json

json.loads()   #  将json数据转化成dict数据
json.load()    #  读取json文件数据,转成dict数据
json.dumps()   #  将dict数据转化成json数据
json.dump()    #  :将dict数据转化成json数据后写入json文件

python jsonpath

import jsonpath

data = {
    'success':"true",
    "code":"0000",
    "message":"null",
    "messageMap":{

    },
    "data":{
        "status":7,
        "statusDesc":"入驻成功",
        "remark":""
    },
    "refresh":"false"
}


p = jsonpath.jsonpath(data, "$.data.status")
print(p)

>[7]
import jsonpath


d = {
	"store": {
		"book": [{
				"category": "reference",
				"author": "Nigel Rees",
				"title": "Sayings of the Century",
				"price": 8.95
			}, {
				"category": "fiction",
				"author": "Evelyn Waugh",
				"title": "Sword of Honour",
				"price": 12.99
			}, {
				"category": "fiction",
				"author": "Herman Melville",
				"title": "Moby Dick",
				"isbn": "0-553-21311-3",
				"price": 8.99
			}, {
				"category": "fiction",
				"author": "J. R. R. Tolkien",
				"title": "The Lord of the Rings",
				"isbn": "0-395-19395-8",
				"price": 22.99
			}
		],
		"bicycle": {
			"color": "red",
			"price": 19.95
		}
	}
}


res = jsonpath.jsonpath(d, '$..book[?(@.title == "Sayings of the Century")].*')
print(res)

res2 = jsonpath.jsonpath(d, "$.store.book[?(@.category=='reference')].author")
print(res2)
print(type(res2))

>
['reference', 'Nigel Rees', 'Sayings of the Century', 8.95]
['Nigel Rees']
<class 'list'>

jsonschema

介绍

官方文档:http://json-schema.org/implementations.html
githua: https://github.com/Julian/jsonschema
参考文档; https://blog.csdn.net/u012219045/article/details/98872192

validate断言

from jsonschema import validate
import json


data = {
    "name" : "Eggs", 
    "price" : 123
    }

schema = {
     "type" : "object",
     "properties" : {
         "price" : {"type" : "number"},
         "name" : {"type" : "string"},
    },
    }

def test_json_schema():
    validate(instance=data, schema=schema)

schema代码生成


# Pydantic会自动从模型创建JSON Schema
from  pydantic import BaseModel, Schema

class MainModel(BaseModel):
    """
    docstring
    """
    snap: int = Schema(
        42,
        title='The Snap',
        description='this is the value of snap',
        gt=30,
        lt=50,
    )
    class Config:
        title = 'Main'

print(MainModel.schema())

>{'title': 'Main', 'description': 'docstring', 'type': 'object', 'properties': {'snap': {'title': 'The Snap', 'description': 'this is the value of snap', 'default': 42, 'exclusiveMinimum': 30, 'exclusiveMaximum': 50, 'type': 'integer'}}}

pyHamcrest

pyHamcrest简介

用于编写匹配器对象的框架。他提供了一套匹配符Matcher,这些匹配符更接近自然语言,可读性高,更加灵活。Hamcrest还有很好的可扩展性,能够创建自定义的匹配器。

Hamcrest支持多语言,pyHamcrest是支持python语言的第三方库

参考文档: https://www.cnblogs.com/tynam/p/12377658.html ,查看常见方法

官网: http://hamcrest.org/

pyHamcrest用法

自动化测试过程中,当基础断言功能无法满足要求时,常使用pyHamcrest断言
from hamcrest import *

def mul(a, b):
    return a * b


def test_mul():

    assert_that(mul(2, 2), equal_to(4))


def test_close():
    assert_that(0.1*0.1, close_to(0.01, 0.09))

def test_equal():
    assert_that(0.1*0.1, equal_to(0.010000000000000002))


def test_equal():
    assert_that(0.1*0.1, equal_to(0.010000000000000002))

def test_has():
    assert_that(['a', 'b', 'c'], has_items('c', 'a'))

def test_any():
    assert_that('c', any_of('c', 'a'))

def test_all():
    assert_that('qwerty', has_length(6))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值