pytest学习和使用22-allure特性 丨总览中的Environment、Categories设置以及Flaky test使用
创始人
2025-06-01 06:36:17
0

22-allure特性 丨总览中的Environment和Categories设置

  • 1 Environment设置
    • 1.1 设置方法
    • 1.2 创建文件
  • 2 Categories设置
    • 2.1 设置方式
    • 2.2 创建文件
  • 3 关于Flaky test
    • 3.1 Flaky test介绍
    • 3.2 产生Flaky Tests的原因
    • 3.3 Flaky安装
    • 3.4 Flaky使用
    • 3.5 小结
      • 小结1
      • 小结2

  • 如下图,我们可以看到allure报告的总览,里边的一些特性是可以自定义设置的。
    在这里插入图片描述

1 Environment设置

  • Environment可以理解为环境变量;
  • 默认为空;
  • 可以自己设置。

1.1 设置方法

  • 在存放测试报目录下创建environment.properties或者environment.xml文件;
  • 而测试报告目录是使用--alluredir指定的目录,比如:
--alluredir allure-results
  • 比如之前提到的用例:
pytest -n auto --alluredir=allure-results test_xdist.py

在这里插入图片描述

1.2 创建文件

  • environment.properties
Browser=Chrome
Browser.Version=111.0.5563.65
Env=Test
IP=192.168.1.133
Allure-Pytest.Version=2.8.12
  • 运行后查看Environment
    在这里插入图片描述
  • 或者创建environment.xml
environment>BrowserChromeBrowser.Version111.0.5563.65EnvTestIP192.168.1.133Allure-Pytest.Version2.8.12

2 Categories设置

  • Categories即分类,测试用例结果的分类;
  • 默认有两种分类:
# Product defects 产品缺陷(测试结果:failed)
# Test defects 测试缺陷(测试结果:error/broken)
  • 可以自定义分类。

2.1 设置方式

  • environment方式一样,在allure-results目录中创建categories.json文件

2.2 创建文件

在这里插入图片描述

[{"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:测试用例运行的错误堆栈信息,默认.*  ,通过正则匹配
  • 写一个用例,验证下:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/3/20
# 文件名称:test_yyy.py
# 作用:allure特性categories验证
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelsonimport pytest
import timeclass TestCase01():def test_case_01(self):time.sleep(1)print("case01$$$$$$$$$$$$$$$$$$$$$")assert 1 == 2def test_case_02(self):time.sleep(1)print("case02$$$$$$$$$$$$$$$$$$$$$")assert 3 == 3def test_case_03(self):time.sleep(1)print("case03$$$$$$$$$$$$$$$$$$$$$")assert "is" in "is_you"def test_case_04(self):time.sleep(1)print("case04$$$$$$$$$$$$$$$$$$$$$")assert 5 < 10def test_case_05(self):time.sleep(1)print("case05$$$$$$$$$$$$$$$$$$$$$")assert 222 == 333def test_case_06(self):time.sleep(1)print("case06$$$$$$$$$$$$$$$$$$$$$")assert 444 > 666class TestCase02():def test_case_07(self):time.sleep(1)print("case07$$$$$$$$$$$$$$$$$$$$$")assert 10/2 == 5.0def test_case_08(self):time.sleep(1)print("case08$$$$$$$$$$$$$$$$$$$$$")assert "num" in "num_list"def test_case_09(self):time.sleep(1)print("case08$$$$$$$$$$$$$$$$$$$$$")assert "num1" in "num_list"if __name__ == '__main__':pytest.main(["-s", "test_yyy.py"])
  • 运行命令:
pytest -n auto --alluredir=allure-results test_yyy.py
  • 运行命令:
allure serve allure-results
  • 查看结果:
    在这里插入图片描述
    在这里插入图片描述

3 关于Flaky test

3.1 Flaky test介绍

  • Flaky test在被测对象和测试条件都不变的情况下,有时候失败、有时候成功的测试;
  • 实际上就是不稳定的测试,或者随机失败(随机成功)的测试;
  • 标记成Flaky是为了当用例失败的情况下,我们能获取足够详细的信息。

3.2 产生Flaky Tests的原因

  • 异步等待;
  • 并发;
  • 资源泄露;
  • 远程服务;
  • 测试依赖性。

3.3 Flaky安装

pip3 install pytest-ignore-flaky
C:\Users\Administrator>pip3 install pytest-ignore-flaky
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pytest-ignore-flakyDownloading https://pypi.tuna.tsinghua.edu.cn/packages/22/bf/4a670d28c8c37569e26536c068d83b37a01aea9fff9a45a03ae3be5344b9/pytest_ignore_flaky-2.0.0-py3-none-any.whl (3.9 kB)
Requirement already satisfied: pytest>=6.0 in d:\python37\lib\site-packages (from pytest-ignore-flaky) (6.2.4)
Requirement already satisfied: py>=1.8.2 in d:\python37\lib\site-packages (from pytest>=6.0->pytest-ignore-flaky) (1.10.0)
Requirement already satisfied: colorama in d:\python37\lib\site-packages (from pytest>=6.0->pytest-ignore-flaky) (0.4.4)
Requirement already satisfied: attrs>=19.2.0 in d:\python37\lib\site-packages (from pytest>=6.0->pytest-ignore-flaky) (20.3.0)
Requirement already satisfied: atomicwrites>=1.0 in d:\python37\lib\site-packages (from pytest>=6.0->pytest-ignore-flaky) (1.4.0)
Requirement already satisfied: iniconfig in d:\python37\lib\site-packages (from pytest>=6.0->pytest-ignore-flaky) (1.1.1)
Requirement already satisfied: importlib-metadata>=0.12 in d:\python37\lib\site-packages (from pytest>=6.0->pytest-ignore-flaky) (2.1.1)
Requirement already satisfied: toml in d:\python37\lib\site-packages (from pytest>=6.0->pytest-ignore-flaky) (0.10.2)
Requirement already satisfied: packaging in d:\python37\lib\site-packages (from pytest>=6.0->pytest-ignore-flaky) (20.8)
Requirement already satisfied: pluggy<1.0.0a1,>=0.12 in d:\python37\lib\site-packages (from pytest>=6.0->pytest-ignore-flaky) (0.13.1)
Requirement already satisfied: zipp>=0.5 in d:\python37\lib\site-packages (from importlib-metadata>=0.12->pytest>=6.0->pytest-ignore-flaky) (1.2.0)
Requirement already satisfied: pyparsing>=2.0.2 in d:\python37\lib\site-packages (from packaging->pytest>=6.0->pytest-ignore-flaky) (2.4.7)
Installing collected packages: pytest-ignore-flaky
Successfully installed pytest-ignore-flaky-2.0.0

3.4 Flaky使用

  • 再写一个用例:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/3/20
# 文件名称:test_yyy.py
# 作用:allure特性categories验证
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelsonimport pytest
import timeclass TestCase01():def test_case_01(self):time.sleep(1)print("case01$$$$$$$$$$$$$$$$$$$$$")assert 1 == 2def test_case_02(self):time.sleep(1)print("case02$$$$$$$$$$$$$$$$$$$$$")assert 3 == 3def test_case_03(self):time.sleep(1)print("case03$$$$$$$$$$$$$$$$$$$$$")assert "is" in "is_you"def test_case_04(self):time.sleep(1)print("case04$$$$$$$$$$$$$$$$$$$$$")assert 5 < 10def test_case_05(self):time.sleep(1)print("case05$$$$$$$$$$$$$$$$$$$$$")assert 222 == 333def test_case_06(self):time.sleep(1)print("case06$$$$$$$$$$$$$$$$$$$$$")assert 444 > 666class TestCase02():def test_case_07(self):time.sleep(1)print("case07$$$$$$$$$$$$$$$$$$$$$")assert 10/2 == 5.0def test_case_08(self):time.sleep(1)print("case08$$$$$$$$$$$$$$$$$$$$$")assert "num" in "num_list"@pytest.mark.flakydef test_case_09(self):time.sleep(1)print("case08$$$$$$$$$$$$$$$$$$$$$")assert "num1" in "num_list"if __name__ == '__main__':pytest.main(["-s", "test_yyy.py"])
  • 使用命令直接运行用例:
pytest -n auto --alluredir=allure-results test_yyy.py
========================================== short test summary info ===========================================
FAILED test_yyy.py::TestCase01::test_case_06 - assert 444 > 666
FAILED test_yyy.py::TestCase01::test_case_01 - assert 1 == 2
FAILED test_yyy.py::TestCase01::test_case_05 - assert 222 == 333
FAILED test_yyy.py::TestCase02::test_case_09 - AssertionError: assert 'num1' in 'num_list'
==================================== 4 failed, 5 passed, 1 rerun in 5.99s ====================================
  • 从上发现被我们使用@pytest.mark.flaky标记的用例,断言是失败的,也正常标准失败:
    在这里插入图片描述
    在这里插入图片描述
  • 命令行加上代码:--ignore-flaky重新运行:
pytest -n auto --alluredir=allure-results test_yyy.py --ignore-flaky

在这里插入图片描述

  • 发现被标记的用例变成了xfailed而不是失败了:
    在这里插入图片描述
    在这里插入图片描述

3.5 小结

小结1

  • 默认情况下, @pytest.mark.flaky 装饰器标记的测试用例默认会执行;
  • 当用例执行结果成功时正常执行正常显示用例结果;
  • 当用例执行结果失败时,测试用例默认失败重跑一次。

小结2

  • pytest命令行参数 --ignore-flaky 运行 @pytest.mark.flaky 标记的测试用例:当用例执行成功时执行结果显示正常;
  • 当用例执行失败时执行结果显示XFAIL(skip flaky test failure)

相关内容

热门资讯

安卓子系统windows11,... 你知道吗?最近科技圈可是炸开了锅,因为安卓子系统在Windows 11上的兼容性成了大家热议的话题。...
电脑里怎么下载安卓系统,电脑端... 你有没有想过,你的电脑里也能装上安卓系统呢?没错,就是那个让你手机不离手的安卓!今天,就让我来带你一...
索尼相机魔改安卓系统,魔改系统... 你知道吗?最近在摄影圈里掀起了一股热潮,那就是索尼相机魔改安卓系统。这可不是一般的改装,而是让这些专...
安卓系统哪家的最流畅,安卓系统... 你有没有想过,为什么你的手机有时候像蜗牛一样慢吞吞的,而别人的手机却能像风一样快?这背后,其实就是安...
安卓最新系统4.42,深度解析... 你有没有发现,你的安卓手机最近是不是有点儿不一样了?没错,就是那个一直在默默更新的安卓最新系统4.4...
android和安卓什么系统最... 你有没有想过,你的安卓手机到底是用的是什么系统呢?是不是有时候觉得手机卡顿,运行缓慢,其实跟这个系统...
平板装安卓xp系统好,探索复古... 你有没有想过,把安卓系统装到平板上,再配上XP系统,这会是怎样一番景象呢?想象一边享受着安卓的便捷,...
投影仪装安卓系统,开启智能投影... 你有没有想过,家里的老式投影仪也能焕发第二春呢?没错,就是那个曾经陪你熬夜看电影的“老伙计”,现在它...
安卓系统无线车载carplay... 你有没有想过,开车的时候也能享受到苹果设备的便利呢?没错,就是那个让你在日常生活中离不开的iOS系统...
谷歌安卓8系统包,系统包解析与... 你有没有发现,手机更新换代的速度简直就像坐上了火箭呢?这不,最近谷歌又发布了安卓8系统包,听说这个新...
微软平板下软件安卓系统,开启全... 你有没有想过,在微软平板上也能畅享安卓系统的乐趣呢?没错,这就是今天我要跟你分享的神奇故事。想象你手...
coloros是基于安卓系统吗... 你有没有想过,手机里的那个色彩斑斓的界面,背后其实有着一个有趣的故事呢?没错,我要说的就是Color...
安卓神盾系统应用市场,一站式智... 你有没有发现,手机里的安卓神盾系统应用市场最近可是火得一塌糊涂啊!这不,我就来给你好好扒一扒,看看这...
黑莓平板安卓系统升级,解锁无限... 亲爱的读者们,你是否还记得那个曾经风靡一时的黑莓手机?那个标志性的全键盘,那个独特的黑莓体验,如今它...
安卓文件系统采用华为,探索高效... 你知道吗?最近安卓系统在文件管理上可是有了大动作呢!华为这个科技巨头,竟然悄悄地给安卓文件系统来了个...
深度系统能用安卓app,探索智... 你知道吗?现在科技的发展真是让人惊叹不已!今天,我要给你揭秘一个超级酷炫的话题——深度系统能用安卓a...
安卓系统的分区类型,深度解析存... 你有没有发现,你的安卓手机里藏着不少秘密?没错,就是那些神秘的分区类型。今天,就让我带你一探究竟,揭...
安卓系统铠无法兑换,揭秘无法兑... 最近是不是有很多小伙伴在玩安卓系统的游戏,突然发现了一个让人头疼的问题——铠无法兑换!别急,今天就来...
汽车安卓系统崩溃怎么刷,一键刷... 亲爱的车主朋友们,你是否曾遇到过汽车安卓系统崩溃的尴尬时刻?手机系统崩溃还能重启,但汽车系统崩溃了,...
miui系统可以刷安卓p系统吗... 亲爱的手机控们,你是否对MIUI系统情有独钟,同时又对安卓P系统的新鲜功能垂涎欲滴?今天,就让我带你...