allure
报告的总览,里边的一些特性是可以自定义设置的。Environment
可以理解为环境变量;environment.properties
或者environment.xml
文件;--alluredir
指定的目录,比如:--alluredir allure-results
pytest -n auto --alluredir=allure-results test_xdist.py
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>Browser Chrome Browser.Version 111.0.5563.65 Env Test IP 192.168.1.133 Allure-Pytest.Version 2.8.12
Categories
即分类,测试用例结果的分类;# Product defects 产品缺陷(测试结果:failed)
# Test defects 测试缺陷(测试结果:error/broken)
environment
方式一样,在allure-results
目录中创建categories.json
文件[{"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
Flaky test
在被测对象和测试条件都不变的情况下,有时候失败、有时候成功的测试;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
# -*- 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
而不是失败了:@pytest.mark.flaky
装饰器标记的测试用例默认会执行;pytest
命令行参数 --ignore-flaky
运行 @pytest.mark.flaky
标记的测试用例:当用例执行成功时执行结果显示正常;XFAIL(skip flaky test failure)
。下一篇:Java面试总结(九)