【pytest + allure】生成测试报告

一、下载导入包

  • 1.在自己下载包环境下cmd指令:
pip install allure-pytest -i https://mirrors.aliyun.com/pypi/simple
  • 2.导入包
import allure,pytest

二、生成报告

  • 1…依次点击File–>Settings
    在这里插入图片描述
  • 2.找到Tools–>Python Integrated Tools–>点击下拉框选择Unittests
    在这里插入图片描述
  • 3.把命令写到main.py文件运行完测试用例的代码之后就自动生成报告了。
#生成测试数据
pytest.main(["-sq",__file__,'--alluredir=./report'])
#生成测试报告
os.system('allure generate ./report -O ./report-html --clean')

三、打开测试报告

1.用例执行完成后,在report文件夹下会有两个文件夹:report、allure-report。在html这个文件夹下,找到index.html文件,这个就是我们生成的测试报告,右键用浏览器打开就可以看到了。
在这里插入图片描述

四、完整代码例子

import allure,os,json,pytest
from jsonpath import jsonpath
from openpyxl import load_workbook,Workbook
import requests
from utils.MysqlUtilss import Mysql


mysql = Mysql()

wk = load_workbook(filename='D://接口测试用例.xlsx')
# 选择活动工作表或者指定的工作表
sheet = wk['登录退出']

data_test = list(sheet.iter_rows(values_only=True,min_row=2))

class Test():
    """项目名称【swsq后台】"""
    @pytest.mark.parametrize("index",list(range(len(data_test))))
    def test_admin_api(self, index):
        i = data_test[index]
        allure.dynamic.feature("项目名称【swsq】")
        allure.dynamic.title("{case_id}_{case_name}".format(case_id=i[0], case_name=i[4]))
        allure.dynamic.tag("P0")
        allure.dynamic.severity("critical")
        allure.dynamic.description("接口参数:{description}".format(description=str(i)))
        sql = str(i[6]).split("\n")
        url = i[7]+i[8]
        method = str(i[9])
        params = i[10]
        yq_result = str(i[11]).strip("\n").split("\n")
        if method == "GET":
            pass
        elif method == "POST":
            r = requests.post(url,json=json.loads(params))
            for l in sql:
                mysql.exec(l)
            for yq in yq_result:
                left = yq.split("==")[0]
                right = yq.split("==")[1]
                sj_result = jsonpath(r.json(), left)
                print(sj_result)
                print("*"*10)
                if right == "true":
                    with allure.step(f"断言验证【{left}】是否存在"):
                        # allure.attach("data",i)
                        assert sj_result
                elif "%" in right:
                    with allure.step(f"断言验证实际结果【{sj_result[0]}】是否包含【{right.strip('%')}】"):

                        assert right.strip("%") in sj_result[0]
                else:
                    with allure.step(f"断言验证实际结果【{sj_result[0]}】与预期结果【{right}】完全一致"):

                        assert sj_result[0] == right

if __name__ == '__main__':
    # 生成测试数据
    pytest.main(["-sq",__file__,'--alluredir=./report'])
    # 生成测试报告
    os.system('allure generate ./report -O ./report-html --clean')

1.allure.dynamic

写一个项目名:allure.dynamic.feature(“项目名称【swsq】”)
左侧用例名称:allure.dynamic.title(“{case_id}_{case_name}”.format(case_id=i[0], case_name=i[4]))
标签:allure.dynamic.tag(“P0”)
优先级:allure.dynamic.severity(“critical”)
描述:allure.dynamic.description(“接口参数:{description}”.format(description=str(i)))
执行:with allure.step(f"断言验证【{left}】是否存在")
在这里插入图片描述

2.jsonpath

from jsonpath import jsonpath

a={“a”:“123”,“B”:“123”}#一个为a的json数据
b = jsonpath(a,“$.123”)#
print(b)

jsonpath用法:

  • 1.第一个参数:json数据
  • 2.第二个参数:$.键
  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值