Pytest测试框架(五):pytest + allure生成测试报告

本文详细介绍了如何使用pytest与allure框架生成自动化测试报告。内容包括allure的安装、pytest-allure插件的安装、测试报告的生成和查看方法,以及allure的特性如feature、story、step、link、issue、testcase和severity的使用。此外,还展示了如何通过allure.attach()方法添加附加信息,并给出了结合selenium生成测试报告的示例。

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

Allure 是一款轻量级、支持多语言的开源自动化测试报告生成框架,由Java语言开发,可以集成到 Jenkins。

pytest 测试框架支持Allure 报告生成。pytest也可以生成junit格式的xml报告和HTML报告,命令如下:

pytest test_demo.py --junitxml=report.xml
pytest test_demo.py --html=report.html #需要安装插件:pip install pytest-html

Allure 报告更加灵活美观,本文介绍如何使用pytest 生成 allure测试报告

环境安装

安装allure

  1. allure包下载:https://github.com/allure-framework/allure2/releases
  2. 解压 -> 进入bin目录 -> 运行allure.bat,
  3. 把bin目录加入PATH环境变量

allure官网 : https://qameta.io/allure-report/

allure文档 : https://docs.qameta.io/allure/#

安装 allure-pytest插件

pip install allure-pytest

生成Allure报告

运行

pytest [测试文件] -s -q --alluredir=./result #--alluredir用于指定存储测试结果的路径

查看测试报告

方式一:直接打开默认浏览器展示报告

allure serve ./result/

方式二:从结果生成报告

  • 生成报告

    allure generate ./result/ -o ./report/ --clean (覆盖路径加–clean)

  • 打开报告

    allure open -h 127.0.0.1 -p 8883 ./report/

实例代码:https://docs.qameta.io/allure/#_pytest

test_allure.py:

import pytest

def test_success():
    """this test succeeds"""
    assert True

def test_failure():
    """this test fails"""
    assert False

def test_skip():
    """this test is skipped"""
    pytest.skip('for a reason!')

def test_broken():
    raise Exception('oops')

方法1

执行测试用例:

pytest test_allure.py --alluredir=./result/1

打开报告:

> allure serve ./result/1
Generating report to temp directory...
Report successfully generated to C:\Users\10287\AppData\Local\Temp\6968593833275403330\allure-report
Starting web server...
2020-10-25 20:59:42.368:INFO::main: Logging initialized @4873ms to org.eclipse.jetty.util.log.StdErrLog
Server started at <http://169.254.57.162:60084/>. Press <Ctrl+C> to exit

方法2

allure generate ./result/1 -o ./report/1/ --clean
allure 
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值