使用pytest+allure生成测试报告,
1.首先需要完成安装 pip install allure-pytest 插件
2.安装完成后需要配置环境变量 #记得配置,不然后续流程会有问题
代码部分
import pytest
import allure
@allure.story('获取页面权限')
#获取回访基础数据页面权限
def test_visit_BSdata_TableList_getDetail(self):
result =self.mc.TableList_getDetail(1123058638169051136,1190571324263632896,1190553842194845696)
#添加接口返回数据
allure.attach(result,'接口返回详情',allure.attachment_type.JSON)
return result
#运行代码
if __name__ == '__main__':
#生成报告文件
pytest.main(['-s','--alluredir=allure-results','--clean-alluredir'])
#--clean-alluredir用于清除历史报告记录,否则报告中会显示多次执行记录
#生成报告结果html
os.system('allure generate ./allure-results -o ./allure-results/resport --clean')
#自动打开报告文件
os.system('allure open ./allure-results/resport')
注意生成报告文件、结果的路径要一致,否则会导致生成了报告文件,但是打开html文件无数据的情况
其中allure.attach是用于将接口返回接口传入报告中
这里我使用的是json格式的内容
如果格式不对,需要进行转换再传入,否则在报告查询中会出现乱码
def get_api():
url = 'wwww.baidu.com'
data = {a:1}
response = requests.get(url=url, params=data)
response_json = response.text
#这里返回的数据格式就是json格式,所以可以直接进行添加
allure.attach(response_json,'result',allure.attachment_type.JSON)
#如果返回值里做了格式转换的,则需要将格式转回json
def get_api_1():
url = 'wwww.baidu.com'
data = {a:1}
response = requests.get(url=url, params=data)
result = response.json
#查看result类型
print(type(result))
#这里的result是dict类型,需要转换为json格式
allure.attach((json.dumps(result,ensure_ascii = False)).encode('utf-8'),'result',allure.attachment_type.JSON )