• pytest结合Allure生成测试报告


    1.Allure配置安装

    要求:

    ​ 1.需要安装jdk环境

    ​ 2.下载Allure安装包,下载地址:Allure下载地址

    ​ 3.解压后放置英文路径下并配置环境变量PATH

    image-20240221181923327

    验证:allure --version

    image-20240221182246655

    ​ 4.安装allure-pytest插件pip install allure-pytest

    验证:pip show allure-pytest

    image-20240221182447358

    2.使用

    基本命令

    1.使用allure时需要在cmd中执行,可以使用os.system命令省去在cmd窗口中再次执行一次;

    import os
    
    import pytest
    
    
    def test_01():
        print('开始登录')
    
    
    def test_02():
        print('上传数据')
    
    
    def test_03():
        print('数据浏览')
    
    
    if __name__ == '__main__':
        pytest.main(['-sv','test_allure01.py','--alluredir', './result'])
        #pytest.main(['test_allure01.py','--alluredir', './result','--clean-alluredir'])
        os.system('allure generate ./result -o ./report --clean')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • '-sv','test_allure01.py'表示执行指定的测试文件并在控制台中输出;

      ​ 注:使用‘-sv’后,测试报告中就不会再记录控制台的信息了;

    • '--alluredir', './result'表示创建allure报告的路径,allure生成的报告文件都是json文件

    image-20240222100412162

    • '--clean-alluredir'表示清除allure之前生成的json数据,如果不加这个参数会导致:如果对某些用例进行注释或删除,生成的测试报告仍会有之前的记录数据;

    • os.system('allure generate ./result -o ./report --clean')表示在main中转换allure生成的报告文件为html格式;

    • ./result -o :开始执行上面allure 生成的json文件

    • ./report :生成的html格式报告存放路径

    • –clean:清除之前生成的报告,但是不会清除文件,记录仍会保留

    • 使用pytest -h可以查看相关参数

      image-20240223094030734

      –alluredir=DIR 在指定目录中生成 Allure 报告(目录可能不存在)

      –clean-alluredir 如果存在,则清理 alluredir 文件夹

      –allure-no-capture 不将 pytest 捕获的日志/标准输出/标准错误附加到报告中

      –inversion=INVERSION 运行不在测试计划中的测试

    执行完后会生成很多文件,右键浏览器中打开index.html文件

    image-20240222100828308

    报告美化

    1.前置条件

    前置条件只要在测试用例中使用到,测试报告中会自动添加上用例前置函数的名称;

    conftest.py

    import pytest
    
    
    @pytest.fixture
    # @allure.title('前置准备')  # 可以修改fix01的名字
    def fix01():
        print('用例准备前置工作')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    test_allure01.py

    import os
    
    import allure
    import pytest
    
    
    @allure.step('步骤1:执行登录')
    def login():
        print('开始登录')
        print('输入账号')
        print('输入密码')
    
    
    @allure.step('步骤2:上传数据')
    def upload_data():
        print('点击上传按钮')
        print('上传数据')
    
    
    def test_01(fix01):
        # 测试用例中调用登录业务
        login()
    
    
    def test_02():
        # 调用登录业务
        upload_data()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    image-20240222102748961

    image-20240222141721279

    2.用例步骤

    使用装饰器@allure.step()在需要说明的测试步骤函数上装饰后,在测试用例的测试步骤上就能显示出来;

    @allure.step('步骤1:执行登录')会带上函数中的传参和对应的值

    @allure.step("登陆")
    def log(user, pwd):
        print(user)
        print(pwd)
    
    
    @pytest.fixture
    def fix01():
        log('zz', '123')
        print('用例准备前置工作')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    image-20240222135754308

    ``with allure.step(‘步骤1:执行登录’)`不会带上函数里面的传参

    def log(user, pwd):
        with allure.step('用户名'):
            print(user)
        with allure.step('密码'):
            print(pwd)
    
    
    @pytest.fixture
    def fix01():
        log('zz', '123')
        print('用例准备前置工作')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    image-20240222135955772

    3.标题和描述

    import os
    
    import allure
    import pytest
    
    
    @allure.step('步骤1:执行登录')
    def login():
        print('开始登录')
        print('输入账号')
        print('输入密码')
    
    
    @allure.step('步骤2:上传数据')
    def upload_data():
        print('点击上传按钮')
        print('上传数据')
    
    
    class TestUpload:
    
        @allure.story("一级模块")  # 表示一级模块,同名的一级模块会把用例归纳在一起
        @allure.title("登录")  # 用例的title
        @allure.issue("http://127.0.0.1:8080/zentao/buge-login.html", name=
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
  • 相关阅读:
    java毕业设计项目选题基于SSM+JSP+MYSQL+H-UI 实现的校园食堂点餐|订餐系统
    Java-Jackson使用详解
    3 Mybatis详解~
    Python文件高阶操作:复制、删除、移动、压缩文件夹
    问题 R: 超级楼梯(递推,基础DP)查表
    2022-08-25 AndroidR 默认赋予app权限,不弹出权限申请窗口
    第一篇、了解多线程
    SAP IDoc状态70 - This IDoc is saved as the original of an edited document.
    智慧园区能源监控平台:构建绿色智能的未来城市
    MCE | 丙型肝炎病毒的终结之路
  • 原文地址:https://blog.csdn.net/weixin_45595846/article/details/136261114