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

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

1. 断言静态字段

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

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}")

总结

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

相关推荐
怎么没有名字注册了啊10 小时前
Qt开发:MessageBox怎么调出标题栏那个问号按钮?
windows
李子红了时11 小时前
EncFS MP中文版汉化下载(免费文件加密工具)
windows·安全
辻弋20112 小时前
五年前的旅行视频糊成马赛克?Video2X用Real-ESRGAN逐帧重建细节,但只支持Windows、集显用户建议直接放弃
服务器·数据库·windows·游戏引擎·电脑
会飞的土拨鼠呀12 小时前
Windows 系统关闭虚拟内存文件pagefile.sys
windows
传奇开心果编程14 小时前
【Compose Multiplatform 跨端开发学与练】第7课 平台适配与互操作
android·windows·学习·ui·ios·kotlin·composer
工作10年+,存储芯片行业14 小时前
Linux NVMe 中断排查与性能优化:CPU 亲和性
linux·运维·服务器·windows·性能优化·ssd·pcie
lpfasd12315 小时前
WinSW在Win7上失败真相-实测与修复
windows·nginx
万山寒16 小时前
windows使用WinSW注册服务
windows
白搞电子18 小时前
ESP32-S31 项目如何制作 Windows 一键烧录包:完整镜像、esptool 与数据边界
windows·stm32·单片机
apple_6926588519 小时前
Github开源免费 红果短剧 Windows 电脑桌面版:民间开发者做的第三方播放器,不用装模拟器了
windows