You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

数据驱动测试中多可变数据集的断言设置与管理方案咨询

可变测试参数的断言设置与多数据集管理方案

一、可变参数的动态断言调整

你当前的断言是硬编码固定值,直接和输入参数绑定做动态断言即可,不用随数据集修改断言逻辑:

response = requests.post(url, json= plan1A1C(suminsured, tenure))
json_response = response.json()

# 动态关联输入参数作为预期值,适配不同测试数据集
assert int(json_response[0]['premium']) == int(suminsured)
assert int(json_response[0]['term']) == int(tenure)

如果保费计算有单独业务规则(比如折扣、阶梯定价),可以封装独立的预期值计算函数,调用函数生成预期值再做断言:

# 封装业务规则计算预期保费
def get_expected_premium(suminsured, tenure):
    # 示例规则:保额10万以上打95折,不满10万原价
    discount = 0.95 if suminsured >= 100000 else 1
    return suminsured * discount

assert int(json_response[0]['premium']) == int(get_expected_premium(suminsured, tenure))

二、多组测试数据集的管理方案

针对10组及以上的测试数据,用参数化测试的方式统一管理,所有测试数据集中存放,测试逻辑只写一次,不会出现重复代码:

示例(基于Python pytest框架)

import pytest
import requests

# 统一管理所有测试用例,新增/修改用例仅需调整该列表
TEST_DATA = [
    # 格式:(保额suminsured, 期限tenure, 预期保费, 预期term)
    (100000, 1, 100000, 1),
    (200000, 3, 190000, 3),
    (50000, 2, 50000, 2),
    # 剩余7组测试用例直接在此补充即可
]

@pytest.mark.parametrize("suminsured, tenure, expect_premium, expect_term", TEST_DATA)
def test_insurance_plan(suminsured, tenure, expect_premium, expect_term):
    # 测试逻辑仅写1次,自动遍历所有测试数据执行
    response = requests.post(url, json=plan1A1C(suminsured, tenure))
    json_response = response.json()
    assert int(json_response[0]['premium']) == int(expect_premium)
    assert int(json_response[0]['term']) == int(expect_term)

方案优势

  • 测试数据和测试逻辑完全解耦,调整用例不用修改执行代码,降低出错概率
  • 执行后会单独返回每组测试用例的执行结果,哪组数据出错可以直接定位
  • 可扩展性强,后续新增测试用例只需在TEST_DATA列表加一条记录即可

内容的提问来源于stack exchange,提问作者Apratim Chaudhuri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 18:15:03