前言
- 前面提到,可以使用@allure.title 和 @allure.description 给测试用例添加标题和描述
- 在用例里面也可以动态更新标题和详情
- 使用allure.dynamic.description 和 allure.dynamic.title 实现
源码
class Dynamic(object):
@staticmethod
def title(test_title):
plugin_manager.hook.add_title(test_title=test_title)
@staticmethod
def description(test_description):
plugin_manager.hook.add_description(test_description=test_description)
@staticmethod
def description_html(test_description_html):
plugin_manager.hook.add_description_html(test_description_html=test_description_html)
@staticmethod
def label(label_type, *labels):
plugin_manager.hook.add_label(label_type=label_type, labels=labels)
@staticmethod
def severity(severity_level):
Dynamic.label(LabelType.SEVERITY, severity_level)
@staticmethod
def feature(*features):
Dynamic.label(LabelType.FEATURE, *features)
@staticmethod
def story(*stories):
Dynamic.label(LabelType.STORY, *stories)
@staticmethod
def tag(*tags):
Dynamic.label(LabelType.TAG, *tags)
@staticmethod
def link(url, link_type=LinkType.LINK, name=None):
plugin_manager.hook.add_link(url=url, link_type=link_type, name=name)
@staticmethod
def issue(url, name=None):
Dynamic.link(url, link_type=LinkType.ISSUE, name=name)
@staticmethod
def testcase(url, name=None):
Dynamic.link(url, link_type=LinkType.TEST_CASE, name=name)
知识点
查看源码可以看出,allure很多特性都支持dynamic用法:
allure.dynamic.title
allure.dynamic.description
allure.dynamic.description_html
allure.dynamic.severity
allure.dynamic.feature
allure.dynamic.story
allure.dynamic.link
allure.dynamic.issue
allure.dynamic.testcase
举个🌰(title)
代码
"""
__File__ = test_dynamic2.py.py
__Project__= _JAuto-Interface
__Time__ = 2022-02-26 18:24:36
__Author__ = 黎晟
"""
import allure
@allure.title("标题:用例执行成功后会被替换")
def test_with_dynamic_title_success():
assert 2 + 2 == 4
allure.dynamic.title('用例执行成功后,更新后的title')
@allure.title("标题:用例执行失败后不会被替换")
def test_with_dynamic_title_failed():
assert 2 + 2 == 3
allure.dynamic.title('用例执行失败后,无效的的title')
测试报告
执行成功的用例,标题被更新为用例内的标题
举个🌰(description)
代码
"""
__File__ = test_dynamic2.py.py
__Project__= _JAuto-Interface
__Time__ = 2022-02-26 18:24:36
__Author__ = 黎晟
"""
import allure
def test_with_dynamic_description_success():
"""
描述:用例执行成功后会被替换
"""
assert 2 + 2 == 4
allure.dynamic.description('用例执行成功后,更新后的描述')
def test_with_dynamic_description_failed():
"""
描述:用例执行失败后不会被替换
"""
assert 2 + 2 == 3
allure.dynamic.description('用例执行失败后,无效的的描述')
测试报告
用例执行失败,未被替换的描述
用例执行成功,被替换了的描述
结合参数化parametrize
代码
"""
__File__ = test_dynamic2.py.py
__Project__= _JAuto-Interface
__Time__ = 2022-02-26 18:24:36
__Author__ = 黎晟
"""
import allure
import pytest
data_dynamic = [
("name1", "password1", "登录成功用例"),
("name2", "password2", "登录失败用例"),
("name3", "password3", "账号不存在用例")
]
@pytest.mark.parametrize("name,pwd,title", data_dynamic)
def test_with_dynamic(name, pwd, title):
"""
登录用例
"""
allure.dynamic.title(title)
allure.dynamic.description(f'nam为{name},pwd为{pwd}')
测试报告
结合其他特性
代码
"""
__File__ = test_dynamic2.py.py
__Project__= _JAuto-Interface
__Time__ = 2022-02-26 18:24:36
__Author__ = 黎晟
"""
import allure
def test_other_with_dynamic():
assert 1 == 1
allure.dynamic.feature('动态feature')
allure.dynamic.story('动态story')
allure.dynamic.severity(allure.severity_level.BLOCKER)
allure.dynamic.link("dynamic动态生成的Link:https://blog.csdn.net/qq_42841075?type=blog")
allure.dynamic.issue("dynamic动态生成的Issue:https://blog.csdn.net/qq_42841075?type=blog")
allure.dynamic.testcase("dynamic动态生成的testcase:https://blog.csdn.net/qq_42841075?type=blog")
测试报告
|