接口自动化测试如何灵活地验证动态变化的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}")

总结

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

相关推荐
東雪木2 小时前
泛型、反射、注解(Spring 框架核心底层)专属复习笔记
java·windows·笔记·学习·spring
sun0077003 小时前
Windows下UniGetUI,Linux下敲命令
windows
流星白龙3 小时前
【MySQL高阶】18.缓冲池页管理
数据库·windows·mysql
AI行业学习5 小时前
PuTTY 工具下载部署、基础配置及 SSH 远程服务器连接完整操作指南Windows 平台 【2026.6.1】
运维·windows·ssh
tealcwu5 小时前
【Unity实战】Unity IAP 4.x 在 Windows Store (UWP) 平台上的实现指南
windows·unity·游戏引擎
爱讲故事的6 小时前
操作系统第四讲:OS Interfaces and Syscalls(操作系统接口与系统调用)
linux·windows·ubuntu
糖果店的幽灵7 小时前
LangChain 1.3 完全教程:从入门到精通-Part 10: Memory(记忆系统)
windows·microsoft·langchain
tealcwu8 小时前
【Unity实战】Unity IAP 5.3 中实现 Windows Custom Store 实战教程
windows·unity·游戏引擎
ZenosDoron9 小时前
vsnprintf可变参数格式化输出函数
windows
许彰午9 小时前
11_Java集合框架概述
java·windows·python