Pytest之allure报告生成

文章介绍了如何配置和使用Allure报告工具,结合pytest框架生成测试报告。通过函数标记装饰器如@allure.epic,@allure.feature等,可以详细组织测试结构。文中展示了生成JSON文件并转换为HTML报告的命令,以及在代码中使用allure.step进行步骤注解的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

准备条件:

  1. 安装 Allure 并配置环境变量 https://github.com/allure-framework/allure2/releases

  1. import allure

报告生成:

  1. 结合pytest框架生成allure报告

if __name__ == '__main__':
    import pytest
    import os

    pytest.main(['-vs', './test_case_api/test.py', '--alluredir=../tmp/allure-results', '../tmp/allure-results'])
    os.system("allure generate ../tmp/allure-results -o ../tmp/html --clean")

生成json文件的固定写法:

pytest.main(['-vs',用例脚本文件路径,指定alluredir路径,生成json报告存放路径])

输出json文件为html文件固定写法:

os.system("allure generate json文件存放路径 -o html文件存放路径 --clean")

  1. 函数标记装饰器

函数标记装饰器,可以同步展示到测试报告内(函数才能使用)

@allure.epic 史诗->系统

@allure.feature 模块

@allure.story 故事

@allure.title 标题

@allure.step 步骤

@allure.feature("xx模块")
class Test_100(unittest.TestCase):
    @allure.story("用户中心")
    @allure.title('新增用户')
    def test_01(self):
        with allure.step("新增"):

生成报告效果如下:

1、在展示层级上,epic包含feature,feature包含story,story包含title,title包含step

2、它们可以同时使用,也可以单独使用,也可以不使用

  1. 标记方式都支持汉字

with allure.step()在python脚本中都可以使用,是代码块形式

with allure.step():

print("第一步")

    def wait(self, assert_sql, value):
        assert_sql = Template(assert_sql).substitute(goods=Argument.goods)
        sql_connect = Sql()
        result = sql_connect.execute(assert_sql).fetchone()
        with allure.step("当状态为空时,等待3s"):
            while not result:
                result = sql_connect.execute(assert_sql).fetchone()
                time.sleep(3)
        with allure.step("当状态不为空时,判断状态值"):
            while result:
                time.sleep(3)
                result = sql_connect.execute(assert_sql).fetchone()
                if result[0] == int(value):
                    with allure.step("成功"):
                        break
                else:
                    with allure.step("循环等待3s"):
                        continue

### 如何使用 Pytest 生成 Allure 测试报告 #### 配置环境 为了能够成功生成 Allure 报告,首先需要确保环境中已正确安装所需的工具和库。以下是具体操作: 1. **安装 `allure-pytest` 插件** 使用 pip 工具来安装 `allure-pytest` 插件,这是 pytest 的一个扩展插件,用于支持 Allure 报告功能。 ```bash pip install allure-pytest ``` 2. **配置 Allure 命令行工具** 下载并配置 Allure 命令行工具以便后续生成 HTML 格式的测试报告。对于 Windows 用户,可以从官方 GitHub 地址下载 `.zip` 文件[^4] 并将其路径添加到系统的 PATH 环境变量中。 3. **设置 pytest.ini 或 conftest.py** 修改项目的根目录下的 `pytest.ini` 文件以包含必要的选项参数,从而简化命令执行过程。 ```ini [pytest] addopts = -vs --alluredir ./allure_result ``` 此处指定了默认的运行模式以及存储 Allure 数据的目标文件夹位置为当前项目中的 `./allure_result/`[^1]。 #### 运行测试用例 通过以下命令启动测试并将结果保存至指定目录: ```bash pytest -vs --alluredir=./myallure --clean-alluredir ``` 上述命令会清理旧有的 Allure 结果数据后再重新收集新的 JSON 格式的结果记录。 #### 生成最终HTML版本的Allure报告 完成测试之后,利用之前准备好的 Allure CLI 来转换原始 XML/json 资料成易于阅读浏览的形式即网页版报表: ```bash allure serve myallure/ ``` 此指令会在本地临时构建一份静态站点供即时预览查看效果[^3]。 如果希望长期保留这些文档,则可采用如下方式导出固定链接地址对应的完整档案包: ```bash allure generate ./myallure -o ./report --clean && allure open ./report ``` 以上就是完整的流程说明,涵盖了从基础环境搭建直至最后呈现可视化成果的所有环节。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值