自动化测试基础——allure下载安装及配置及pytest + allure-pytest插件生成allure企业级测试报告及企业级定制

前言

allure 是一个java测试报告框架。所以要基于JDK环境

一、allure下载

二、allure安装

  • 将下载好的安装包解压到需要存放的路径下(我这边存放在了D盘的根目录下)

    在这里插入图片描述

    在这里插入图片描述

三、allure目录介绍

  • bin:命令文件

  • config:配置文件

  • lib:存放jar包

  • plugins:插件

    在这里插入图片描述

四、allure环境变量配置

  1. 此电脑 → 属性

    在这里插入图片描述

  2. 高级系统设置

    在这里插入图片描述

  3. 环境变量

    在这里插入图片描述

  4. 双击系统变量下的Pash

    在这里插入图片描述

  5. 新建

    在这里插入图片描述

  6. 填写 allure 安装的路径 → 确定(D:\allure-2.21.0\bin)

    在这里插入图片描述

  7. 确定

    在这里插入图片描述

  8. 确定

    在这里插入图片描述

  9. Windows系统及PyCharm中验证

    allure --version
    

    在这里插入图片描述

    在这里插入图片描述

注意:allure环境变量配置完成以后,Windows窗口验证没问题,pycharm验证有问题,则重启pycharm重新验证即可

五、pytest + allure-pytest插件生成allure企业级测试报告

  1. 安装 pytest allure-pytest 插件

    pip install pytest 
    pip install allure-pytest
    
  2. 配置 pytest.ini 全局配置文件

    # --alluredir=./temps:设置allure生成临时的json格式的报告存放的路径
    # --clean-alluredir:清空上一次运行的记录
    
    addopts = -vs --alluredir=./temps --clean-alluredir
    

    在这里插入图片描述

  3. pytest测试框架的主函数入口配置

    参数作用
    generte生成报告
    tempsallure生成临时的json格式的报告存放的路径
    -o生成allure报告的目录
    report生成allure报告存放的目录
    -c,–clean清空
    import os
    
    import pytest
    
    # 运行pytest测试框架的主函数
    if __name__ == '__main__':
        pytest.main()
        # 调用allure生成报告
        os.system("allure generate ./temps -o ./report --clean")
    

    在这里插入图片描述

注意:需要通过pytest主函数执行才会生成allure的html测试报告

  1. 查看allure生成的html测试报告

    在这里插入图片描述
    在这里插入图片描述

  2. 修改allure报告的语言

    在这里插入图片描述

    在这里插入图片描述

六、allure企业级报告的log定制

  1. 前往allure安装目录下的plugins存放插件的目录下复制定制log的插件名称

    在这里插入图片描述

  2. 将复制的插件名称粘贴到allure安装目录下的config目录下的allure.yml文件中

    config目录下的allure.yml文件中配置自定义的logo插件

    - custom-logo-plugin
    

    在这里插入图片描述

    在这里插入图片描述

  3. 重新启动pytest生成allure报告,查看是否启用自定义log插件

    在这里插入图片描述

  4. 将准备好的log图片放到allure安装目录下的plugins\custom-logo-plugin\static定制log的插件路径下

    在这里插入图片描述

  5. 修改allure安装目录下的plugins\custom-logo-plugin\static定制log的插件路径下的styles.css文件

    通过修改这两个参数来调整log图片的大小和位置:margin-left: 10px;height: 90px;

    .side-nav__brand {
      background: url('log.jpg') no-repeat left center !important;
      margin-left: 10px;
      height: 90px;
      background-size: contain !important;
    }
    
    .side-nav__brand-text {
      display: none;
    }
    

    在这里插入图片描述
    在这里插入图片描述

  6. 重新启动pytest生成allure报告,查看是否定制log成功

    在这里插入图片描述

七、allure企业级报告功能内容定制

1.功能左边层级定制

在这里插入图片描述

  • (1)项目名称(史诗):@allure.epic()

  • (2)模块名称(特性):@allure.feature()

  • (3)接口名称(分组):@allure.story()

  • (4)用例标题:有两种方式(两种效果是一样的,方式二更加灵活)

    • 方式一:@allure.title()
    • 方式二:allure.dynamic.title()

    方法一

    import allure
    
    
    @allure.epic("项目名称-智考1.0")
    @allure.feature("模块-用户管理模块")
    class TestFirstClass():
    
        @allure.story("用户登陆接口")
        @allure.title("用例名称-验证成功登陆")
        def test_login(self):
            print("登陆")
    
        @allure.story("用户注册接口")
        @allure.title("用例名称-验证成功注册")
        def test_register(self):
            print("注册")
    
        @allure.story("添加用户")
        @allure.title("用例名称-验证成功添加用户")
        def test_add_user(self):
            print("添加用户")
    

    在这里插入图片描述
    在这里插入图片描述

    在这里插入图片描述

    方法二

    import allure
    
    
    @allure.epic("项目名称-智考1.0")
    @allure.feature("模块-用户管理模块")
    class TestFirstClass():
    
        @allure.story("用户登陆接口")
        def test_login(self):
            allure.dynamic.title("用例名称-验证成功登陆")
            print("登陆")
    
        @allure.story("用户注册接口")
        def test_register(self):
            allure.dynamic.title("用例名称-验证成功注册")
            print("注册")
    
        @allure.story("添加用户")
        def test_add_user(self):
            allure.dynamic.title("用例名称-验证成功添加用户")
            print("添加用户")
    

    在这里插入图片描述

注意:每次修改内容后要查看预期结果需要重新执行allure生成报告

2.功能右边优先级定制


优先级默认都是:normal

在这里插入图片描述

  • 测试用例严重级别:

    • BLOCKER:致命的(@allure.severity(allure.severity_level.BLOCKER)
    • CRITICAL:严重的(@allure.severity(allure.severity_level.CRITICAL)
    • NORMAL:正常的(@allure.severity(allure.severity_level.NORMAL)
    • MINOR:轻微的(@allure.severity(allure.severity_level.MINOR)
    • TRIVIAL:不重要的(@allure.severity(allure.severity_level.TRIVIAL)
    import allure
    
    
    @allure.epic("项目名称-智考1.0")
    @allure.feature("模块-用户管理模块")
    class TestFirstClass():
    
        @allure.story("用户登陆接口")
        @allure.severity(allure.severity_level.BLOCKER)
        def test_login(self):
            allure.dynamic.title("用例名称-验证成功登陆")
            print("登陆")
    
        @allure.story("用户注册接口")
        @allure.severity(allure.severity_level.CRITICAL)
        def test_register(self):
            allure.dynamic.title("用例名称-验证成功注册")
            print("注册")
    
        @allure.story("添加用户")
        @allure.severity(allure.severity_level.NORMAL)
        def test_add_user(self):
            allure.dynamic.title("用例名称-验证成功添加用户")
            print("添加用户")
    
        @allure.story("删除用户")
        @allure.severity(allure.severity_level.MINOR)
        def test_delete_user(self):
            allure.dynamic.title("用例名称-验证成功删除用户")
            print("删除用户")
    
        @allure.story("修改用户")
        @allure.severity(allure.severity_level.TRIVIAL)
        def test_update_user(self):
            allure.dynamic.title("用例名称-验证成功修改用户")
            print("修改用户")
    

    在这里插入图片描述
    在这里插入图片描述

3.功能右边测试用例描述定制

  • 测试用例的描述定制:有两种方式

    • 方式一:@allure.description()
    • 方式二:allure.dynamic.description()

    方法一:

    import allure
    
    
    @allure.epic("项目名称-智考1.0")
    @allure.feature("模块-用户管理模块")
    class TestFirstClass():
    
        @allure.story("用户登陆接口")
        @allure.severity(allure.severity_level.BLOCKER)
        @allure.description("这是验证登陆是否成功")
        def test_login(self):
            allure.dynamic.title("用例名称-验证成功登陆")
            print("登陆")
    
        @allure.story("用户注册接口")
        @allure.severity(allure.severity_level.CRITICAL)
        @allure.description("这是验证注册是否成功")
        def test_register(self):
            allure.dynamic.title("用例名称-验证成功注册")
            print("注册")
    
        @allure.story("添加用户")
        @allure.severity(allure.severity_level.NORMAL)
        @allure.description("这是验证添加用户是否成功")
        def test_add_user(self):
            allure.dynamic.title("用例名称-验证成功添加用户")
            print("添加用户")
    
        @allure.story("删除用户")
        @allure.severity(allure.severity_level.MINOR)
        @allure.description("这是验证删除用户是否成功")
        def test_delete_user(self):
            allure.dynamic.title("用例名称-验证成功删除用户")
            print("删除用户")
    
        @allure.story("修改用户")
        @allure.severity(allure.severity_level.TRIVIAL)
        @allure.description("这是验证修改用户是否成功")
        def test_update_user(self):
            allure.dynamic.title("用例名称-验证成功修改用户")
            print("修改用户")
    
    

    在这里插入图片描述
    在这里插入图片描述
    方法二:

    import allure
    
    
    @allure.epic("项目名称-智考1.0")
    @allure.feature("模块-用户管理模块")
    class TestFirstClass():
    
        @allure.story("用户登陆接口")
        @allure.severity(allure.severity_level.BLOCKER)
        def test_login(self):
            allure.dynamic.title("用例名称-验证成功登陆")
            allure.dynamic.description("这是验证登陆是否成功")
            print("登陆")
    
        @allure.story("用户注册接口")
        @allure.severity(allure.severity_level.CRITICAL)
        def test_register(self):
            allure.dynamic.title("用例名称-验证成功注册")
            allure.dynamic.description("这是验证注册是否成功")
            print("注册")
    
        @allure.story("添加用户")
        @allure.severity(allure.severity_level.NORMAL)
        def test_add_user(self):
            allure.dynamic.title("用例名称-验证成功添加用户")
            allure.dynamic.description("这是验证添加用户是否成功")
            print("添加用户")
    
        @allure.story("删除用户")
        @allure.severity(allure.severity_level.MINOR)
        def test_delete_user(self):
            allure.dynamic.title("用例名称-验证成功删除用户")
            allure.dynamic.description("这是验证删除用户是否成功")
            print("删除用户")
    
        @allure.story("修改用户")
        @allure.severity(allure.severity_level.TRIVIAL)
        def test_update_user(self):
            allure.dynamic.title("用例名称-验证成功修改用户")
            allure.dynamic.description("这是验证修改用户是否成功")
            print("修改用户")
    

    在这里插入图片描述

    在这里插入图片描述

4.功能右边测试用例链接定制

  • @allure.link():接口访问链接

  • @allure.issue():bug链接

  • @allure.testcase():测试用例链接

    import allure
    
    
    @allure.epic("项目名称-智考1.0")
    @allure.feature("模块-用户管理模块")
    class TestFirstClass():
    
        @allure.story("用户登陆接口")
        @allure.severity(allure.severity_level.BLOCKER)
        @allure.link("接口访问链接")
        @allure.issue("bug链接")
        @allure.testcase("测试用例链接")
        def test_login(self):
            allure.dynamic.title("用例名称-验证成功登陆")
            allure.dynamic.description("这是验证登陆是否成功")
            print("登陆")
    

    在这里插入图片描述

    在这里插入图片描述

5.功能右边测试用例测试步骤定制

  • 测试步骤定制:两种方式(常用第二种

    • 方式一:@allure.step()
    • 方式二:with allure.step():

    方法一:

    import allure
    
    
    @allure.epic("项目名称-智考1.0")
    @allure.feature("模块-用户管理模块")
    class TestFirstClass():
    
        @allure.story("用户登陆接口")
        @allure.severity(allure.severity_level.BLOCKER)
        @allure.link("接口访问链接")
        @allure.issue("bug链接")
        @allure.testcase("测试用例链接")
        @allure.step("测试步骤:输入用户名、输入密码、点击登陆")
        def test_login(self):
            allure.dynamic.title("用例名称-验证成功登陆")
            allure.dynamic.description("这是验证登陆是否成功")
            print("登陆")
    

    在这里插入图片描述
    在这里插入图片描述

    方法二:

    import allure
    
    
    @allure.epic("项目名称-智考1.0")
    @allure.feature("模块-用户管理模块")
    class TestFirstClass():
    
        @allure.story("用户登陆接口")
        @allure.severity(allure.severity_level.BLOCKER)
        @allure.link("接口访问链接")
        @allure.issue("bug链接")
        @allure.testcase("测试用例链接")
        def test_login(self):
            allure.dynamic.title("用例名称-验证成功登陆")
            allure.dynamic.description("这是验证登陆是否成功")
            with allure.step("第一步:输入用户名"):
                print("输入用户名")
            with allure.step("第二步:输入密码"):
                print("输入密码")
            with allure.step("第三步:点击登陆"):
                print("点击登陆")
            print("登陆")
    

    在这里插入图片描述
    在这里插入图片描述

6.功能右边测试用例测试步骤+附件定制

  • 测试用例步骤+附件(一般用于错误截图(一般用于web自动化测试))

    import allure
    
    
    @allure.epic("项目名称-智考1.0")
    @allure.feature("模块-用户管理模块")
    class TestFirstClass():
    
        @allure.story("用户登陆接口")
        @allure.severity(allure.severity_level.BLOCKER)
        @allure.link("接口访问链接")
        @allure.issue("bug链接")
        @allure.testcase("测试用例链接")
        def test_login(self):
            allure.dynamic.title("用例名称-验证成功登陆")
            allure.dynamic.description("这是验证登陆是否成功")
            print("登陆")
            # 测试步骤
            for i in range(1, 6):
                with allure.step("第" + str(i) + "步"):
                    pass
            # 错误截图
            with open("D:\\error.png", mode="rb") as f:
                result = f.read()
                allure.attach(body=result, name="错误截图", attachment_type=allure.attachment_type.PNG)
    

    在这里插入图片描述

    在这里插入图片描述

6.功能右边测试用例测试步骤+文本内容定制

  • 文本内容定制:一般应用于接口自动化

  • 文本内容定制:allure.attach("文本内容", name="文本名称", attachment_type=allure.attachment_type.TEXT)

    import allure
    
    
    @allure.epic("项目名称-智考1.0")
    @allure.feature("模块-用户管理模块")
    class TestFirstClass():
    
        @allure.story("用户登陆接口")
        @allure.severity(allure.severity_level.BLOCKER)
        @allure.link("接口访问链接")
        @allure.issue("bug链接")
        @allure.testcase("测试用例链接")
        def test_login(self):
            allure.dynamic.title("用例名称-验证成功登陆")
            allure.dynamic.description("这是验证登陆是否成功")
            print("登陆")
            # 测试步骤
            for i in range(1, 6):
                with allure.step("第" + str(i) + "步"):
                    pass
            # 错误截图
            with open("D:\\error.png", mode="rb") as f:
                result = f.read()
                allure.attach(body=result, name="错误截图", attachment_type=allure.attachment_type.PNG)
            # 接口自动化:文本
            # 请求四要素
            allure.attach("接口地址:https://www.baidu.com", name="文本1", attachment_type=allure.attachment_type.TEXT)
            allure.attach("接口参数:{一般从yaml中获取}", name="文本2", attachment_type=allure.attachment_type.TEXT)
            allure.attach("接口请求方式:get", name="文本3", attachment_type=allure.attachment_type.TEXT)
            allure.attach("请求头:{一般从yaml中获取}", name="文本4", attachment_type=allure.attachment_type.TEXT)
            # 响应内容
            allure.attach("响应文本:{一般从yaml中获取}", name="文本5", attachment_type=allure.attachment_type.TEXT)
            allure.attach("接口执行结果:成功/失败", name="文本6", attachment_type=allure.attachment_type.TEXT)
    

    在这里插入图片描述

    在这里插入图片描述

八、局域网下查看allure测试报告

1.局域网(内网)


让公司内部局域网下其他人员查看allure测试报告(通过下面方法,将得到的链接地址分析给其他人员即可)

  • 命令:allure open allure测试报告地址

    allure open ./report
    

在这里插入图片描述
在这里插入图片描述

2.局域网(外网)


需要做网络映射,需要运维人员去完成,不属于测试的工作范围

  • 34
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

人菜瘾大的小熊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值