(25-看到allure动态生成标题和描述的特性时,直言简直太灵活了(allure.dynamic使用))
1 之前关于标题和描述是怎么做的?
1.1 之前标题使用@allure.title装饰器
- 举个例子看下,之前已经学习过了,不再赘述了:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/3/28
# 文件名称:test_allure_title.py
# 作用:@allure.title特性
import allure
@allure.title("用户正常登陆")
def test_login01():
pass
@allure.title("用户名错误")
def test_login02():
pass
- 查看报告:
1.2 之前描述使用@allure.description装饰器
- 举例:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/3/28
# 文件名称:test_allure_description.py
# 作用:@allure.description特性
import allure
import pytest
# 使用方法一
@allure.description("""
这个用例的主要所用是验证?
哦,对了,我也不知道干啥的!!!
""")
def test_case01():
num = 100 * (1 + 9)
assert num == 1000
# 使用方法二
def test_case02():
"""
这是一个用例!
这个用例什么也没做!
这个用例只是简单做个验证而已~
"""
pass
2 allure.dynamic的动态功能怎么做?
2.1 allure.dynamic源码
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)
2.2 从allure.dynamic源码可得出能动态设置的类型
allure.dynamic.title
allure.dynamic.description
allure.dynamic.description_html
allure.dynamic.label
allure.dynamic.severity
allure.dynamic.feature
allure.dynamic.story
allure.dynamic.tag
allure.dynamic.link
allure.dynamic.issue
allure.dynamic.testcase
2.3 类型说明和举例
2.3.1 allure.dynamic.title
- 动态标题:
@allure.title("使用装饰器标题")
def test_title():
print("CASE-01")
allure.dynamic.title("使用动态标题")
2.3.2 allure.dynamic.description
- 动态描述:
@allure.description("使用装饰器进行描述")
def test_description():
print("CASE-02")
allure.dynamic.description("使用动态描述")
2.3.3 allure.dynamic.description_html
- 动态描述传一段
html
代码:
html_d = """
Test @allure.description_html
<table style="width:100%">
<tr>
<th>姓名</th>
<th>成绩</th>
</tr>
<tr align="center">
<td>张三</td>
<td>100</td>
</table>
"""
def test_description_html():
print("CASE-03")
allure.dynamic.description_html(html_d)
2.3.4 allure.dynamic.label
- 动态用例的标签;
- 举例:
def test_label():
print("CASE-04")
allure.dynamic.label("this_label")
2.3.5 allure.dynamic.severity
- 动态用例的优先级;
- 举例:
@allure.severity("trivial")
def test_severity():
print("CASE-05")
allure.dynamic.severity("blocker")
2.3.6 allure.dynamic.feature
- 模块名称 功能点的描述,往下是 story;
- 举例:
@allure.feature('feature000')
def test_feature():
print("CASE-06")
allure.dynamic.feature("feature111")
2.3.7 allure.dynamic.story
- 用户故事,往下是title;
- 举例:
@allure.story('story222')
def test_story():
print("CASE-07")
allure.dynamic.story("story333")
2.3.8 allure.dynamic.tag
- 用例的标记;
- 举例:
def test_tag():
print("CASE-08")
allure.dynamic.tag("this is tag")
2.3.9 allure.dynamic.link
- 定义一个链接;
- 举例:
@allure.link("https://blog.csdn.net/NoamaNelson")
def test_link():
print("CASE-09")
allure.dynamic.link("https://juejin.cn/user/3655236621185246")
2.3.10 allure.dynamic.issue
- 缺陷,对应缺陷管理系统里面的链接;
- 举例:
def test_issue():
print("CASE-10")
allure.dynamic.issue('https://bbs.csdn.net/forums/NoamaNelson', '全栈测试技术社区')
2.3.11 allure.dynamic.testcase
- 测试用例的链接地址;
- 举例:
def test_testcase():
print("CASE-11")
allure.dynamic.issue("https://bbs.csdn.net/forums/NoamaNelson?category=10003&typeId=1566629", "测试用例地址")
3 本文涉及到的源码
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/3/29
# 文件名称:test_allure_dynamic.py
# 作用:allure.dynamic特性使用
import allure
@allure.title("使用装饰器标题")
def test_title():
print("CASE-01")
allure.dynamic.title("使用动态标题")
@allure.description("使用装饰器进行描述")
def test_description():
print("CASE-02")
allure.dynamic.description("使用动态描述")
html_d = """
Test @allure.description_html
<table style="width:100%">
<tr>
<th>姓名</th>
<th>成绩</th>
</tr>
<tr align="center">
<td>张三</td>
<td>100</td>
</table>
"""
def test_description_html():
print("CASE-03")
allure.dynamic.description_html(html_d)
def test_label():
print("CASE-04")
allure.dynamic.label("this_label")
@allure.severity("trivial")
def test_severity():
print("CASE-05")
allure.dynamic.severity("blocker")
@allure.feature('feature000')
def test_feature():
print("CASE-06")
allure.dynamic.feature("feature111")
@allure.story('story222')
def test_story():
print("CASE-07")
allure.dynamic.story("story333")
def test_tag():
print("CASE-08")
allure.dynamic.tag("this is tag")
@allure.link("https://blog.csdn.net/NoamaNelson")
def test_link():
print("CASE-09")
allure.dynamic.link("https://juejin.cn/user/3655236621185246")
def test_issue():
print("CASE-10")
allure.dynamic.issue('https://bbs.csdn.net/forums/NoamaNelson', '全栈测试技术社区')
def test_testcase():
print("CASE-11")
allure.dynamic.issue("https://bbs.csdn.net/forums/NoamaNelson?category=10003&typeId=1566629", "测试用例地址")