pytest(六)——allure-pytest的基础使用

前言

一、allure-pytest的基础使用

二、需要掌握的allure特性

[2.1 Allure报告结构](#2.1 Allure报告结构)

[2.2 Environment](#2.2 Environment)

[2.3 Categories](#2.3 Categories)

[2.4 Flaky test](#2.4 Flaky test)

三、allure的特性,@allure.step()、allure.attach的详细使用

[3.1 @allure.step](#3.1 @allure.step)

[3.2 allure.attach(挺有用的)](#3.2 allure.attach(挺有用的))

参数列表

四、allure的特性,@allure.description()、@allure.title()的详细使用

[4.1 @allure.description()](#4.1 @allure.description())

[4.2 @allure.title()](#4.2 @allure.title())

作用

[1. @pytest.mark.parametrize](#1. @pytest.mark.parametrize)

[2. 参数列表](#2. 参数列表)

[3. indirect=True](#3. indirect=True)

[五、 allure的特性,@allure.link()、@allure.issue()、@allure.testcase()的详细使用](#五、 allure的特性,@allure.link()、@allure.issue()、@allure.testcase()的详细使用)

总结

[六、allure 打标记之 @allure.epic()、@allure.feature()、@allure.story() 的详细使用](#六、allure 打标记之 @allure.epic()、@allure.feature()、@allure.story() 的详细使用)

[6.1 allure的标记装饰器](#6.1 allure的标记装饰器)

[6.2 BDD标记装饰器](#6.2 BDD标记装饰器)

[1. @allure.epic](#1. @allure.epic)

[2. @allure.feature](#2. @allure.feature)

[3. @allure.story](#3. @allure.story)

[七、allure 环境准备](#七、allure 环境准备)

[7.1 allure 和 pytest 相关环境安装](#7.1 allure 和 pytest 相关环境安装)

[7.2 配置 allure 环境变量](#7.2 配置 allure 环境变量)

[八、allure.severity 标记用例级别](#八、allure.severity 标记用例级别)

[8.1 Allure 提供的用例级别](#8.1 Allure 提供的用例级别)

总结

[九、清空 allure 历史报告记录](#九、清空 allure 历史报告记录)

[十、 allure 命令行参数](#十、 allure 命令行参数)

参考文献

前言

  1. Allure Framework是一种灵活的轻量级多语言测试报告工具,不仅可以以简洁的Web报告形式非常简洁地显示已测试的内容,也允许参与开发过程的每个人从日常测试中提取最大程度的有用信息
  2. 从开发/质量保证的角度来看,Allure报告可以缩短常见缺陷的生命周期:可以将测试失败划分为bug和损坏的测试,还可以配置log,step,fixture,attachments,timings,历史记录以及与TMS的集成以及Bug跟踪系统,因此负责任的开发人员和测试人员将掌握所有信息
  3. 从管理人员的角度来看,Allure提供了一个清晰的"全局",涵盖了已涵盖的功能,缺陷聚集的位置,执行时间表的外观以及许多其他方便的事情
  4. Allure的模块化和可扩展性确保您始终能够微调某些东西,以使Allure更适合您
  5. 对于管理层来说,测试报告当然是越直观、简洁、数据清晰越好,而Allure就满足以上这么多点,而且很好的和pytest集成了
  6. 相比于pytest-html来说,Allure的报告真的是十全十美鸭!!
  7. 唯一不足的就是,拓展功能需要在测试用例集上加装饰器

一、allure-pytest的基础使用

安装插件

复制代码
pip3 install allure-pytest -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

代码的目录结构如下所示

复制代码
# 是项目文件夹名称
18allure
│  conftest.py
│  test_1.py
│  __init__.py
│              
├─test_51job
│  │  conftest.py
│  │  test_case1.py
│  │  __init__.py 
│          
├─test_toutiao
│  │  test_case2.py
│
├─test_weibo
│  │  conftest.py
│  │  test_case3.py
│  │  __init__.py 
│  

最外层的conftest.py代码

python 复制代码
# 外层conftest.py
import pytest
@pytest.fixture(scope="session")
def login():
    print("====登录功能,返回账号,token===")
    name = "我是最外层conftest的name"
    token = "我是最外层conftest的token"
    yield name, token
    print("====退出登录!!!====")

最外层的test_1.py文件

python 复制代码
from time import sleep
import pytest
@pytest.mark.parametrize("n", list(range(5)))
def test_get_info(login, n):
    sleep(1)
    name, token = login
    print("***基础用例:获取用户个人信息***", n)
    print(f"用户名:{name}, token:{token}")

test_51job包下的conftest.py文件

python 复制代码
import pytest
@pytest.fixture(scope="module")
def open_51(login):
    name, token = login
    print(f"###用户 {name} 打开51job网站###")

test_51job包下的test_case1.py文件

python 复制代码
from time import sleep
import pytest
@pytest.mark.parametrize("n", list(range(5)))
def test_case2_01(open_51, n):
    sleep(1)
    print("51job,列出所有职位用例", n)
@pytest.mark.parametrize("n", list(range(5)))
def test_case2_02(open_51, n):
    sleep(1)
    print("51job,找出所有python岗位", n)

test_toutiao包下的test_case2.py文件

python 复制代码
from time import sleep
import pytest
@pytest.mark.parametrize("n", list(range(5)))
def test_no_fixture(login, n):
    sleep(1)
    print("==没有__init__测试用例,我进入头条了==", login)

test_weibo包下的conftest.py文件

python 复制代码
import pytest
@pytest.fixture(scope="function")
def open_weibo(login):
    name, token = login
    print(f"&&& 用户 {name} 返回微博首页 &&&")

test_weibo包下的test_case3.py文件

python 复制代码
from time import sleep
import pytest
@pytest.mark.parametrize("n", list(range(5)))
class TestWeibo:
    def test_case1_01(self, open_weibo, n):
        sleep(1)
        print("查看微博热搜", n)
    def test_case1_02(self, open_weibo, n):
        sleep(1)
        print("查看微博范冰冰", n)

在终端命令行中输入

python 复制代码
pytest -n auto --alluredir=allure

或者使用脚本运行代码

python 复制代码
import pytest

if __name__ == '__main__':
    pytest.main(["-n", "auto", "--alluredir=allure"])

要在测试完成后查看实际报告,需要使用Allure命令行来让测试结果生成报告

python 复制代码
allure serve allure

运行的截图

通过网页进行访问操作

二、需要掌握的allure特性

2.1 Allure报告结构

  • Overview:总览

  • Categories:类别,默认是分了failed和error,凡是执行结果是其中一个的都会被归到类里面,可以通过这里快捷查看哪些用例是failed和error的

  • Suites:测试套件,就是所有用例的层级关系,可以根据package、module、类、方法来查找用例

  • Graphs:测试结果图形化,包括用例执行结果的分布图,优先级,耗时等

  • Timeline:可以看到测试用例精确的测试时序(执行顺序),包括执行时间

  • Behaviors:行为驱动,根据epic、feature、story来分组测试用例(后面会讲到)

  • Packages:这就是按照package、module来分组测试用例了

前面我们介绍了allure的快速入门,只是单纯的敲allure命令而已

其实allure还有内置的特性可以让我们在pytest代码里面用起来,然后我们生成的报告更加直观、详细、贴合管理层的心意...

2.2 Environment

可以理解成环境变量参数,没有什么实际作用,个人觉得只是为了让别人知道本次测试的运行环境参数而已,显示啥都是自己定的

注意!!默认是没有的哦

如何添加Environment呢

通过创建environment.properties或者environment.xml文件,并把文件存放到allure-results(这个目录是生成最后的html报告之前,生成依赖文件的目录)目录下,就是 --alluredir 后面跟的目录

像我这里目录就是allure,所以放在allure下面 --alluredir allure

environment.properties

python 复制代码
Browser=Chrome
Browser.Version=81.0.4044.92
Stand=Production
ApiUrl=127.0.0.1/login
python.Version=3.7.2

或者environment.xml

XML 复制代码
<environment>
    <parameter>
        <key>Browser</key>
        <value>Chrome</value>
    </parameter>
    <parameter>
        <key>Browser.Version</key>
        <value>81.0.4044.92</value>
    </parameter>
    <parameter>
        <key>Stand</key>
        <value>Production</value>
    </parameter>
        <parameter>
        <key>ApiUrl</key>
        <value>127.0.0.1/login</value>
    </parameter>
        <parameter>
        <key>python.Version</key>
        <value>3.7.2</value>
    </parameter>
</environment>

2.3 Categories

**直译:**分类

**通俗理解:**测试用例结果的分类

默认情况下,有两类缺陷:

  1. Product defects 产品缺陷(测试结果:failed)
  2. Test defects 测试缺陷(测试结果:error/broken)

我们是可以创建自定义缺陷分类的,将 categories.json 文件添加到allure-results目录即可(和上面environment.properties放同一个目录)

categories.json

python 复制代码
[
  {
    "name": "Ignored tests", 
    "matchedStatuses": ["skipped"] 
  },
  {
    "name": "Infrastructure problems",
    "matchedStatuses": ["broken", "failed"],
    "messageRegex": ".*bye-bye.*" 
  },
  {
    "name": "Outdated tests",
    "matchedStatuses": ["broken"],
    "traceRegex": ".*FileNotFoundException.*" 
  },
  {
    "name": "Product defects",
    "matchedStatuses": ["failed"]
  },
  {
    "name": "Test defects",
    "matchedStatuses": ["broken"]
  }
]

讲下参数的含义

  • name:分类名称
  • matchedStatuses:测试用例的运行状态,默认["failed", "broken", "passed", "skipped", "unknown"]
  • messageRegex:测试用例运行的错误信息,默认是 .* ,是通过正则去匹配的哦!
  • traceRegex:测试用例运行的错误堆栈信息,默认是 .* ,也是通过正则去匹配的哦!

注意:这里的name是可以写中文的哦!

  • failed:测试未通过(断言失败)。
  • broken:测试运行中出现错误(未处理的异常)。
  • passed:测试成功通过(所有断言成立)。
  • skipped:测试被跳过(显式跳过)。
  • unknown:测试状态无法确定(较少见)。
python 复制代码
import pytest

# 1. 测试用例:失败(failed)
def test_failed():
    """测试用例未通过,断言失败"""
    # 这里我们尝试断言 1 + 1 等于 3,这显然是错误的
    assert 1 + 1 == 3  # 这个断言会失败

# 2. 测试用例:通过(passed)
def test_passed():
    """测试用例成功通过,所有断言均成立"""
    assert 2 + 2 == 4  # 这个断言会成功

# 3. 测试用例:被跳过(skipped)
@pytest.mark.skip(reason="Skipping this test")
def test_skipped():
    """测试用例被显式跳过,不会执行"""
    assert 1 + 1 == 2  # 这个测试不会执行

# 4. 测试用例:运行中出现错误(broken)
def test_broken():
    """测试用例在执行过程中抛出异常,导致无法正常运行"""
    raise Exception("An error occurred")  # 这个测试会被标记为 broken

if __name__ == '__main__':
    pytest.main()

2.4 Flaky test

**用法:**在类或者方法上直接加 @Flaky

**官方也说了:**可以将整个测试类标记为Flaky

那什么是Flaky呢?

  • 简单来说就是,不够稳定的测试用例集,有可能前阵子还运行成功,过阵子就运行失败,理解成"闪烁"

  • 标记成Flaky的好处就是:当用例失败的情况下,我们能获取足够详细的信息,毕竟有可能某些测试用例是非常重要的

  • 如果不标记为Flaky的话,可能就要禁用这些测试

三、allure的特性,@allure.step()、allure.attach的详细使用

allure除了支持pytest自带的特性之外(fixture、parametrize、xfail、skip),自己本身也有强大的特性可以在pytest中使用

3.1 @allure.step

  • allure报告最重要的一点是,它允许对每个测试用例进行非常详细的步骤说明
  • 通过 @allure.step() 装饰器,可以让测试用例在allure报告中显示更详细的测试过程
python 复制代码
import allure
@allure.step("第一步")
def passing_step():
    print("我是第一步过程")
@allure.step("第二步")
def step_with_nested_steps():
    print("我是第二步过程")
    nested_step()
@allure.step("第三步")
def nested_step():
    print("我是第三步过程")
    nested_step_with_arguments(1, 'abc')
@allure.step("第四步{0},{arg2}")
def nested_step_with_arguments(arg1, arg2):
    print("我是第四步过程")
@allure.step("第五步")
def test_with_nested_steps():
    print("我是第五步过程")
    passing_step()
    step_with_nested_steps()

知识点

  • step() 只有一个参数,就是title,你传什么,在allure上就显示什么

  • 可以像python字符串一样,支持位置参数和关键字参数 {0},{arg2},可看第四步那里,如果函数的参数没有匹配成功就会报错哦

  • step() 的使用场景,给我感觉就是,当方法之间嵌套会比较有用,否则的话只会显示一个步骤,类似下面图

3.2 allure.attach(挺有用的)

**作用:**allure报告还支持显示许多不同类型的附件,可以补充测试结果;自己想输出啥就输出啥,挺好的

语法: allure.attach(body, name, attachment_type, extension)

参数列表

  • body:要显示的内容(附件)
  • name:附件名字
  • attachment_type:附件类型,是 allure.attachment_type 里面的其中一种
  • extension:附件的扩展名(比较少用)

allure.attachment_type提供了哪些附件类型?

python 复制代码
import pytest
import allure
import os
def test_with_attachment():
    """演示如何使用 allure.attach 来附加信息"""
    # 附加文本信息
    allure.attach("这是一些附加文本信息", name="附加文本", attachment_type=allure.attachment_type.TEXT)
    # 附加图像(假设有一张图像文件存在)
    image_path = r"D:\workplace20240513\pythonbase\python测试学习\01pytest学习\demo9-allure\test_allure\screenshot.png"  # 图像文件路径
    if os.path.exists(image_path):
        with open(image_path, "rb") as f:
            allure.attach(f.read(), name="屏幕截图", attachment_type=allure.attachment_type.PNG)
    else:
        print("未找到图像文件,跳过该步骤。")
    # 进行一个简单的断言
    assert 1 + 1 == 2  # 这个断言会成功

if __name__ == '__main__':
    pytest.main()

语法二: allure.attach.file(source, name, attachment_type, extension)

source:文件路径,相当于传一个文件

其他参数和上面的一致

python 复制代码
import allure
import pytest
@pytest.fixture
def attach_file_in_module_scope_fixture_with_finalizer(request):
    allure.attach('在fixture前置操作里面添加一个附件txt', 'fixture前置附件', allure.attachment_type.TEXT)
    def finalizer_module_scope_fixture():
        allure.attach('在fixture后置操作里面添加一个附件txt', 'fixture后置附件',
                      allure.attachment_type.TEXT)
    request.addfinalizer(finalizer_module_scope_fixture)

def test_with_attacments_in_fixture_and_finalizer(attach_file_in_module_scope_fixture_with_finalizer):
    pass

def test_multiple_attachments():
    allure.attach('<head></head><body> 一个HTML页面 </body>', 'Attach with HTML type', allure.attachment_type.HTML)
    allure.attach.file('./reports.html', attachment_type=allure.attachment_type.HTML)

这是一个用了 allure.attach() 来插入一段自己写的HTML和 allure.attach.file() 来导入一个已存在的HTML文件(pytest-html报告)

四、allure的特性,@allure.description()、@allure.title()的详细使用

上一篇文章介绍了两种allure的特性

  • **@allure.step() 装饰器:**可以设置测试步骤,让测试用例的执行过程更加详细
  • **allure.attach() 函数:**可以设置需要显示在allure报告的附件,包含了多种类型,可以通过allure.attachment_type查看支持的类型

这一篇幅,我们主要来讲解另外两个特性,可以增加报告的可读性哦!

  • @allure.description()
  • @allure.title()

它们用法极其相近,只是作用不一样而已

4.1 @allure.description()

作用

可以添加足够详细的测试用例描述,以便于管理层查看哦哈哈哈

语法格式,有三种

  1. @allure.description(str)

  2. 在测试用例函数声明下方添加 """ """

  3. @allure.description_html(str):相当于传一个HTML代码组成的字符串,类似 allure.attach() 中传HTML

**注意:**方式一方式二的效果和作用是一致的, 哪个方便哪个来

python 复制代码
import allure
# 方式一
@allure.description("""
这是一个@allure.description装饰器
没有特别的用处
""")
def test_description_from_decorator():
    assert 42 == int(6 * 7)
# 方式二
def test_unicode_in_docstring_description():
    """
    当然,在方法声明的下一行这样子写,也算一种添加description的方式哦
    """
    assert 42 == int(6 * 7)
# 方式三
@allure.description_html("""
<h1>Test with some complicated html description</h1>
<table style="width:100%">
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
  </tr>
  <tr align="center">
    <td>William</td>
    <td>Smith</td>
</table>
""")
def test_html_description():
    assert True

4.2 @allure.title()

作用

  • 使得测试用例的标题更具有可读性,毕竟我们可以写成中文
  • 支持占位符传递关键字参数哦(动态标题,结合 @pytest.mark.parametrize 使用)
python 复制代码
import pytest, allure
@allure.title("前置操作:登录")
@pytest.fixture
def test_loginss(request):
    params = request.param
    name = params["username"]
    pwd = params["pwd"]
    allure.attach(f"这是测试用例传的参数{params}")
    print(name, pwd, params)
    return name, pwd
@allure.title("成功登录,测试数据是:{test_loginss}")
@pytest.mark.parametrize("test_loginss", [
    {"username": "name1", "pwd": "pwd1"},
    {"username": "name2", "pwd": "pwd2"}], indirect=True)
def test_success_login(test_loginss):
    name, pwd = test_loginss
    allure.attach(f"账号{name},密码{pwd}")

如果没有添加 @allure.title() 的话,测试用例的标题默认就是函数名,这样的可读性不高,毕竟咱们是中国人,显示中文title还是很有必要的~所以墙裂建议大伙儿加上啦!

@pytest.mark.parametrize 是 pytest 提供的一个装饰器,用于参数化测试用例。它允许你为测试函数提供多个输入参数,从而在不同的输入下执行相同的测试逻辑。

在你给出的例子中,使用了 indirect=True,这是一个很重要的特性。以下是对这段代码的详细解释。

python 复制代码
@pytest.mark.parametrize("test_loginss", [
    {"username": "name1", "pwd": "pwd1"},
    {"username": "name2", "pwd": "pwd2"}
], indirect=True)
1. @pytest.mark.parametrize
  • 作用 :它用于生成多个测试用例。这里,test_loginss 是参数的名称,后面的列表是提供给这个参数的值。
2. 参数列表
  • 列表中的每个字典代表一个测试用例的输入。这里定义了两个字典:
    • 第一个字典:{"username": "name1", "pwd": "pwd1"}
    • 第二个字典:{"username": "name2", "pwd": "pwd2"}
3. indirect=True
  • 作用 :当你使用 indirect=True 时,pytest 会将参数值作为 Fixture 的输入。也就是说,test_loginss 不会直接作为测试参数,而是会查找一个名为 test_loginss 的 Fixture,并将这两个字典作为输入传递给该 Fixture。

  • 这意味着你需要定义一个名为 test_loginss 的 Fixture,来处理这些参数。

五、 allure的特性,@allure.link()、@allure.issue()、@allure.testcase()的详细使用

上一篇文章介绍了两种allure的特性

  • @allure.description() :添加测试用例描述,一共三种方式哦!
  • @allure.title():指定测试用例标题,默认是函数名哦!

这一篇幅,我们主要来讲解最后三个常见特性,主要是为了将allure报告和测试管理系统集成,可以更快速的跳转到公司内部地址

  • @allure.link()
  • @allure.issue()
  • @allure.testcase()

直接上代码

python 复制代码
import allure

TEST_CASE_LINK = 'https://github.com/qameta/allure-integrations/issues/8#issuecomment-268313637'
@allure.link('https://www.youtube.com/watch?v=4YYzUTYZRMU')
def test_with_link():
    pass

@allure.link('https://www.youtube.com/watch?v=Su5p2TqZxKU', name='点击我看一看youtube吧')
def test_with_named_link():
    pass
@allure.issue('140', 'bug issue链接')
def test_with_issue_link():
    pass

@allure.testcase(TEST_CASE_LINK, '测试用例地址')
def test_with_testcase_link():
    pass

知识点

  • issue()和testcase()其实调用的也是link(),只是link_type不一样

  • 必传参数 url:跳转的链接

  • 可选参数 name:显示在allure报告的名字,如果不传就是显示完整的链接;建议传!!不然可读性不高

  • 可以理解成:三个方法是一样的,我们都提供跳转链接和名字,只是链接的type不一样,最终显示出来的样式不一样 而已【type不一样,样式不一样】

  • 如果你喜欢,只用@allure.link()也可以

  • 而出现三个装饰器的原因是为了更好地将链接分类【访问链接、Bug链接、测试用例链接】

运行结果,查看allure报告

@allure.link()不传name参数时的样式

不传name的话,如果链接很长,可读性就比较差啦!

@allure.link()传了name参数时的样式

@allure.testcase()的样式

其实跟link()没有太大区别.....

@allure.issue()的样式

多了个虫子哈哈哈哈

总结

  • 为了减少程序的阅读复杂性,其实可以统一用@allure.link()
  • 传name,写好链接描述,就知道这个链接是干嘛的啦,反正三个装饰器的作用都是一样的,就是样式略微不同....

六、allure 打标记之 @allure.epic()、@allure.feature()、@allure.story() 的详细使用

  • 前面几篇文章主要介绍了allure的特性,这篇文章我们就来讲下allure的标记用法
  • 有时候我们写pytest的时候,会用到 @pytest.mark 但并不会显示在allure报告上
  • 而allure也提供了三种类型的标记装饰器,它们是可以显示在allure报告上的

6.1 allure的标记装饰器

  • BDD样式的标记装饰器
  • 优先级(严重程度)标记装饰器
  • 自定义标记装饰器

6.2 BDD标记装饰器

提供了三个装饰器

  • **@allure.epic:**敏捷里面的概念,定义史诗,往下是 feature
  • **@allure.feature:**功能点的描述,理解成模块往下是 story
  • **@allure.story:**故事,往下是 title
python 复制代码
import allure
def test_without_any_annotations_that_wont_be_executed():
    pass
@allure.story('epic_1')
def test_with_epic_1():
    pass
@allure.story('story_1')
def test_with_story_1():
    pass
@allure.story('story_2')
def test_with_story_2():
    pass
@allure.feature('feature_2')
@allure.story('story_2')
def test_with_story_2_and_feature_2():
    pass

在 BDD(行为驱动开发)和 Allure 报告中,@allure.epic@allure.feature@allure.story 这三个装饰器用于组织和描述测试用例的结构,帮助团队更好地理解测试的目的和背景。

1. @allure.epic

  • 定义:表示一个大型的主题或目标,通常是一个项目或一个较大的功能模块的顶层描述。
  • 实际应用 :例如,在一个电商平台的测试中,可以用 @allure.epic("电商平台") 来标识整个电商项目的测试。所有与电商相关的功能都可以归类到这个史诗下。

2. @allure.feature

  • 定义:表示某个具体的功能点或模块,通常是史诗中的一个子功能。
  • 实际应用 :继续上面的电商平台例子,可以用 @allure.feature("用户管理") 来描述与用户注册、登录、信息修改等相关的测试用例。这有助于团队快速定位某个具体功能的测试情况。

3. @allure.story

  • 定义:表示一个具体的用户故事,通常是用户在使用某个功能时的特定场景。
  • 实际应用 :在用户管理模块下,可以用 @allure.story("用户注册") 来描述与用户注册相关的测试用例。这可以帮助团队理解每个测试用例的具体目的,如"验证用户注册成功"或"检查重复注册时的错误提示"。

示例代码

python 复制代码
import allure
import pytest

@allure.epic("电商平台")
class TestECommerce:
    @allure.feature("用户管理")
    @allure.story("用户注册")
    def test_user_registration(self):
        """测试用户注册功能"""
        assert True  # 模拟注册逻辑
    @allure.feature("用户管理")
    @allure.story("用户登录")
    def test_user_login(self):
        """测试用户登录功能"""
        assert True  # 模拟登录逻辑
    @allure.feature("商品管理")
    @allure.story("添加商品")
    def test_add_product(self):
        """测试添加商品功能"""
        assert True  # 模拟添加商品逻辑

知识点

  • story 是 feature 的子集,当测试用例有 @allure.feature、@allure.story 时,在报告上会先显示 feature,点开之后再显示 story**【可以想象成,安徒生童话(feature)有很多个童话故事(story)】**

  • 如果不加 @allure.feature、@allure.story 时,在Behaviors栏目下,测试用例都不会分类显示,当用例多的时候可能看的花里胡哨

七、allure 环境准备

7.1 allure 和 pytest 相关环境安装

python 复制代码
# allure
pip3 install allure-pytest -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

#pytest
pip3 install pytest -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

7.2 配置 allure 环境变量

allure是一个命令行工具,可以去 github 下载最新版:Releases · allure-framework/allure2 · GitHub

解压然后配置环境

八、allure.severity 标记用例级别

  • 平时写测试用例也会划分优先级
  • 同样,allure 也提供用例级别,在 allure 报告可以清晰看到不同级别用例的缺陷数量

在 Allure 报告中,你可以通过用例级别来划分测试用例的优先级,从而更清晰地了解不同优先级下的缺陷数量。这种优先级划分通常用于帮助团队识别哪些测试用例是最重要的,优先处理那些影响较大的缺陷。

8.1 Allure 提供的用例级别

Allure 报告允许你定义以下几个级别的测试用例:

  • Blocker:阻止功能正常工作的缺陷,必须立即解决。
  • Critical:对系统的重要功能造成重大影响的缺陷,需要尽快修复。
  • Major:对功能有明显影响的缺陷,但不阻止整体功能的使用。
  • Minor:影响较小的缺陷,优先级最低。
python 复制代码
import allure
def test_with_no_severity_label():
    pass
@allure.severity(allure.severity_level.TRIVIAL)
def test_with_trivial_severity():
    print("trivial: 轻微缺陷(必须项无提示,或者提示不规范)")
@allure.severity(allure.severity_level.NORMAL)
def test_with_normal_severity():
    print("normal: 一般缺陷(边界情况,格式错误)")

@allure.severity(allure.severity_level.NORMAL) # normal: 一般缺陷(边界情况,格式错误)
class TestClassWithNormalSeverity(object):
    def test_inside_the_normal_severity_test_class(self):
        """ 测试类优先级 normal;看看测试用例是否会自动继承优先级 """
        print()

    @allure.severity(allure.severity_level.CRITICAL)
    def test_inside_the_normal_severity_test_class_with_overriding_critical_severity(self):
        """
        测试类优先级 normal
        测试用例优先级 critical
        """
        print("critical:严重缺陷(功能点缺失)")
@allure.severity("normal")
def test_case_1():
    """ normal 级别测试用例 """
    print("test case 11111111")

@allure.severity("critical")
def test_case_2():
    """ critical 级别测试用例 """
    print("test case 222222222")
@allure.severity("blocker")
def test_case_3():
    """ blocker 级别测试用例 """
    print("test case 4444444")

@allure.severity("minor")
def test_case_4():
    """ minor 级别测试用例 """
    print("test case 11111111")

def test_case_5():
    """ 没标记 severity 的用例默认为 normal"""
    print("test case 5555555555")

在 Allure 报告中,你将看到不同优先级下的测试用例和它们的执行状态。通过这种方式,你可以清晰地看到:

  • 每个级别的测试用例数量
  • 每个级别的失败用例数量
  • 方便团队在修复缺陷时优先处理那些优先级高的测试用例

总结

  • 使用 Allure 的用例级别可以有效地划分测试用例的优先级,帮助团队明确哪些缺陷需要优先解决。
  • 这种结构化的报告能够提高团队的工作效率,确保关键功能始终处于可用状态。

九、清空 allure 历史报告记录

  • pytest 运行 测试用例生成 allure 报告时,当测试用例名称修改后重新运行,会保留历史运行记录
  • 又或者分开运行两个测试用例文件,但是 allure 报告生成目录是同一个,那么 allure 报告会同时显示两个文件的测试用例运行情况
  • 咱们来看看这种情况

我觉得直接删除就是直接的方式

十、 allure 命令行参数

记住最常用的一个命令行

python 复制代码
 allure generate allure

生成报告

可以直接点击运行

参考文献

Pytest系列(18)- 超美测试报告插件之allure-pytest的基础使用 - 小菠萝测试笔记 - 博客园

使用allure serve查看报告提示allure-results does not exists的解决方法 - 奔奔-武 - 博客园

pycharm里allure serve ./results/login执行后,浏览器里打开的报告为空,解决办法_allure serve result 没效果-CSDN博客

解决allure-report下index.html文件打开空白显示数据_allure生成的html打开空白-CSDN博客

Pytest系列(19)- 我们需要掌握的allure特性 - 小菠萝测试笔记 - 博客园

Pytest系列(20)- allure的特性,@allure.step()、allure.attach的详细使用 - 小菠萝测试笔记 - 博客园

Pytest系列(21)- allure的特性,@allure.description()、@allure.title()的详细使用 - 小菠萝测试笔记 - 博客园

Pytest系列(22)- allure的特性,@allure.link()、@allure.issue()、@allure.testcase()的详细使用 - 小菠萝测试笔记 - 博客园
Pytest系列(23)- allure 打标记之 @allure.epic()、@allure.feature()、@allure.story() 的详细使用 - 小菠萝测试笔记 - 博客园

Pytest 系列(24)- allure 环境准备 - 小菠萝测试笔记 - 博客园

Pytest 系列(25)- @allure.severity 标记用例级别 - 小菠萝测试笔记 - 博客园

Allure+pytest 生成测试报告 - 三只松鼠 - 博客园

相关推荐
努力搬砖的咸鱼10 小时前
从零开始搭建 Pytest 测试框架(Python 3.8 + PyCharm 版)
python·pycharm·pytest
FINE!(正在努力!)2 天前
PyTest框架学习
学习·pytest
程序员杰哥3 天前
接口自动化测试之pytest 运行方式及前置后置封装
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
测试老哥3 天前
Pytest+Selenium UI自动化测试实战实例
自动化测试·软件测试·python·selenium·测试工具·ui·pytest
水银嘻嘻3 天前
07 APP 自动化- appium+pytest+allure框架封装
python·appium·自动化·pytest
天才测试猿4 天前
接口自动化测试之pytest接口关联框架封装
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
not coder5 天前
Pytest Fixture 详解
数据库·pytest
not coder5 天前
pytest 常见问题解答 (FAQ)
开发语言·python·pytest
程序员的世界你不懂5 天前
(1)pytest简介和环境准备
pytest
not coder5 天前
Pytest Fixture 是什么?
数据库·oracle·pytest