一、Allure插件常用方法
Allure是一个强大的测试报告框架,提供了一系列方法来生成漂亮、交互式的测试报告。下面是Allure插件的一些常用方法的详细介绍:
attach:将附件添加到报告中。
- attach(content, name=None, attachment_type=None):将内容(content)作为附件添加到报告中。可以指定附件的名称(name)和附件类型(attachment_type)。
示例:
import allure allure.attach('附件内容', name='附件名称', attachment_type=allure.attachment_type.TEXT)
step:创建一个测试步骤。
- step(name=None, title=None, parameters=None):创建一个测试步骤,并可指定步骤的名称(name),标题(title)和参数(parameters)。
示例:
import allure with allure.step('测试步骤1'): # 执行测试步骤1的操作 with allure.step('测试步骤2'): # 执行测试步骤2的操作
feature:指定测试用例所属的功能特性。
- feature(name):指定测试用例所属的功能特性。
示例:
import allure @allure.feature('登录功能') def test_login(): # 执行登录测试
story:指定测试用例所属的用户故事。
- story(name):指定测试用例所属的用户故事。
示例:
import allure @allure.story('用户故事1') def test_functionality(): # 执行用户故事1的测试
issue:指定测试用例相关的问题或缺陷。
- issue(issue_id):指定测试用例相关的问题或缺陷。
示例:
import allure @allure.issue('BUG-123') def test_bug(): # 执行与BUG-123相关的测试
severity:指定测试用例的严重程度。
- severity(severity_level):指定测试用例的严重程度,包括BLOCKER、CRITICAL、NORMAL、MINOR和TRIVIAL。
示例:
import allure @allure.severity(allure.severity_level.CRITICAL) def test_critical(): # 执行关键性测试
link:添加一个链接到测试报告中。
- link(url, name=None, link_type=None):添加一个链接(url)到测试报告中,可以指定链接的名称(name)和链接类型(link_type)。
示例:
import allure allure.link('https://www.example.com', name='示例链接')
这些只是Allure插件的一小部分方法,它们可以帮助您创建更具可读性和可视化效果的测试报告。您可以根据自己的需求选择使用适合的方法来增强您的Allure报告。
二、pytest常用参数和选项
pytest是一个功能强大的Python测试框架,它提供了多种参数和选项来执行测试用例。下面是pytest执行用例的常用参数的详细介绍:
文件/目录:指定要执行的测试文件或目录。
示例:
pytest test_file.py # 执行单个测试文件 pytest test_directory/ # 执行一个目录下的所有测试文件
指定用例:指定要执行的特定测试用例。
示例:
pytest test_file.py::test_func # 执行特定测试文件中的特定测试函数 pytest test_file.py -k "keyword" # 执行包含关键字的测试用例
标记:执行带有特定标记的测试用例。
示例:
pytest -m marker_name # 执行带有特定标记的测试用例
并行执行:使用多个进程或线程并行执行测试用例。
示例:
pytest -n num_processes # 使用指定数量的进程进行并行执行 pytest -d num_threads # 使用指定数量的线程进行并行执行
生成报告:生成测试报告。
示例:
pytest --alluredir=dir_path # 生成Allure XML报告到指定目录 pytest --junitxml=file_path # 生成JUnit XML报告到指定文件 pytest --html=file_path # 生成HTML报告到指定文件
覆盖率报告:生成代码覆盖率报告。
示例:
pytest --cov=package_name # 生成代码覆盖率报告 pytest --cov-report=report_type # 指定代码覆盖率报告的类型(如term、html、xml)
参数化测试:使用参数化数据执行测试用例。
示例:
import pytest @pytest.mark.parametrize("param1,param2", [(1, 2), (3, 4)]) def test_function(param1, param2): # 使用不同的参数执行测试用例
重试:对失败的测试用例进行重试执行。
示例:
pytest --reruns=num_retries # 对失败的测试用例进行指定次数的重试 pytest --reruns-delay=seconds # 重试之间的延迟时间
这些只是pytest执行用例时的一些常用参数,您可以根据自己的需求选择合适的参数来执行和管理测试用例。同时,pytest还提供了更多的选项和插件,以满足更多复杂的测试需求。
三、pytest结合Allure常用参数
pytest结合Allure执行特定测试用例的参数有很多,下面是一些常用的参数以及使用方法的举例说明:
--allure-severities:根据指定的严重性级别筛选测试用例,并在Allure报告中显示。
示例:
pytest --allure-severities="blocker, critical" # 只运行严重性级别为 blocker 和 critical 的用例,并生成相应的Allure报告
--allure-stories:根据指定的故事筛选测试用例,并在Allure报告中显示。
示例:
pytest --allure-stories="story1, story2" # 只运行故事为 story1 和 story2 的用例,并生成相应的Allure报告
--alluredir:指定Allure报告的输出目录。
示例:
pytest --alluredir=reports/allure-report # 将生成的Allure报告保存在 reports/allure-report 目录下
--allure-features:根据指定的特性筛选测试用例,并在Allure报告中显示。
示例:
pytest --allure-features="feature1, feature2" # 只运行特性为 feature1 和 feature2 的用例,并生成相应的Allure报告
--allure-labels:根据指定的标签筛选测试用例,并在Allure报告中显示。
示例:
pytest --allure-labels="label1, label2" # 只运行带有标签 label1 和 label2 的用例,并生成相应的Allure报告
--allure-link-pattern:定义Allure报告中显示的链接模式。
示例:
pytest --allure-link-pattern=issue:https://example.com/issue/{} # 在Allure报告中,将issue链接的模式设置为 https://example.com/issue/{issue number}
这些参数可以结合使用,根据特定的需求和测试场景进行灵活配置,以生成符合要求的Allure报告,并执行特定的测试用例。同时,还可以结合其他pytest参数和插件来进一步扩展和定制测试框架的功能。
四、示例
import allure
import pytest
def test_success():
"""this test succeeds"""
assert True
def test_failure():
"""this test fails"""
assert False
def test_skip():
"""this test is skipped"""
pytest.skip('for a reason!')
@allure.step('allure.step = step test broken')
def test_broken():
raise Exception('oops')
@allure.title('allure.title = step test skip')
def test_title():
"""this test is skipped"""
assert True
@allure.link(url = 'https://www.baidu.com', name='百度一下')
def test_link():
assert True
@allure.issue(url = 'https://blog.csdn.net/', name='CSDN') # 会有一个可供点击的图标
def test_issue():
assert True
@allure.testcase(url = 'https://360.net/', name='360')
def test_testcase():
assert True
# 敏捷开发分层标识Epic, Feature, Stories
# 执行命令:pytest test_status1.py --allure-stories story1,story2
@allure.story('story1')
def test_story1():
assert True
@allure.story('story2')
def test_story2():
assert True
@allure.feature('feature1')
def test_feature():
assert True
@allure.epic('epic1')
def test_epic():
assert True
# 添加描述
@allure.description('description')
def test_description():
assert True
@allure.description_html('description_html')
def test_description_html():
assert True
# 优先级,执行命令:pytest test_status1.py --allure-severities normal,blocker
'''
BLOCKER = 'blocker'
CRITICAL = 'critical'
NORMAL = 'normal'
MINOR = 'minor'
TRIVIAL = 'trivial'
'''
@allure.severity(allure.severity_level.TRIVIAL)
def test_TRIVIAL():
assert True
@allure.severity(allure.severity_level.MINOR)
def test_MINOR():
assert True
@allure.severity(allure.severity_level.NORMAL)
def test_NORMAL():
assert True
@allure.severity(allure.severity_level.CRITICAL)
def test_CRITICAL():
assert True
@allure.severity(allure.severity_level.BLOCKER)
def test_BLOCKER():
assert True
def test_BLOCKER():
# Allure测试报告添加附件
allure.attach('文件内容:A text attacment in module scope fixture', '文件名称:blah blah blah,下一个字段,附件类型',name='test_status1.py',attachment_type=allure.attachment_type.TEXT)
assert True