Pytest框架之 - Allure特性之定制测试报告

Pytest支持使用@allure的一些方法修饰测试用例, 使测试用例在allure报告中能够更加详细的显示测试过程

定制结果展示

Pytest框架之 - Allure特性之定制测试报告_第1张图片

 


@allure.epic:敏捷里面的概念,定义史诗;  下层是feature

@allure.feature:功能点的描述,理解成模块;  下层是story

@allure.story:故事;  下层是title

@allure.title:用例标题;  最底层

epic、feature、story和title示例

#!/usr/bin/python3.6
# coding=utf-8
# Author: 文

import pytest
import allure

@allure.epic("全局")                                  # 史诗,最顶层
@allure.feature("登录模块")                            # 测试用例特性,主要功能模块
class Test_Login():
    @allure.title("登录成功")                          # 测试用例标题,最底层
    @allure.story("成功登录")                          # 主功能模块下的分支功能
    def test_01(self):
        print("-----> test_01")

    @allure.title("账号不存在")                         # 测试用例标题,最底层
    @allure.story("登录失败")                           # 功能模块下的分支功能
    def test_02(self):
        print("-----> test_02")

    @allure.title("密码错误")                           # 测试用例标题,最底层
    @allure.story("登录失败")                           # 功能模块下的分支功能
    def test_03(self):
        print("-----> test_03")

@allure.epic("全局")                                   # 史诗,最顶层
@allure.feature("作业模块")                             # 测试用例特性,主要功能模块
class Test_Work():
    @allure.title("审批作业")                           # 测试用例标题,最底层
    @allure.story("审批作业")                           # 功能模块下的分支功能
    def test_04(self):
        print("-----> test_04")

Allure报告展示结果:

Pytest框架之 - Allure特性之定制测试报告_第2张图片


@allure.description:测试用例的描述

@allure.step:测试用例的步骤;  嵌套很好用

step和description示例

#!/usr/bin/python3.6
# coding=utf-8
# Author: 文

import pytest
import allure

class Test_Login():
    @allure.description("账号密码正确成功登录")                               # 测试用例的描述
    @allure.step("第一步:成功登录管理后台")                                   # 测试步骤
    def test_01(self):
        print("-----> test_01")
        self.username()
        self.password()
        self.button()

    @allure.step("第二步:输入账号")                                          # 测试步骤
    def username(self):
        print("username")

    @allure.step("第三步:输入密码")                                          # 测试步骤
    def password(self):
        print("password")

    @allure.step("第四步:点击登录")                                          # 测试步骤
    def button(self):
        print("password")

Allure报告展示结果:

Pytest框架之 - Allure特性之定制测试报告_第3张图片


@allure.link:被测链接

@allure.testCase:测试用例链接

@allure.issue:Bug链接(有个小蜘蛛图标)

issue()testcase()其实调用的也是link(),只是link_type不一样(也就是显示样式不一样)

必传参数url:跳转的链接

可选参数name:显示在allure报告的名字,如果不传就是显示完整的链接;建议传!否则可读性不高

可以理解成:三个方法是一样的,我们都提供跳转链接和名字,只是链接的type不一样

link、issue和testCase示例

#!/usr/bin/python3.6
# coding=utf-8
# Author: 文

import pytest
import allure

class Test_Login():
    @allure.testcase(url="https://www.zentao.net", name="测试用例")       # testCase, 测试用例链接
    @allure.link(url="https://www.baidu.com", name="功能页面")            # link: 被测链接
    @allure.issue(url="https://www.weibo.com", name="bug")               # issue: bug链接
    def test_01(self):
        print("-----> test_01")

Allure报告展示结果:

Pytest框架之 - Allure特性之定制测试报告_第4张图片


allure.attach:附件, allure报告支持显示许多不同类型的附件可用于补充测试结果

语法: allure.attach(body, name, attachment_type, extension) 

参数body:要显示的内容(附件)

参数name:附件名字

参数attachment_type:附件类型,是 allure.attachment_type 里面的其中一种

参数extension:附件的扩展名(比较少用)

allure.attachment_type提供了如下附件类型

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")

attach示例

#!/usr/bin/python3.6
# coding=utf-8
# Author: 文

import pytest
import allure

class Test_Login():
    def test_01(self):
        print("-----> test_01")
        with open(file="F:\\xxx\\login.jpg", mode="rb") as f:
            file = f.read()
            allure.attach(body=file, name="登录页面",attachment_type=allure.attachment_type.JPG)

Allure报告展示结果:

Pytest框架之 - Allure特性之定制测试报告_第5张图片


allure.dynamic:动态生成功能,测试用例执行过程中动态指定标题和描述等标签

allure.dynamic示例

#!/usr/bin/python3.6
# coding=utf-8
# Author: 文

import pytest
import allure

class Test_Login():
    def test_01(self):
        allure.dynamic.feature("动态feature")
        allure.dynamic.story("动态story")
        allure.dynamic.title("动态title")
        allure.dynamic.description("动态description")
        allure.dynamic.severity("blocker")
        allure.dynamic.link(url="https://www.baidu.com", name="动态link")
        allure.dynamic.testcase(url="https://www.zentao.net", name="动态testCase")
        allure.dynamic.issue(url="https://www.weibo.com", name="动态issue")
        print("-----> test_01")

Allure报告展示结果:

Pytest框架之 - Allure特性之定制测试报告_第6张图片

如下包含的方法全部支持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)
    @staticmethod
    def suite(suite_name):
        Dynamic.label(LabelType.SUITE, suite_name)
    @staticmethod
    def parent_suite(parent_suite_name):
        Dynamic.label(LabelType.PARENT_SUITE, parent_suite_name)
    @staticmethod
    def sub_suite(sub_suite_name):
        Dynamic.label(LabelType.SUB_SUITE, sub_suite_name)

注:参数化的测试用例通过allure.dynamic动态方法修改测试用例的title提高测试报告的可读性

你可能感兴趣的:(Pytest)