接口自动化测试如何灵活地验证动态变化的response返回数据

嗨,我是兰若,很多小伙伴在针对动态返回的response,不知道怎么断言,今天教给大家几种方式,可以在接口自动化测试时确保测试的有效性和灵活性:

1. 断言静态字段

首先,您可以断言一些静态字段的值,这些字段的值是预期不变的。例如,您可以检查 status_codemessage 字段:

python 复制代码
response_data = response.json()

assert response_data['status_code'] == 6000
assert response_data['message'] == '成功'

2. 检查动态字段的结构和类型

对于动态字段,您可以检查其结构和类型,而不关心具体的值。例如,您可以验证 gameRecordList 的存在以及每个记录的字段类型:

python 复制代码
game_record_list = response_data['data']['gameRecordList']

# 确保 gameRecordList 是一个列表
assert isinstance(game_record_list, list)

# 检查每个记录的结构
for record in game_record_list:
    assert 'data' in record
    assert isinstance(record['data'], list)
    assert 'day' in record

3. 使用正则表达式验证动态字段

如果某些字段的格式是固定的(例如 ID 或时间戳),您可以使用正则表达式进行验证:

python 复制代码
import re

for record in game_record_list:
    for item in record['data']:
        # 验证 ID 格式
        assert re.match(r'^YBTY_\\d+_YB$', item['id'])
        # 验证时间格式
        assert re.match(r'^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$', item['betTime'])

4. 检查特定字段的值范围或条件

如果您知道某些字段的值应该在特定范围内,可以进行范围检查:

python 复制代码
for record in game_record_list:
    for item in record['data']:
        # 验证赔率在合理范围内
        assert 1.0 <= item['odds'] <= 3.0

5. 使用 JSON Schema 验证

使用 JSON Schema 来验证响应的结构和类型,而不关心具体的值。示例:

python 复制代码
from jsonschema import validate, ValidationError

schema = {
    "type": "object",
    "properties": {
        "status_code": {"type": "integer"},
        "message": {"type": "string"},
        "data": {
            "type": "object",
            "properties": {
                "gameRecordList": {
                    "type": "array",
                    "items": {
                        "type": "object",
                        "properties": {
                            "data": {
                                "type": "array",
                                "items": {
                                    "type": "object",
                                    "properties": {
                                        "id": {"type": "string"},
                                        "siteId": {"type": "integer"},
                                        "memberId": {"type": "integer"},
                                        "memberName": {"type": "string"},
                                        "billNo": {"type": "string"},
                                        "gameType": {"type": "integer"},
                                        "gameName": {"type": "string"},
                                        "odds": {"type": "number"},
                                        "betTime": {"type": "string"},
                                        # 其他字段...
                                    },
                                    "required": ["id", "siteId", "memberId", "memberName", "billNo", "gameType", "gameName", "odds", "betTime"]
                                }
                            },
                            "day": {"type": "string"}
                        },
                        "required": ["data", "day"]
                    }
                },
                "page": {"type": "integer"},
                "pageNum": {"type": "integer"},
                "pageSize": {"type": "integer"}
            },
            "required": ["gameRecordList", "page", "pageNum", "pageSize"]
        }
    },
    "required": ["status_code", "message", "data"]
}

try:
    validate(instance=response_data, schema=schema)
except ValidationError as e:
    print(f"Validation error: {e}")

总结

通过以上方法,可以灵活地验证动态变化的数据,确保接口返回的结构和类型符合预期。

相关推荐
CodeOfCC1 小时前
c语言 封装跨平台线程头文件
linux·c语言·windows
momo卡2 小时前
MinGW-w64的安装详细步骤(c_c++的编译器gcc、g++的windows版,win10、win11真实可用)
c语言·c++·windows
南林yan14 小时前
DLL动态库实现文件遍历功能(Windows编程)
windows
Mike_66614 小时前
win10安装WSL2、Ubuntu24.04
windows·ubuntu·wsl2
liulun15 小时前
Skia如何绘制几何图形
c++·windows
old_power15 小时前
UCRT 和 MSVC 的区别(Windows 平台上 C/C++ 开发相关)
c语言·c++·windows
扛枪的书生15 小时前
AD 提权-CVE-2022-26923: CertiFried
windows·渗透·kali·提权·域渗透
面朝大海,春不暖,花不开16 小时前
Python 文件操作与输入输出:从基础到高级应用
windows·python·microsoft
染指111018 小时前
35.x64汇编写法(二)
汇编·windows·x64游戏·x64汇编·游戏攻防
新兴AI民工19 小时前
windows上的visual studio2022的项目使用jenkins自动打包
windows·jenkins·visual studio