前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Pytest系列(23)- allure打标记,@allure.feature()、@allure.story()、@allure.severity()的详细使用

Pytest系列(23)- allure打标记,@allure.feature()、@allure.story()、@allure.severity()的详细使用

作者头像
小菠萝测试笔记
发布2020-06-09 16:03:00
2.1K0
发布2020-06-09 16:03:00
举报

如果你还想从头学起Pytest,可以看看这个系列的文章哦!

https://www.cnblogs.com/poloyy/category/1690628.html

前言

  • 前面几篇文章主要介绍了allure的特性,这篇文章我们就来讲下allure的标记用法
  • 有时候我们写pytest的时候,会用到 但并不会显示在allure报告上

@pytest.mark

  • 而allure也提供了三种类型的标记装饰器,它们是可以显示在allure报告上的

allure的标记装饰器

  • BDD样式的标记装饰器
  • 优先级(严重程度)标记装饰器
  • 自定义标记装饰器

BDD标记装饰器

提供了两个装饰器

  • @allure.feature
  • @allure.story

直接上代码栗子

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
__title__  =
__Time__   = 2020-04-19 14:27
__Author__ = 小菠萝测试笔记
__Blog__   = https://www.cnblogs.com/poloyy/
"""

import allure


def test_without_any_annotations_that_wont_be_executed():
    pass


@allure.story('epic_1')
def test_with_epic_1():
    pass


@allure.story('story_1')
def test_with_story_1():
    pass


@allure.story('story_2')
def test_with_story_2():
    pass


@allure.feature('feature_2')
@allure.story('story_2')
def test_with_story_2_and_feature_2():
    pass

我们先看看没有设置标记装饰器时,allure报告是咋样的

加了@allure.feature和@allure.story之后,allure报告又是怎么样的呢

知识点

story是feature的子集,当测试用例有 @allure.feature、@allure.story 时,在报告上会先显示feature,点开之后再显示story【可以想象成,安徒生童话(feature)有很多个童话故事(story)】

如果不加 @allure.feature、@allure.story 时,在Behaviors栏目下,测试用例都不会分类显示,当用例多的时候可能看的花里胡哨

总结

倘若是用pytest+allure写项目的话,又想用@pytest.mark.xxx 来自定义标记的话可以尝试用 @allure.feature、@allure.story 替换,毕竟可以显示在报告上

问题来了,用命令行方式运行时,可以指定运行某个story或者feature吗?

答案是:当然可以!!跟@pytest.mark.xxx没啥区别哦!!

  • --allure-features
  • --allure-stories

譬如

代码语言:javascript
复制
 pytest tests.py --allure-stories story_1,story_2
代码语言:javascript
复制
pytest tests.py --allure-features feature2 --allure-stories story2

@ allure.severity

作用:按严重性(优先级)来标记测试用例,它使用allure.severity_level枚举值作为参数

先看看枚举类有哪些常量

严重程度最高blocker,最低trivial

代码语言:javascript
复制
class Severity(str, Enum):
    BLOCKER = 'blocker'
    CRITICAL = 'critical'
    NORMAL = 'normal'
    MINOR = 'minor'
    TRIVIAL = 'trivial'

看看代码栗子

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
__title__  =
__Time__   = 2020-04-19 14:50
__Author__ = 小菠萝测试笔记
__Blog__   = https://www.cnblogs.com/poloyy/
"""

import allure


def test_with_no_severity_label():
    pass


@allure.severity(allure.severity_level.TRIVIAL)
def test_with_trivial_severity():
    pass


@allure.severity(allure.severity_level.NORMAL)
def test_with_normal_severity():
    pass


@allure.severity(allure.severity_level.NORMAL)
class TestClassWithNormalSeverity(object):

    def test_inside_the_normal_severity_test_class(self):
        pass

    @allure.severity(allure.severity_level.CRITICAL)
    def test_inside_the_normal_severity_test_class_with_overriding_critical_severity(self):
        pass

运行结果,查看allure报告

其实就是测试用例多了个优先级severity属性而已...

命令行方式

也可以通过命令行参数运行指定severity的测试用例哦

代码语言:javascript
复制
pytest tests.py --allure-severities normal,critical
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-04-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • allure的标记装饰器
  • BDD标记装饰器
    • 直接上代码栗子
      • 我们先看看没有设置标记装饰器时,allure报告是咋样的
        • 加了@allure.feature和@allure.story之后,allure报告又是怎么样的呢
          • 知识点
            • 总结
              • 问题来了,用命令行方式运行时,可以指定运行某个story或者feature吗?
                • 譬如
                • @ allure.severity
                  • 先看看枚举类有哪些常量
                    • 看看代码栗子
                      • 运行结果,查看allure报告
                        • 命令行方式
                        领券
                        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档