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

总结

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

相关推荐
默默无名的大学生5 小时前
数据结构—顺序表
数据结构·windows
湖南馒头5 小时前
【Win11 启动项BCD文件修复教程】
windows·系统恢复·win1
帅得不敢出门10 小时前
macOS苹果电脑运行向日葵远程控制软件闪退
windows·macos·远程控制·向日葵
索迪迈科技15 小时前
记一次 .NET 某中医药附属医院门诊系统 崩溃分析
windows·c#·.net·windbg
十八旬15 小时前
苍穹外卖项目实战(day7-2)-购物车操作功能完善-记录实战教程、问题的解决方法以及完整代码
java·开发语言·windows·spring boot·mysql
Clownseven20 小时前
阿里云ECS安装Windows Server 2022教程 | 纯文字分步指南
windows·阿里云·云计算
T0uken21 小时前
【C++】LLVM-mingw + VSCode:Windows 开发攻略
c++·windows·vscode
能摆一天是一天1 天前
JAVA stream().flatMap()
java·windows
JosieBook1 天前
【远程运维】Linux 远程连接 Windows 好用的软件:MobaXterm 实战指南
linux·运维·windows