pytest生成测试报告

2022-12-291092

集成Allure报告

如果要在pytest中集成allure报告,首先得下载allure,并配置好环境

allure环境配置 一、下载allure: 地址:https://github.com/allure-framework/allure2/releases 下载之后并解压

二、环境变量配置 将allure解压之后的allure路径的bin目录路径放到环境变量当中

三、安装allure的pytest插件 pip install allure-pytest 2、生成allure报告 安装配置好allure环境之后,在使用pytest执行用例时,就可以通过allure插件的参数来指定生成allure来报告了。

运行参数: --alluredir :指定allure报告保存的路径

命令行运行 pytest --alluredir=reports 代码中使用pytest.main执行 import pytest

pytest.main(['--alluredir=reports']) 3、启动allure服务 在命令终端输入如下命令,启动allure服务 命令:allure serve 生成的报告路径 allure serve reports 执行上述命令,allure服务启动之后会自动在打开浏览器,显示allure的服务页面

4、allure内置常用的方法 添加错误截图 allure.title def error_save_screenshot(driver,file_path, desc):

    # 对当前页web页面进行截图
    driver.save_screenshot(file_path)
    # 将截图保存到allure报告中
    with open(file_path, "rb") as f:
        file = f.read()
        allure.attach(file, "失败截图", allure.attachment_type.PNG)

添加报告中的用例名称 allure.title import allure

class TestLogin:

@allure.title('登录用例')
def test_login(self):
    pass

参数化的用例中动态设置用例名称 allure.dynamic.title 用例数据 casedatas = [ {'title': '反向用例1','data':"xxx"}, {'title': '反向用例2','data':"xxx"}, {'title': '反向用例3','data':"xxx"} ]

class TestLogin: @pytest.mark.parametrize('item',casedatas ) def test_demo(self, item):

    # 动态设置报告中的用例名称
    allure.dynamic.title(item['title'])

添加报告中的功能描述 allure.story @allure.story('登录功能') class TestLogin:

@allure.title('登录用例')
def test_login(self):
    pass

添加报告中套件名称 allure.suite

@allure.suite('登录测试套件') class TestLogin: @allure.title('登录用例') def test_login(self): pass

分享
点赞0
打赏
上一篇:Docker常用命令笔记(一)