pytest学习和使用-allure特性总览中的Environment、Categories设置以及Flaky test使用

  • 如下图,我们可以看到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

  1. Browser=Chrome

  2. Browser.Version=111.0.5563.65

  3. Env=Test

  4. IP=192.168.1.133

  5. Allure-Pytest.Version=2.8.12

  • 运行后查看Environment

    图片

  • 或者创建environment.xml

  1. environment>

  2. <parameter>

  3. <key>Browser</key>

  4. <value>Chrome</value>

  5. </parameter>

  6. <parameter>

  7. <key>Browser.Version</key>

  8. <value>111.0.5563.65</value>

  9. </parameter>

  10. <parameter>

  11. <key>Env</key>

  12. <value>Test</value>

  13. </parameter>

  14. <parameter>

  15. <key>IP</key>

  16. <value>192.168.1.133</value>

  17. </parameter>

  18. <parameter>

  19. <key>Allure-Pytest.Version</key>

  20. <value>2.8.12</value>

  21. </parameter>

  22. </environment>

2 Categories设置

  • Categories即分类,测试用例结果的分类;

  • 默认有两种分类:

  1. # Product defects 产品缺陷(测试结果:failed)

  2. # Test defects 测试缺陷(测试结果:error/broken)

  • 可以自定义分类。

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

2.2 创建文件

图片

在这里插入图片描述

  1. [

  2. {

  3. "name": "Ignored tests",

  4. "matchedStatuses": ["skipped"]

  5. },

  6. {

  7. "name": "Infrastructure problems",

  8. "matchedStatuses": ["broken", "failed"],

  9. "messageRegex": ".*bye-bye.*"

  10. },

  11. {

  12. "name": "Outdated tests",

  13. "matchedStatuses": ["broken"],

  14. "traceRegex": ".*FileNotFoundException.*"

  15. },

  16. {

  17. "name": "Product defects",

  18. "matchedStatuses": ["failed"]

  19. },

  20. {

  21. "name": "Test defects",

  22. "matchedStatuses": ["broken"]

  23. }

  24. ]

  • 参数说明:

  1. name:分类名称

  2. matchedStatuses:测试用例的运行状态,默认["failed", "broken", "passed", "skipped", "unknown"]

  3. messageRegex:测试用例运行的错误信息,默认.* ,通过正则匹配

  4. traceRegex:测试用例运行的错误堆栈信息,默认.* ,通过正则匹配

  • 写一个用例,验证下:

  1. # -*- coding:utf-8 -*-

  2. # 作者:虫无涯

  3. # 日期:2023/3/20

  4. # 文件名称:test_yyy.py

  5. # 作用:allure特性categories验证

  6. # 联系:VX(NoamaNelson)

  7. # 博客:https://blog.csdn.net/NoamaNelson

  8. import pytest

  9. import time

  10. class TestCase01():

  11. def test_case_01(self):

  12. time.sleep(1)

  13. print("case01$$$$$$$$$$$$$$$$$$$$$")

  14. assert1 == 2

  15. def test_case_02(self):

  16. time.sleep(1)

  17. print("case02$$$$$$$$$$$$$$$$$$$$$")

  18. assert3 == 3

  19. def test_case_03(self):

  20. time.sleep(1)

  21. print("case03$$$$$$$$$$$$$$$$$$$$$")

  22. assert"is"in"is_you"

  23. def test_case_04(self):

  24. time.sleep(1)

  25. print("case04$$$$$$$$$$$$$$$$$$$$$")

  26. assert5 < 10

  27. def test_case_05(self):

  28. time.sleep(1)

  29. print("case05$$$$$$$$$$$$$$$$$$$$$")

  30. assert222 == 333

  31. def test_case_06(self):

  32. time.sleep(1)

  33. print("case06$$$$$$$$$$$$$$$$$$$$$")

  34. assert444 > 666

  35. class TestCase02():

  36. def test_case_07(self):

  37. time.sleep(1)

  38. print("case07$$$$$$$$$$$$$$$$$$$$$")

  39. assert10/2 == 5.0

  40. def test_case_08(self):

  41. time.sleep(1)

  42. print("case08$$$$$$$$$$$$$$$$$$$$$")

  43. assert"num"in"num_list"

  44. def test_case_09(self):

  45. time.sleep(1)

  46. print("case08$$$$$$$$$$$$$$$$$$$$$")

  47. assert"num1"in"num_list"

  48. if __name__ == '__main__':

  49. pytest.main(["-s", "test_yyy.py"])

  • 运行命令:

  1. 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
  1. C:\Users\Administrator>pip3 install pytest-ignore-flaky

  2. Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple

  3. Collecting pytest-ignore-flaky

  4. Downloading https://pypi.tuna.tsinghua.edu.cn/packages/22/bf/4a670d28c8c37569e26536c068d83b37a01aea9fff9a45a03ae3be5344b9/pytest_ignore_flaky-2.0.0-py3-none-any.whl (3.9 kB)

  5. Requirement already satisfied: pytest>=6.0in d:\python37\lib\site-packages (from pytest-ignore-flaky) (6.2.4)

  6. Requirement already satisfied: py>=1.8.2in d:\python37\lib\site-packages (from pytest>=6.0->pytest-ignore-flaky) (1.10.0)

  7. Requirement already satisfied: colorama in d:\python37\lib\site-packages (from pytest>=6.0->pytest-ignore-flaky) (0.4.4)

  8. Requirement already satisfied: attrs>=19.2.0in d:\python37\lib\site-packages (from pytest>=6.0->pytest-ignore-flaky) (20.3.0)

  9. Requirement already satisfied: atomicwrites>=1.0in d:\python37\lib\site-packages (from pytest>=6.0->pytest-ignore-flaky) (1.4.0)

  10. Requirement already satisfied: iniconfig in d:\python37\lib\site-packages (from pytest>=6.0->pytest-ignore-flaky) (1.1.1)

  11. Requirement already satisfied: importlib-metadata>=0.12in d:\python37\lib\site-packages (from pytest>=6.0->pytest-ignore-flaky) (2.1.1)

  12. Requirement already satisfied: toml in d:\python37\lib\site-packages (from pytest>=6.0->pytest-ignore-flaky) (0.10.2)

  13. Requirement already satisfied: packaging in d:\python37\lib\site-packages (from pytest>=6.0->pytest-ignore-flaky) (20.8)

  14. Requirement already satisfied: pluggy<1.0.0a1,>=0.12in d:\python37\lib\site-packages (from pytest>=6.0->pytest-ignore-flaky) (0.13.1)

  15. Requirement already satisfied: zipp>=0.5in d:\python37\lib\site-packages (from importlib-metadata>=0.12->pytest>=6.0->pytest-ignore-flaky) (1.2.0)

  16. Requirement already satisfied: pyparsing>=2.0.2in d:\python37\lib\site-packages (from packaging->pytest>=6.0->pytest-ignore-flaky) (2.4.7)

  17. Installing collected packages: pytest-ignore-flaky

  18. Successfully installed pytest-ignore-flaky-2.0.0

3.4 Flaky使用
  • 再写一个用例:

  1. # -*- coding:utf-8 -*-

  2. # 作者:虫无涯

  3. # 日期:2023/3/20

  4. # 文件名称:test_yyy.py

  5. # 作用:allure特性categories验证

  6. # 联系:VX(NoamaNelson)

  7. # 博客:https://blog.csdn.net/NoamaNelson

  8. import pytest

  9. import time

  10. class TestCase01():

  11. def test_case_01(self):

  12. time.sleep(1)

  13. print("case01$$$$$$$$$$$$$$$$$$$$$")

  14. assert1 == 2

  15. def test_case_02(self):

  16. time.sleep(1)

  17. print("case02$$$$$$$$$$$$$$$$$$$$$")

  18. assert3 == 3

  19. def test_case_03(self):

  20. time.sleep(1)

  21. print("case03$$$$$$$$$$$$$$$$$$$$$")

  22. assert"is"in"is_you"

  23. def test_case_04(self):

  24. time.sleep(1)

  25. print("case04$$$$$$$$$$$$$$$$$$$$$")

  26. assert5 < 10

  27. def test_case_05(self):

  28. time.sleep(1)

  29. print("case05$$$$$$$$$$$$$$$$$$$$$")

  30. assert222 == 333

  31. def test_case_06(self):

  32. time.sleep(1)

  33. print("case06$$$$$$$$$$$$$$$$$$$$$")

  34. assert444 > 666

  35. class TestCase02():

  36. def test_case_07(self):

  37. time.sleep(1)

  38. print("case07$$$$$$$$$$$$$$$$$$$$$")

  39. assert10/2 == 5.0

  40. def test_case_08(self):

  41. time.sleep(1)

  42. print("case08$$$$$$$$$$$$$$$$$$$$$")

  43. assert"num"in"num_list"

  44. @pytest.mark.flaky

  45. def test_case_09(self):

  46. time.sleep(1)

  47. print("case08$$$$$$$$$$$$$$$$$$$$$")

  48. assert"num1"in"num_list"

  49. if __name__ == '__main__':

  50. pytest.main(["-s", "test_yyy.py"])

  • 使用命令直接运行用例:

pytest -n auto --alluredir=allure-results test_yyy.py
  1. ========================================== short test summary info ===========================================

  2. FAILED test_yyy.py::TestCase01::test_case_06 - assert444 > 666

  3. FAILED test_yyy.py::TestCase01::test_case_01 - assert1 == 2

  4. FAILED test_yyy.py::TestCase01::test_case_05 - assert222 == 333

  5. FAILED test_yyy.py::TestCase02::test_case_09 - AssertionError: assert'num1'in'num_list'

  6. ==================================== 4 failed, 5 passed, 1 rerun in5.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)

行动吧,在路上总比一直观望的要好,未来的你肯定会感 谢现在拼搏的自己!如果想学习提升找不到资料,没人答疑解惑时,请及时加入扣群: 320231853,里面有各种软件测试+开发资料和技术可以一起交流学习哦。

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!

  • 14
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值