Allure除了具有Pytest基本状态外,其他几乎所有功能也都支持。

1、严重性

如果你想对测试用例进行严重等级划分,可以使用@allure.severity装饰器,它可以应用于函数,方法或整个类。

它以allure.severity_level枚举值作为参数,分别为:BLOCKER(中断),CRITICAL(严重),NORMAL(常规),MINOR(轻微),TRIVIAL(不重要)。

示例:

# test_sample.py
import allure


# 两数相加
def add(x, y):
    return x   y

# 测试类
@allure.severity(allure.severity_level.TRIVIAL)
class TestAdd:

    @allure.severity(allure.severity_level.MINOR)
    def test_first(self):
        assert add(3, 4) == 7

    @allure.severity(allure.severity_level.NORMAL)
    def test_second(self):
        assert add(-3, 4) == 1

    @allure.severity(allure.severity_level.CRITICAL)
    def test_three(self):
        assert add(3, -4) == -1

    @allure.severity(allure.severity_level.BLOCKER)
    def test_four(self):
        assert add(-3, -4) == -7

运行:

E:\workspace-py\Pytest>pytest test_sample.py --alluredir=report --clean-alluredir
========================================================================== test session starts ==========================================================================
platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
rootdir: E:\workspace-py\Pytest
plugins: allure-pytest-2.8.18, assume-2.3.3, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
collected 4 items                                                                                                                                                        

test_sample.py ....                                                                                                                                                [100%]

=========================================================================== 4 passed in 0.06s ===========================================================================

报告:

你还可以通过--allure-severities选项指定严重等级运行,多个以逗号分隔。

E:\workspace-py\Pytest>pytest test_sample.py --allure-severities normal,critical
========================================================================== test session starts ==========================================================================
platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
rootdir: E:\workspace-py\Pytest
plugins: allure-pytest-2.8.18, assume-2.3.3, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
collected 4 items                                                                                                                                                        

test_sample.py ..                                                                                                                                                  [100%]

=========================================================================== 2 passed in 0.02s ===========================================================================

2、功能

如果你想对测试功能、测试场景进行行为描述,可以分别使用装饰器:@allure.feature@allure.story

示例:

# test_sample.py
import allure

# 两数相加
def add(x, y):
    return x   y

@allure.feature('测试类')
class TestAdd:

    @allure.story('01测试两个正数相加')
    def test_first(self):
        assert add(3, 4) == 7

    @allure.story('02测试负数正数相加')
    def test_second(self):
        assert add(-3, 4) == 1

    @allure.story('03测试正数负数相加')
    def test_three(self):
        assert add(3, -4) == -1

    @allure.story('04测试两个负数相加')
    def test_four(self):
        assert add(-3, -4) == -7

运行:

E:\workspace-py\Pytest>pytest test_sample.py --alluredir=report --clean-alluredir
========================================================================== test session starts ==========================================================================
platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
rootdir: E:\workspace-py\Pytest
plugins: allure-pytest-2.8.18, assume-2.3.3, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
collected 4 items                                                                                                                                                        

test_sample.py ....                                                                                                                                                [100%]

=========================================================================== 4 passed in 0.06s ===========================================================================

报告:

你也可以通过--allure-features--allure-stories选择指定具体功能和故事运行,多个以逗号分隔。

E:\workspace-py\Pytest>pytest test_sample.py --allure-stories 01测试两个正数相加
========================================================================== test session starts ==========================================================================
platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
rootdir: E:\workspace-py\Pytest
plugins: allure-pytest-2.8.18, assume-2.3.3, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
collected 4 items                                                                                                                                                        

test_sample.py .                                                                                                                                                   [100%]

=========================================================================== 1 passed in 0.02s ===========================================================================

3、步骤

如果你想对每个测试调用进行非常详细的逐步说明,可以通过@allure.step装饰器来实现(固件同样支持)。

该装饰器会将方法或函数的调用与提供的参数一起添加到报表中,并且可以包含一条描述行,该行支持位置和关键字参数。

示例:

# test_sample.py
import pytest
import allure

@allure.step('两数相加:{0}   {y}')
def add(x, y):
    r = x   y
    print_res(r)
    return r

@allure.step
def print_res(r):
    print('计算结果:', r)

class TestLearning:
    data = [
        [3, 4, 7],
        [-3, 4, 1],
        [3, -4, -1],
        [-3, -4, -7],
    ]
    @pytest.mark.parametrize("data", data)
    def test_add(self, data):
        assert add(data[0], data[1]) == data[2]

报告:

4、标题

如果你想让测试标题更具可读性,可以使用@allure.title装饰器,该装饰器支持参数的占位符并支持动态替换。

示例:

# test_sample.py
import pytest
import allure
def add(x, y):
    return x   y
class TestLearning:
    data = [
        [3, 4, 7],
        [-3, 4, 1],
        [3, -4, -1],
        [-3, -4, -7],
    ]
    @allure.title("测试用例-{data}")
    @pytest.mark.parametrize("data", data)
    def test_add(self, data):
        assert add(data[0], data[1]) == data[2]

报告:

5、描述

如果你想添加测试的详细说明,可以通过添加测试方法描述信息,也可以使用装饰器@allure.description@allure.description_html

示例:

# test_sample.py
import allure

# 被测功能
def add(x, y):
    return x   y

# 测试类
class TestLearning:

    @allure.description('测试正数相加')
    def test_first(self):
        assert add(3, 4) == 7

    @allure.description_html('<h1> 测试负数相加 </h1>')
    def test_second(self):
        """你也可以在这里添加用例的描述信息,但是会被allure装饰器覆盖"""
        assert add(-3, -4) == -7

报告:

6、附件

如果你想在报告中显示不同类型的附件,可以通过以下两种方式来实现:
allure.attach(body, name, attachment_type, extension)
allure.attach.file(source, name, attachment_type, extension)

示例:

import allure

def test_multiple_attachments():
    allure.attach.file(r'C:\Users\Public\Pictures\Sample Pictures\Koala.jpg', attachment_type=allure.attachment_type.JPG)
    allure.attach('<head></head><body> 测试页面 </body>', 'Attach with HTML type', allure.attachment_type.HTML)

报告:

7、链接

如果你想关联缺陷追踪系统或测试管理系统,你可以使用装饰器@allure.link@allure.issue@allure.testcase

示例:

import allure

@allure.link('http://www.baidu.com')
def test_with_named_link():
    pass

@allure.issue('101', '缺陷问题描述')
def test_with_issue_link():
    pass

@allure.testcase('http://this.testcase.com', '测试用例标题')
def test_with_testcase_link():
    pass

报告:

注意:

@allure.issue 将提供带有小错误图标的链接,该描述符将测试用例ID作为输入参数,以将其与提供的问题链接类型的链接模板一起使用。
链接模板在 --allure-link-patternPytest 的配置选项中指定。链接模板和类型必须使用冒号指定:
pytest test_sample.py --alluredir=report --allure-link-pattern=issue:http://www.mytesttracker.com/issue/{}

官方文档地址:https://docs.qameta.io/allure/

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持Devmax。

Pytest测试报告工具Allure的高级用法的更多相关文章

  1. Pytest+Request+Allure+Jenkins实现接口自动化

    这篇文章介绍了Pytest+Request+Allure+Jenkins实现接口自动化的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  2. pytest生成简单自定义测试结果的html报告

    这篇文章主要为大家介绍了pytest生成简单自定义测试结果html报告,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  3. React团队测试并发特性详解

    这篇文章主要为大家介绍了React团队测试并发特性详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  4. python自动化测试之破解图文验证码

    这篇文章介绍了python自动化测试之破解图文验证码的解决方案,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  5. Cli Todo命令行todo工具使用演示

    这篇文章主要为大家介绍了Cli Todo命令行todo工具使用演示示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  6. 时间处理工具 dayjs使用示例详解

    这篇文章主要为大家介绍了时间处理工具 dayjs使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  7. 测试你对技术的掌握度:JSP程序员成长之路

    文/bingo_ge在本文章中使用精通、熟练、熟悉、了解标志你对某技术的掌握程度。能使用此技术优化性能或代码,做到最大可能的重用。二:基本要求1:html掌握程度:熟练。Oracle,DB2,Mysql,Postgresql.6:xml掌握程度:了解原因:AppServer的配置一般是使用XML来实现的。

  8. Pytest测试报告工具Allure的高级用法

    这篇文章介绍了Pytest测试报告工具Allure的高级用法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  9. springboot使用小工具之Lombok、devtools、Spring Initailizr详解

    这篇文章主要介绍了springboot使用小工具之Lombok、devtools、Spring Initailizr详解,Lombok可以代替手写get、set、构造方法等,需要idea装插件lombok,本文通过示例代码给大家介绍的非常详细,需要的朋友可以参考下

  10. 全网非常详细的pytest配置文件

    本文主要介绍了全网非常详细的pytest配置文件,pytest的主配置文件,可以改变pytest的默认行为,有很多可配置的选项,感兴趣的可以了解一下

随机推荐

  1. 10 个Python中Pip的使用技巧分享

    众所周知,pip 可以安装、更新、卸载 Python 的第三方库,非常方便。本文小编为大家总结了Python中Pip的使用技巧,需要的可以参考一下

  2. python数学建模之三大模型与十大常用算法详情

    这篇文章主要介绍了python数学建模之三大模型与十大常用算法详情,文章围绕主题展开详细的内容介绍,具有一定的参考价值,感想取得小伙伴可以参考一下

  3. Python爬取奶茶店数据分析哪家最好喝以及性价比

    这篇文章主要介绍了用Python告诉你奶茶哪家最好喝性价比最高,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧

  4. 使用pyinstaller打包.exe文件的详细教程

    PyInstaller是一个跨平台的Python应用打包工具,能够把 Python 脚本及其所在的 Python 解释器打包成可执行文件,下面这篇文章主要给大家介绍了关于使用pyinstaller打包.exe文件的相关资料,需要的朋友可以参考下

  5. 基于Python实现射击小游戏的制作

    这篇文章主要介绍了如何利用Python制作一个自己专属的第一人称射击小游戏,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起动手试一试

  6. Python list append方法之给列表追加元素

    这篇文章主要介绍了Python list append方法如何给列表追加元素,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  7. Pytest+Request+Allure+Jenkins实现接口自动化

    这篇文章介绍了Pytest+Request+Allure+Jenkins实现接口自动化的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  8. 利用python实现简单的情感分析实例教程

    商品评论挖掘、电影推荐、股市预测……情感分析大有用武之地,下面这篇文章主要给大家介绍了关于利用python实现简单的情感分析的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下

  9. 利用Python上传日志并监控告警的方法详解

    这篇文章将详细为大家介绍如何通过阿里云日志服务搭建一套通过Python上传日志、配置日志告警的监控服务,感兴趣的小伙伴可以了解一下

  10. Pycharm中运行程序在Python console中执行,不是直接Run问题

    这篇文章主要介绍了Pycharm中运行程序在Python console中执行,不是直接Run问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

返回
顶部