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

总结

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

相关推荐
程序猿小D10 小时前
第二百三十五节 JPA教程 - JPA Lob列示例
java·数据库·windows·oracle·jdk·jpa
iummature12 小时前
ZLMediaKit Windows编译以及使用
windows
周伯通*15 小时前
Windows上,使用远程桌面连接Ubuntu
linux·windows·ubuntu
GDAL17 小时前
GNU力量注入Windows:打造高效跨平台开发新纪元
服务器·windows·gnu
小徐敲java21 小时前
Windows本地制作java证书(与jeecgboot配置本地证书ssl问题)
java·windows·ssl
春蕾夏荷_72829772521 小时前
electron nsis打包windows应用程序
javascript·windows·electron·nsis
偷偷小野猪21 小时前
Windows 常用的键盘快捷键总结
windows
Splashtop高性能远程控制软件1 天前
centos远程桌面连接windows
linux·windows·centos·远程控制·远程桌面
楚钧艾克1 天前
Windows系统通过部署wsl + Goland进行跨平台开发
linux·windows·后端·ubuntu·golang
wow2ok1 天前
天融信把桌面explorer.exe删了,导致开机之后无windows桌面,只能看到鼠标解决方法
windows·计算机外设