美文网首页
自动化接口测试JSON Schema断言应用:生成json sc

自动化接口测试JSON Schema断言应用:生成json sc

作者: 乔小布_e1c5 | 来源:发表于2021-02-02 15:41 被阅读0次

什么是 JSON Schema?

  • JSON Schema is a vocabulary that allows you to annotate and validate JSON documents.
  • http://json-schema.org/
  • 示例,简而言之,json schema就是json数据格式的一个描述,可以用来作实例验证。
// JSON Example
{
  "message": "操作成功",
  "responseCode": "0",
  "hasError": false,
  "data": {
    "id": 100123456
  }
}
// 与之对应的JSON Schema
{
    "$schema": "http://json-schema.org/schema#",
    "type": "object",
    "properties": {
        "message": {
            "type": "string"
        },
        "responseCode": {
            "type": "string"
        },
        "hasError": {
            "type": "boolean"
        },
        "data": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "integer"
                }
            },
            "required": ["id"]
        }
    },
    "required": ["data", "hasError", "message", "responseCode"]
}

接口自动化测试利用 JSON Schema 断言 Response?

接口自动化测试一些常用断言方法:

  • status_code:response.status_code == 200
  • headers.xxx:Content-Type == application/json
  • content.xxx:content.responseCode == "0"、content.message == "操作成功"

今天来个不一样的,Json schema Validate:

#!/usr/bin/env python
# -*- encoding: utf-8 -*-

from jsonschema import validate

#! 正常的返回值
resp_pass = {
    "message": "操作成功",
    "responseCode": "0",
    "hasError": False,
    "data": {
        "id": 100120384
    }
}
#! 错误的返回值,没有返回Data ID
resp_fail = {
    "message": "操作成功",
    "responseCode": "0",
    "hasError": False,
    "data": {}
}
#! 预期Schema
resp_schema = {
    "$schema": "http://json-schema.org/schema#",
    "type": "object",
    "properties": {
        "message": {
            "type": "string"
        },
        "responseCode": {
            "type": "string"
        },
        "hasError": {
            "type": "boolean"
        },
        "data": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "integer"
                }
            },
            "required": ["id"]
        }
    },
    "required": ["data", "hasError", "message", "responseCode"]
}
#! 验证成功
try:
    validate(resp_pass, resp_schema)
except Exception as e:
    print('An exception occurred: ', e)

#! 验证失败
try:
    validate(resp_fail, resp_schema)
except Exception as e:
    print('An exception occurred: ', e)

以上执行结果为:

# 看得懂哈,不解释了 :)
An exception occurred:  'id' is a required property

Failed validating 'required' in schema['properties']['data']:
    {'properties': {'id': {'type': 'integer'}},
     'required': ['id'],
     'type': 'object'}

On instance['data']:
    {}
json schema validate fail

问:这么好用,怎么写预期的 json schema呢?

https://www.liquid-technologies.com/online-json-to-schema-converter

可以”硬写“也可以在线转换,输入预期的返回值json,转换成对应的 json schema


online-json-to-schema-converter

进阶:初次自动生成 JSON Schema用于后续断言?

以上,我们在写接口Case的时候还需要打开额外的网站去转换 json to json schema,有没有更简单的方式?

#!/usr/bin/env python
# -*- encoding: utf-8 -*-

from genson import SchemaBuilder

#! 正确的返回值
resp_pass = {
    "message": "操作成功",
    "responseCode": "0",
    "hasError": False,
    "data": {
        "id": 100120384
    }
}

#! 通过正确的返回值生成预期Json schema
builder = SchemaBuilder()
builder.add_object(resp_pass)
resp_schema = builder.to_json()

print(resp_schema)
 """ 输出结果
{
    "$schema": "http://json-schema.org/schema#",
    "type": "object",
    "properties": {
        "message": {
            "type": "string"
        },
        "responseCode": {
            "type": "string"
        },
        "hasError": {
            "type": "boolean"
        },
        "data": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "integer"
                }
            },
            "required": ["id"]
        }
    },
    "required": ["data", "hasError", "message", "responseCode"]
}
 """

我们可以把预期的Json Schema 存在本地(比如CSV或者数据库中),validate时先查询有没有expect schema,有就取出来与response做validation,没有就自动用当前的response生成并保存expect schema(so调试时要确保用正确的返回值生成),后续都拿这个expect schema来validate。把这个过程写成一个方法一直复用,是不是又省时又省力?

from jsonschema import validate
from genson import SchemaBuilder

# 伪代码
def validate_jsonschema(resp_json, expect_flag):
    """
    resp_json:response 返回值
    expect_flag:去数据库查询对应期望schemat的一个flag,通常用接口名

    创建schema逻辑
    1. 数据库查询是否有对应flag(对应接口)的期望schema数据;
    2. if 有: 直接拿来跟response的json数据做validate
    3. if 无: 插入当前这条的响应json数据对应的schema进数据库,assert True! 所以用例写好调试正常之后,第一次运行确保response正确 然后生成对应的schema并保存
    """

    sql_expect = f"SELECT expect_schema from resp_schema WHERE flag ='{expect_flag}'"
    with MySqlDB() as db:
        expect_schema = db.fetch_one(sql_expect)

    if isinstance(resp_json, dict):
        # ? 判断response content为dict(json)格式,否则直接False
        if expect_schema:
            # ? 判断是否存在预期schema
            try:
                expect_schema = json.loads(expect_schema.get('expect_schema'))
                validate(resp_json, expect_schema)
            except Exception as e:
                logger.log_error(e.text)
                assert False
                # ? ValidationFailure,Fail the case
        else:
            builder = SchemaBuilder()
            builder.add_object(resp_json)
            expect_schema = builder.to_json(indent=2, ensure_ascii=False)
            resp_schema = json.dumps(resp_json, indent=2, ensure_ascii=False)
            # ? 将respons json转换成expect schema,记录下来
            sql_insert = "INSERT INTO resp_schema VALUES (%s, %s, %s)"
            with MySqlDB() as db:
                db.create_one(sql_insert,
                              (expect_flag, expect_schema, resp_schema))
        assert True
    else:
        assert False
        # ! resp 非dict格式Fail

相关文章

网友评论

      本文标题:自动化接口测试JSON Schema断言应用:生成json sc

      本文链接:https://www.haomeiwen.com/subject/olyytltx.html