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

 更新时间:2022年07月08日 08:49:28   作者:小旭2021  
这篇文章介绍了Pytest测试报告工具Allure的高级用法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

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/

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

相关文章

  • Python中创建表格详细过程

    Python中创建表格详细过程

    这篇文章主要介绍了Python中创建表格,在python中使用tabulate库来创建表格,并针对输出形式进行不断改进来美化输出效果,并给出了代码示例。需要的小伙伴可以参考一下
    2022-02-02
  • python内置数据类型之列表操作

    python内置数据类型之列表操作

    数据类型是一种值的集合以及定义在这种值上的一组操作。这篇文章主要介绍了python内置数据类型之列表的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-11-11
  • Python selenium get_cookies获取cookie不全的解决方案

    Python selenium get_cookies获取cookie不全的解决方案

    之前使用爬虫时最让我头疼的就是cookie失效的问题了,下面这篇文章主要给大家介绍了关于Python selenium get_cookies获取cookie不全的解决方案,需要的朋友可以参考下
    2022-10-10
  • Python条件语句与循环语句

    Python条件语句与循环语句

    这篇文章主要介绍了Python条件语句与循环语句,条件语句就是通过指定的表达式的运行结果来判断当前是执行还是跳过某些指定的语句块,循环语句就是对某些语句的重复执行,这个重复执行是通过指定表达式来控制的,下面来看具体内容及续航管案例吧,需要的朋友可以参考一下
    2021-11-11
  • Flask框架编写文件下载接口过程讲解

    Flask框架编写文件下载接口过程讲解

    这篇文章主要介绍了Flask框架编写文件下载接口的过程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2023-01-01
  • Python字典底层实现原理详解

    Python字典底层实现原理详解

    今天小编就为大家分享一篇Python字典底层实现原理详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-12-12
  • python版本的仿windows计划任务工具

    python版本的仿windows计划任务工具

    这篇文章主要介绍了python版本的仿windows计划任务工具,计划任务工具根据自己设定的具体时间,频率,命令等属性来规定所要执行的计划,当然功能不是很全大家可以补充
    2018-04-04
  • NumPy 数组使用大全

    NumPy 数组使用大全

    这篇文章主要介绍了NumPy 数组使用大全,在本教程中,你将学习如何在 NumPy 数组上以多种方式添加、删除、排序和操作元素。 文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-04-04
  • python读写修改Excel之xlrd&xlwt&xlutils

    python读写修改Excel之xlrd&xlwt&xlutils

    这篇文章主要介绍了python读写修改Excel之xlrd&xlwt&xlutils,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • 解决TensorFlow训练内存不断增长,进程被杀死问题

    解决TensorFlow训练内存不断增长,进程被杀死问题

    今天小编就为大家分享一篇解决TensorFlow训练内存不断增长,进程被杀死问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02

最新评论