pytest学习和使用25-当看到allure的动态生成标题和描述的特性时,直言简直太灵活了(allure.dynamic使用)

简介: pytest学习和使用25-当看到allure的动态生成标题和描述的特性时,直言简直太灵活了(allure.dynamic使用)

1 之前关于标题和描述是怎么做的?

1.1 之前标题使用@allure.title装饰器

  • 举个例子看下,之前已经学习过了,不再赘述了:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/3/28 
# 文件名称:test_allure_title.py
# 作用:@allure.title特性
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

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特性
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

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 = """
<h1>Test @allure.description_html</h1>
<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特性使用
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

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 = """
<h1>Test @allure.description_html</h1>
<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", "测试用例地址")
目录
相关文章
|
测试技术
pytest学习和使用23-通俗易懂的聊聊allure常用特性集合及使用方法说明
pytest学习和使用23-通俗易懂的聊聊allure常用特性集合及使用方法说明
100 0
|
测试技术
pytest学习和使用22-allure特性 丨总览中的Environment、Categories设置以及Flaky test使用
pytest学习和使用22-allure特性 丨总览中的Environment、Categories设置以及Flaky test使用
113 0
|
前端开发 C++
VS Code配置snippets代码片段快速生成html模板,提高前端编写效率
VS Code配置snippets代码片段快速生成html模板,提高前端编写效率
171 0
|
测试技术 Python
python接口自动化(二十三)--unittest断言——上(详解)
在测试用例中,执行完测试用例后,最后一步是判断测试结果是 pass 还是 fail,自动化测试脚本里面一般把这种生成测试结果的方法称为断言(assert)。用 unittest 组件测试用例的时候,断言的方法还是很多的,下面介绍几种常用的断 言方法:assertEqual、assertIn、assertTrue。想了解更多可以点击 传送门 看一下最后的小结有大致介绍。
228 0
python接口自动化(二十三)--unittest断言——上(详解)
|
网络协议 jenkins 测试技术
python接口自动化(二十五)--unittest断言——下(详解)
本篇还是回归到我们最初始的话题,想必大家都忘记了,没关系看这里:传送门 没错最初的话题就是登录,由于博客园的登录机制改变了,本篇以我找到的开源免费的登录API为案例,结合 unittest 框架写 2 个用例。同样我们先来看一下接口文档。
136 0
python接口自动化(二十五)--unittest断言——下(详解)
|
测试技术
pytest学习和使用11-Pytest如何使用自定义标记mark?
pytest学习和使用11-Pytest如何使用自定义标记mark?
69 0
pytest学习和使用11-Pytest如何使用自定义标记mark?
|
测试技术 数据库 Python
python接口自动化(二十二)--unittest执行顺序隐藏的坑(详解)
大多数的初学者在使用 unittest 框架时候,不清楚用例的执行顺序到底是怎样的。对测试类里面的类和方法分不清楚,不知道什么时候执行,什么时候不执行。虽然或许通过代码实现了,也是稀里糊涂的一知半解,这样还好,好歹自己鼓 捣出了,但是时间和效率并不是很高,下次遇到还是老样子。那么本篇通过最简单案例来给给为小伙伴详细讲解、演示一下 unittest 执行顺序。
228 0
python接口自动化(二十二)--unittest执行顺序隐藏的坑(详解)
|
Web App开发 JSON 测试技术
python接口自动化(二十四)--unittest断言——中(详解)
上一篇通过简单的案例给小伙伴们介绍了一下unittest断言,这篇我们将通过结合和围绕实际的工作来进行unittest的断言。这里以获取城市天气预报的接口为例,设计了 2 个用例,一个是查询北京的天气,一个是查询 南京为例,你也可以查询别的城市的天气预报。
132 0
python接口自动化(二十四)--unittest断言——中(详解)
|
测试技术 Python
python接口自动化(二十六)--批量执行用例 discover(详解)
我们在写用例的时候,单个脚本的用例好执行,那么多个脚本的时候,如何批量执行呢?这时候就需要用到 unittest 里面的 discover 方法来加载用例了。加载用例后,用 unittest 里面的 TextTestRunner 这里类的 run 方法去一次执行多个脚 本的用例。那么前边介绍那么多都是半道开始,半道出家,这篇就带大家从头到尾,一步一步给小伙伴们详细介绍一下。
231 0
python接口自动化(二十六)--批量执行用例 discover(详解)
【pytest官方文档】解读- 如何自定义mark标记,并将测试用例的数据传递给fixture函数
【pytest官方文档】解读- 如何自定义mark标记,并将测试用例的数据传递给fixture函数
【pytest官方文档】解读- 如何自定义mark标记,并将测试用例的数据传递给fixture函数