Pytest测试框架系列 - allure 报告进阶使用

前言

上一篇文章我们讲解了pytest allure的使用,但是还有一些知识点我们还没有学会,下面让我们继续学习,更加灵活的使用allure报告。

进阶功能

  • allure 命令行参数
  • 与参数化 parametrize 结合使用
  • allure.dynamic 动态更新信息
  • allure 报告显示环境配置信息

Allure 命令行参数

前面文章我们学习了很多命令行参数,这里说的是allure相关的命令行参数,主要用来用例筛选,跟前面用例标记一样,这里的标记不需要我们新建标记,由于我们在使用allure属性的时候用例已经标记。

我们可以通过 pytest -h,查看有哪些命令参数

--allure-severities=SEVERITIES_SET
                        Comma-separated list of severity names.
                        Tests only with these severities will be run.
                        Possible values are: blocker, critical, normal, minor, trivial.
  --allure-epics=EPICS_SET
                        Comma-separated list of epic names.
                        Run tests that have at least one of the specified feature labels.
  --allure-features=FEATURES_SET
                        Comma-separated list of feature names.
                        Run tests that have at least one of the specified feature labels.
  --allure-stories=STORIES_SET
                        Comma-separated list of story names.
                        Run tests that have at least one of the specified story labels.
  --allure-ids=IDS_SET  Comma-separated list of IDs.
                        Run tests that have at least one of the specified id labels.
  --allure-link-pattern=LINK_TYPE:LINK_PATTERN
                        Url pattern for link type. Allows short links in test,
                        like 'issue-1'. Text will be formatted to full url with python
                        str.format().

从上面可以看出来常用命令行参数,但是常用的就是2个:

  • –allure-epics=EPICS_SET
  • –allure-severities=SEVERITIES_SET

–allure-epics=EPICS_SET 根据业务功能执行用例,关键代码示例:

# !/usr/bin/python3
# _*_coding:utf-8 _*_
""""
# @Time  :2021/7/14 14:46
# @Author  : king
# @File    :test_allure.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""
import logging
import allure
import pytest
from class_06.case_step import step_02, step_01, step_03, step_04, step_05

@allure.epic("教材")
@allure.feature("教材管理")
class TestTextBook:

    @allure.story("添加教材成功")
    @allure.testcase("http://XXX.XXX.x.x:8081/butly/testcase-1.html")
    @allure.issue("http://XXX.XXX.x.x:8081/butly/bug-1.html")
    @allure.link("https://blog.csdn.net/u010454117")
    @allure.title("添加教材")
    @allure.description_html("<h2><font color='red'>正常添加教材</font></h2>")
    @allure.severity(allure.severity_level.BLOCKER)
    def test_add_textbook(self):
        """用例描述:
        1.在教材界面,点击新增按钮
        2.在新增教材界面,选择教材版本、章节信息
        3.点击确定按钮
        """
        logging.info("开始添加教材操作")
        step_01()
        step_02()
        step_05()
        logging.info("添加教材完成")

    @allure.testcase("http://XXX.XXX.x.x:8081/butly/testcase-2.html")
    @allure.issue("http://XXX.XXX.x.x:8081/butly/bug-3.html")
    @allure.link("https://blog.csdn.net/u010454117")
    @allure.title("删除教材")
    @allure.story("删除教材成功")
    @allure.description("正常删除教材数据")
    @allure.severity(allure.severity_level.CRITICAL)
    def test_delete_textbook(self):
        """用例描述:
        1.在教材界面,选择需要删除的教材
        2.在教材界面,点击删除按钮
        3.再确认删除界面,点击确定按钮
        """
        logging.info("开始删除教材操作")
        step_03()
        step_04()
        logging.info("删除教材完成")

@allure.epic("资源")
@allure.feature("资源管理")
class TestResources:

    @allure.testcase("http://XXX.XXX.x.x:8081/butly/testcase-5.html")
    @allure.issue("http://XXX.XXX.x.x:8081/butly/bug-5.html")
    @allure.link("https://blog.csdn.net/u010454117")
    @allure.title("添加资源")
    @allure.story("添加资源成功")
    @allure.severity(allure.severity_level.BLOCKER)
    def test_add_resources(self):
        """用例描述:
        1.在资源界面,点击添加的资源
        2.在资源预览界面,点击添加按钮
        """
        logging.info("开始添加资源操作")
        step_01()
        step_02()
        step_05()
        logging.info("添加资源完成")

    @allure.testcase("http://XXX.XXX.x.x:8081/butly/testcase-6.html")
    @allure.issue("http://XXX.XXX.x.x:8081/butly/bug-6.html")
    @allure.link("https://blog.csdn.net/u010454117")
    @allure.title("删除资源")
    @allure.story("删除资源成功")
    @allure.severity(allure.severity_level.CRITICAL)
    def test_delete_resources(self):
        """用例描述:
        1.在备课界面,点击资源的删除按钮
        2.在资源删除确定界面,点击确定按钮
        """
        logging.info("开始删除资源操作")
        step_01()
        step_04()
        logging.info("删除资源完成")
        assert "删除成功" == "删除成功"

if __name__ == '__main__':
    pytest.main()

在命令行执行命令 pytest -v --alluredir ./report/allure --allure-epics=资源, 查看结果:从执行结果看出来,只执行了两条用例。
执行结果

–allure-severities=SEVERITIES_SET 根据用例等级执行

演示代码上述一样,在命令行执行命令 pytest -v --alluredir ./report/allure --allure-severities=blocker, 查看结果:从执行结果看出来,只执行了两条用例。
执行结果
使用allure命令行参数筛选,减少了在手动打标记的操作,使用场景: 冒烟用例可以标记为block、或者只允许一个业务的功能等

与参数化 parametrize 结合使用

我们特别在做接口自动化的时候,参数化场景使用的很多,我们先来看下参数化和allure报告基本结合的示例:

# !/usr/bin/python3
# _*_coding:utf-8 _*_
""""
# @Time  :2021/7/16 22:14
# @Author  : king
# @File    :test_allure_params.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""
import allure
import pytest


datas = [
    {"user": "king", "pwd": 123456},
    {"user": "king1", "pwd": 1234567},
    {"user": "hello", "pwd": 123456}
]

@pytest.mark.parametrize("data", datas)
def test_login(data):
    print(data)
    assert data["user"].startswith("king") and data["pwd"] == 123456

if __name__ == '__main__':
    pytest.main(["-s", "-v"])

我们执行 pytest --alluredir ./report test_allure_params.pyallure serve ./report 后,查看 allure 报告。
执行结果
现在看这个报告,用例标题显示的不太友好,可读性不强,我们可以通过三种方式进行优化

  • 通过给@pytest.mark.parametrize 增加一个参数 ids,前面讲解参数化的时候讲解过
  • 通过@allure.title()增加
  • 通过allure.dynamic.title(title) 动态修改属性(后面讲解)

通过给@pytest.mark.parametrize

示例:

@pytest.mark.parametrize("data", datas, 
ids=["正确的用户名和密码", "正确的用户名和错误的密码", "不存在的用户名和密码"])
def test_login(data):
    print(data)
    assert data["user"].startswith("king") and data["pwd"] == 123456

我们再次执行 pytest --alluredir ./report test_allure_params.pyallure serve ./report 后,查看 allure 报告,现在看起来用例标题是不是友好很多了
执行结果

通过@allure.title()

示例:

# !/usr/bin/python3
# _*_coding:utf-8 _*_
""""
# @Time  :2021/7/16 23:39
# @Author  : king
# @File    :test_allure_params1.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""
import allure
import pytest

datas = [
    {"user": "king", "pwd": 123456, "information": "正确的用户名和密码"},
    {"user": "king1", "pwd": 1234567, "information": "正确的用户名和错误的密码"},
    {"user": "hello", "pwd": 123456, "information": "不存在的用户名和密码"}
]

@pytest.mark.parametrize("data", datas)
@allure.title('{data[information]}')
def test_login(data):
    print(data)
    assert data["user"].startswith("king") and data["pwd"] == 123456

if __name__ == '__main__':
    pytest.main(["-s", "-v"])

我们继续再次执行 pytest --alluredir ./report test_allure_params.pyallure serve ./report 后,查看 allure 报告,现在看起来用例标题更加简洁
执行结果

allure.dynamic 动态更新信息

有时候我们需要根据用例信息动态显示信息,这样我们就需要在用例执行期间或者用例执行完成修改我们的展示信息,这就用到了动态更新信息功能。

  • allure.dynamic.feature
  • allure.dynamic.link
  • allure.dynamic.issue
  • allure.dynamic.testcase
  • allure.dynamic.story
  • allure.dynamic.title
  • allure.dynamic.description

我们这里就讲下常用的 allure.dynamic.titleallure.dynamic.description

场景:我们根据标识来进行不同的逻辑判断执行不同的方法

示例:

# !/usr/bin/python3
# _*_coding:utf-8 _*_
""""
# @Time  :2021/7/17 0:11
# @Author  : king
# @File    :test_update_info.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""
import allure
import pytest

def login(user, pwd):
    if user.startswith("king") and pwd == 123456:
        print("用户登录成功!")
        return True
    print("用户登录失败!")
    return False

def home(user):
    print("{} 您好,欢迎登录系统!".format(user))

datas = [
    {"user": "king", "pwd": 123456},
    {"user": "king1", "pwd": 1234567},
    {"user": "hello", "pwd": 123456}
]

@pytest.mark.parametrize("data", datas)
def test_login(data):
    flag = login(data["user"], data["pwd"])
    if flag:
        allure.dynamic.title("登录成功,进入首页")
        description = """
            1.登录系统,登录参数为,用户名:{} ,密码 {}
            2.进入首页
        """.format(data["user"], data["pwd"])
        allure.dynamic.description(description)
        home(data["user"])
    else:
        allure.dynamic.title("登录失败,提示错误信息")
        description = """
                    1.登录系统,登录参数为,用户名:{} ,密码 {}
                    2.登录失败,提示错误信息
                """.format(data["user"], data["pwd"])
        allure.dynamic.description(description)
        allure.dynamic.title("登录失败,登录用户名:{} ,密码 {}".format(data["user"], data["pwd"]))
        raise

if __name__ == '__main__':
    pytest.main()

我们执行 pytest --alluredir ./report test_updata_info.pyallure serve ./report 后,查看 allure 报告
执行结果
动态修改信息就讲到这里,其他动态更新信息方法可以自己动手实践一下,但是这里大家可能有个疑问,当时同时存在 allure.dynamic.title@allure.title()ids 时,那该怎么显示呢?

通过源码..\Lib\site-packages\allure_pytest\utils.py111行可以看出来优先级:

def allure_name(item, parameters):
    name = escape_name(item.name)
    title = allure_title(item)
    return title.format(**{**parameters, **item.funcargs}) if title else name

具体执行过程就不做多讲解,大家选择使用其中一种方式即可。

优先级:allure.dynamic.title > @allure.title() > ids

allure 报告显示环境配置信息

从allure报告总览页面看见,环境区域显示为空,既然有这个功能,我们就能够配置环境。
allure 环境显示
根据官方文档说明:
环境配置
要将信息添加到环境小部件,只需在生成报告之前创建environment.properties(或environment.xml)文件到allure-results目录

  • environment.properties
  • environment.xml

environment.properties 示例
环境变量
environment.properties

platform=Windows
python.Version=3.7.0
pytest.Version=6.2.4
allure-pytest.Version=2.9.43
project=lecture
User=king

我们执行 allure serve ./report 后,查看 allure 报告,查看环境区域显示
环境显示
environment.xml 方式
示例
environment.xml

<environment>
    <parameter>
        <key>platform</key>
        <value>Windows</value>
    </parameter>
    <parameter>
        <key>python.Version</key>
        <value>3.7.0</value>
    </parameter>
    <parameter>
        <key>pytest.Version</key>
        <value>6.2.4</value>
    </parameter>
    <parameter>
        <key>allure-pytest.Version</key>
        <value>2.9.43</value>
    </parameter>
    <parameter>
        <key>project</key>
        <value>Lecture</value>
    </parameter>
        <parameter>
        <key>User</key>
        <value>king</value>
    </parameter>
</environment>

我们执行 allure serve ./report 后,查看 allure 报告,查看环境区域显示
执行环境
问题:我们现在每次都是在结果界面新建并输入,但是我们如果代码自动执行时,肯定不能手动新建环境配置文件

解决方法:我们可以新建一个文件复制方法将环境配置文件,在生成报告之前复制到执行结果目录,或者直接在结果目录代码新建配置文件并写入环境信息

总结

  • allure 命令行参数,使用allure命令行参数筛选,减少手动打标记的操作,使用场景: 冒烟用例可以标记为block、或者按照业务功能用例执行等,执行用例时根据命令行参数筛选指定用例
  • 与参数化 parametrize 结合使用,主要是更新用例属性信息,使用报告内容显示更加友好
  • allure.dynamic 动态更新信息,我们需要根据用例信息动态显示信息,这样我们就需要在用例执行期间或者用例执行完成修改我们的展示信息
  • allure 报告显示环境配置信息,要将环境信息添加到报告模块,只需在生成报告之前创建environment.properties(或environment.xml)文件到allure-results目录

以上为内容纯属个人理解,如有不足,欢迎各位大神指正,转载请注明出处!

如果觉得文章不错,欢迎关注微信公众号,微信公众号每天推送相关测试技术文章
个人微信号

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

测试之路king

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值