首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >pytest学习和使用23-通俗易懂的聊聊allure常用特性集合及使用方法说明

pytest学习和使用23-通俗易懂的聊聊allure常用特性集合及使用方法说明

原创
作者头像
虫无涯
发布2023-03-30 09:08:06
2.4K0
发布2023-03-30 09:08:06
举报
文章被收录于专栏:全栈测试技术全栈测试技术

1 @allure.step()

  • @allure.step()装饰器,可以让测试用例在allure报告中显示详细的测试过程;
  • step() 只有一个参数title,传什么就在allure上就显示什么;
  • 举例:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/3/28 
# 文件名称:test_allure_step.py
# 作用:@allure.step特性
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import pytest
import allure

@allure.step("步骤1:用户登陆")
def test_login():
    pass

@allure.step("步骤2:用户数据检查")
def test_check():
    test_login()


if __name__ == '__main__':
    pytest.main(["-s", "test_allure_step.py"])
  • 运行命令再回顾下:
pytest -n auto --alluredir=allure test_allure_step.py
allure serve allure
  • 运行后如下:
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
  • 还有一种场景就是用例之间的嵌套, step() 支持支持位置参数和关键字参数;
  • 如下:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/3/28 
# 文件名称:test_allure_step1.py
# 作用:@allure.step特性
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import pytest
import allure

@allure.step("步骤1:用户登陆{0}, {passwd}")
def login(name, passwd):
    pass

@allure.step("步骤2:数据检查")
def check():
    login("zhang", "123456")

@allure.step("步骤3:登陆")
def test_case():
    check()

if __name__ == '__main__':
    pytest.main(["-s", "test_allure_step1.py"])
在这里插入图片描述
在这里插入图片描述

2 allure.attach

  • allure报告支持显示许多不同类型的附件;
  • 使用方法是:
allure.attach(body, name, attachment_type, extension) 
  • 参数说明:
body:附件内容
name:附件名称
attachment_type:附件类型,是 allure.attachment_type 里面的其中一种
extension:附件扩展名
  • allure.attachment_type的类型如下,可以查看源码:
class AttachmentType(Enum):

    def __init__(self, mime_type, extension):
        self.mime_type = mime_type
        self.extension = extension

    TEXT = ("text/plain", "txt")
    CSV = ("text/csv", "csv")
    TSV = ("text/tab-separated-values", "tsv")
    URI_LIST = ("text/uri-list", "uri")

    HTML = ("text/html", "html")
    XML = ("application/xml", "xml")
    JSON = ("application/json", "json")
    YAML = ("application/yaml", "yaml")
    PCAP = ("application/vnd.tcpdump.pcap", "pcap")

    PNG = ("image/png", "png")
    JPG = ("image/jpg", "jpg")
    SVG = ("image/svg-xml", "svg")
    GIF = ("image/gif", "gif")
    BMP = ("image/bmp", "bmp")
    TIFF = ("image/tiff", "tiff")

    MP4 = ("video/mp4", "mp4")
    OGG = ("video/ogg", "ogg")
    WEBM = ("video/webm", "webm")

    PDF = ("application/pdf", "pdf")
  • 举例:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/3/28 
# 文件名称:test_allure_attach.py
# 作用:allure.attach特性
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import allure
import pytest

def test_login():
    allure.attach("user01,user02,user03", "用户信息", allure.attachment_type.TEXT)
    pass
在这里插入图片描述
在这里插入图片描述
  • allure.attach()的另一种使用方法为:
allure.attach.file(source, name, attachment_type, extension)
  • 参数说明:source:文件路径 body:附件内容 name:附件名称 attachment_type:附件类型,是 allure.attachment_type 里面的其中一种 extension:附件扩展名
  • 举例:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/3/28 
# 文件名称:test_allure_attach.py
# 作用:allure.attach特性
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import allure
import pytest

def test_login():
    allure.attach("user01,user02,user03", "用户信息", allure.attachment_type.TEXT)
    pass

def test_login_info():
    allure.attach.file("./user_info.csv", attachment_type=allure.attachment_type.CSV)
    pass
在这里插入图片描述
在这里插入图片描述

3 @allure.description()

  • 添加更详细的测试用例描述;
  • 格式为:@allure.description(str),类似于函数声明下方添加 """ """
  • 举例:
# -*- 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
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4 @allure.title()

  • 描述测试用例的标题,可为中文;
  • 格式为:@allure.title(str)
  • 如下:
# -*- 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
在这里插入图片描述
在这里插入图片描述
  • 另外,allure.title()支持占位符传递关键字参数;
  • 如下:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/3/28 
# 文件名称:test_allure_title1.py
# 作用:@allure.title特性
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import allure
import pytest

@allure.title("用户登陆")
@pytest.fixture
def test_login(request):
    args = request.param
    user = args["name"]
    pwd = args["password"]
    print(user, pwd, args)
    yield user, pwd

@allure.title("正常登陆,用户信息为:{test_login}")
@pytest.mark.parametrize("test_login", [
    {"name": "xiaoli", "password": "123456"},
    {"name": "laoli", "password": "123456"}], indirect=True)
def test_login_info(test_login):
    user, pwd = test_login
    allure.attach(f"用户名:{user}, 密码:{pwd}")
在这里插入图片描述
在这里插入图片描述

5 @allure.link()、@allure.issue()、allure.testcase()

  • 为了将allure报告和一些系统集成,可以更快速的跳转到指定地址;
  • 源码为:
def link(url, link_type=LinkType.LINK, name=None):
    return safely(plugin_manager.hook.decorate_as_link(url=url, link_type=link_type, name=name))
  • 参数说明:
issue()和testcase()其实调用的也是link(),只是link_type不一样
url:跳转的链接;
name:可选参数,显示在allure报告的名字,不传则显示完整的链接;
link_type:跳转的type类型;LINK、ISSUE、TEST_CASE,即访问链接、Bug链接、测试用例链接;
总结:三个方法是一样的,只是link_type不一样,allure报告显示的样式不一样。
  • 举例:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/3/28 
# 文件名称:test_link_issue_testcase.py
# 作用:@allure.link()、@allure.issue()、@allure.testcase()的特性
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import allure

@allure.link("https://blog.csdn.net/NoamaNelson")
def test_blog_link():
    pass

@allure.link("https://blog.csdn.net/NoamaNelson", name="点击查看博客主页")
def test_blog_link1():
    pass

@allure.issue('https://bbs.csdn.net/forums/NoamaNelson', '全栈测试技术社区')
def test_blog_issue():
    pass

@allure.testcase("https://bbs.csdn.net/forums/NoamaNelson?category=10003&typeId=1566629", "测试用例地址")
def test_blog_testcase():
    pass
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

6 @allure.epic()、@allure.feature()、@allure.story()

  • allure的三种标记装饰器,可以显示在allure报告上;
  • @pytest.mark 不会显示在allure报告上;
  • 作用:
@allure.epic:往下是 feature
@allure.feature:功能点的描述,模块往下是 story
@allure.story:故事,往下是 title
注意:
story 是 feature 的子集。
  • 举例:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/3/28 
# 文件名称:test_epic_feature_story.py
# 作用:@allure.epic()、@allure.feature()、@allure.story()特性
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import allure

def test_case01():
    pass

@allure.story('epic01')
def test_case02():
    pass

@allure.story('story01')
def test_case03():
    pass

@allure.story('story02')
def test_case04():
    pass

@allure.feature('feature02')
@allure.story('story02')
def test_case05():
    pass
在这里插入图片描述
在这里插入图片描述

7 @allure.severity

  • allure 报告可以看到不同级别用例的缺陷数量,即标记用例级别 ;
  • 使用方法:
@allure.severity(allure.severity_level.xxx)
# xxx为级别
  • 比如@allure.severity(allure.severity_level.TRIVIAL),可以看下源码:
class Severity(str, Enum):
    BLOCKER = 'blocker'
    CRITICAL = 'critical'
    NORMAL = 'normal'
    MINOR = 'minor'
    TRIVIAL = 'trivial'
  • 从以上可以看出有5个等级,分别是:

blocker:阻塞缺陷 critical:严重缺陷

normal: 一般缺陷

minor:次要缺陷

trivial: 轻微缺陷

  • 举例:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/3/28 
# 文件名称:test_severity.py
# 作用:@allure.severity标记用例级别
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import allure

# @allure.severity(allure.severity_level.NORMAL)
@allure.severity("normal")
@allure.description("""normal级别用例""")
def test_case01():
    print("用例01")

# @allure.severity(allure.severity_level.CRITICAL)
@allure.severity("critical")
@allure.description("""critical级别用例""")
def test_case02():
    print("用例02")

# @allure.severity(allure.severity_level.BLOCKER)
@allure.severity("blocker")
@allure.description("""blocker级别用例""")
def test_case03():
    print("用例03")

# @allure.severity(allure.severity_level.MINOR)
@allure.severity("minor")
@allure.description("""minor级别用例""")
def test_case04():
    print("用例04")

# @allure.severity(allure.severity_level.TRIVIAL)
@allure.severity("trivial")
@allure.description("""trivial级别用例""")
def test_case05():
    print("用例05")

@allure.description("""没标记,默认为 normal""")
def test_case06():
    print("用例06")
在这里插入图片描述
在这里插入图片描述
  • 没标记的默认normal
    在这里插入图片描述
    在这里插入图片描述
  • 图表样式:
    在这里插入图片描述
    在这里插入图片描述
  • 也可以使用命令行参数 allure-severities根据优先级选择需要运行的测试用例,比如:
pytest test_severity.py -sq --alluredir=./allure --allure-severities=blocker,critical
用例02
.用例03
.
2 passed in 0.10s

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 @allure.step()
  • 2 allure.attach
  • 3 @allure.description()
  • 4 @allure.title()
  • 5 @allure.link()、@allure.issue()、allure.testcase()
  • 6 @allure.epic()、@allure.feature()、@allure.story()
  • 7 @allure.severity
相关产品与服务
腾讯云服务器利旧
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com