快逃
好像就叫 apitest,体验太差了,一个用例编辑就能有四层页面深度,实际用起来还是点得发昏,还好跑了
那听起来就是你们已经习惯了手写 mysql 的方式,按照加代理做语句替换的方式可能确实是最适合你们的,做好替换器的本地开发感觉是 ok 的
描述再详细点?看看现在的调用链路上的结构是咋样的
上吧,练习写业务的绝佳机会,copilot 会告诉你答案
能理解这个目的,那你这里毕竟把【数据位置】和具体的 json 实例 : resp.json()['data']['list'][0]['id'] 耦合在一起了,得重新设计一下才行
def get_value_by_path(json_data, path):
for key in path:
if isinstance(key, int) or key in json_data:
json_data = json_data[key]
else:
return None
return json_data
# 传参 path
def check_response(resp, path, expected_code=2000):
allure.step("Check response status code and content")
try:
assert resp.status_code == 200, f"Expected status code 200 but got {resp.status_code}"
assert resp.json()['code'] == expected_code, f"Expected code {expected_code} but got {resp.json()['code']}"
# 通过path 拿到数据
return get_value_by_path(resp.json(), path)
except (AssertionError,IndexError) as e:
allure.attach(
json.dumps(resp.json(), ensure_ascii=False, indent=2),
name="response.json",
attachment_type=allure.attachment_type.JSON
)
allure.attach(
resp.text,
name="response.text",
attachment_type=allure.attachment_type.TEXT
)
raise AssertionError(f"Fixture: 断言失败") from e
@pytest.fixture(scope="session", autouse=True)
def tf_get_trainerId_xiehe(tf_getSession):
url = Environment.HOST_ADMIN + 'managerList'
data = {'page': 1,
'limit': 30,
'keys': Environment.userphone_tea,
'characterType': 1,
'roles': ''}
resp = tf_getSession.post(url=url, data=data)
# 自定义 【数据位置】
target_path = ['data', 'list', 0, 'id']
AssociatedDataTanQiang.id_xiehe_admin = check_response(resp,target_path)
yield
# 传参去掉target_position
def check_response(resp, expected_code=2000):
allure.step("Check response status code and content")
try:
assert resp.status_code == 200, f"Expected status code 200 but got {resp.status_code}"
assert resp.json()['code'] == expected_code, f"Expected code {expected_code} but got {resp.json()['code']}"
# target_position在内部的异常处理内再显式调用
return resp.json()['data']['list'][0]['id']
except (AssertionError,IndexError) as e:
allure.attach(
json.dumps(resp.json(), ensure_ascii=False, indent=2),
name="response.json",
attachment_type=allure.attachment_type.JSON
)
allure.attach(
resp.text,
name="response.text",
attachment_type=allure.attachment_type.TEXT
)
raise AssertionError(f"Fixture: 断言失败") from e
@pytest.fixture(scope="session", autouse=True)
def tf_get_trainerId_xiehe(tf_getSession):
url = Environment.HOST_ADMIN + 'managerList'
data = {'page': 1,
'limit': 30,
'keys': Environment.userphone_tea,
'characterType': 1,
'roles': ''}
resp = tf_getSession.post(url=url, data=data)
# 只传resp
AssociatedDataTanQiang.id_xiehe_admin = check_response(resp)
yield
这里不是 allure,fixture,异常处理等等特性的原因
简单理解一下
IndexError 就是越界异常,或者说下标找不到
你再看一下 checkresponse 这个入参
resp.json()['data']['list'][0]['id']
这个 resp 数据的 list 已经没有第 1 个 list,也没有 list 中的 id 了
就是这么越界的吧
check_response(resp,resp.json()['data']['list'][0]['id'])
是因为封装后,对 resp 的参数访问被放到了异常处理外层,然后报 IndexError 吗?