1、teststeps中设置teardown
使用$符号在请求后获取response的内容,自定义函数中进行处理
teststeps = [
Step(
RunRequest("查找界面上传的所有文件")
.get("/xxx/xxx")
.with_headers(
**{
"accept-language": "${read_ini_config(Header, accept-language)}",
"autel-token": "${get_login_info(expert_001, token)}",
}
)
# 如果界面有文件,则遍历response并删除文件,如果没有文件则返回None
.teardown_hook("${teardown_hook_clean_file($response)}}")
.validate()
.assert_equal("status_code", 200)
.assert_equal('headers."Content-Type"', "application/json")
.assert_equal("body.code", 200)
.assert_equal("body.message", "success")
),
2、teardown_hook_clean_file 的content
def teardown_hook_clean_file(response):
# 如果response有内容存在,则获取所有列表
data_content = response.body["data"]
if data_content == []:
return None
file_id = [x["id"] for x in data_content]
for i in file_id:
url = (host + f"/xxx/xxx/{i}")
with requests.delete(url=url, headers=header) as response:
data = response.json()
print("成功删除用户操作手册界面的内容")
3、问题
(1)data_content = response.body["data"]
# 我采坑了,字典获取我竟然用 body.data ,还好debug出来了,人都给我整麻了
(2)循环了一次,我直接return了,然后没有删除直接上传,导致接口一直报错:请求参数错误,定位半个小时
def teardown_hook_clean_file(response):
# 如果response有内容存在,则获取所有列表
data_content = response.body["data"]
if data_content == []:
return None
file_id = [x["id"] for x in data_content]
for i in file_id:
url = (host + f"/xxx/xxx/{i}")
with requests.delete(url=url, headers=header) as response:
data = response.json()
print("成功删除用户操作手册界面的内容")
return data