Pytest_定制allure报告(12)

定制报告需要先导入allure模块,再使用以下装饰器方法:

  • feature: 标注主要功能模块。
  • story: 标注feature功能模块下的分支功能。
  • description:在报告中显示用例描述。
  • step: 标注测试用例步骤。
  • issue && testcase:标注用例关联的链接。
  • attach: 添加一些附加内容到测试报告中。
  • severity: 标注测试用例的重要级别,包含blocker, critical, normal, minor, trivial 几个不同的等级。

feature && story

主要用于为用例分层级

import allure


@allure.feature("评论模块")
class TestComment:

    @allure.story("填写所有信息,点击提交,预期评论成功")
    def test_001(self):
        print("
填写信息")
        print("
点击提交")

    @allure.story("不输入任何信息,点击提交,预期提示填写必填项")
    def test_002(self):
        print("
点击提交")

报告样式如下:

description

用于在报告中增加用例描述信息,除了这个方法外,还可以在方法下使用3个引号的方式增加用例描述信息。

import allure


@allure.feature("评论模块")
class TestComment:

    @allure.story("填写所有信息,点击提交,预期评论成功")
    @allure.description("用例描述...这样...那样...")
    def test_001(self):
        print("
填写信息")
        print("
点击提交")

    @allure.story("不输入任何信息,点击提交,预期提示填写必填项")
    def test_002(self):
        """用例描述...也可以...这样...那样"""
        print("
点击提交")

报告样式如下:

step

在报告中增加测试步骤的显示。

import allure


@allure.feature("评论模块")
class TestComment:

    @allure.story("填写所有信息,点击提交,预期评论成功")
    @allure.description("用例描述...这样...那样...")
    def test_001(self):
        with allure.step("1、填写信息"):
            print("
填写信息")
            assert 1 == 1
        with allure.step("2、点击提交"):
            print("
点击提交")
            assert 2 == 1

    @allure.story("不输入任何信息,点击提交,预期提示填写必填项")
    def test_002(self):
        """用例描述...也可以...这样...那样"""
        print("
点击提交")

报告样式如下:

issue && testcase

issue与testcase用于在报告中增加关联链接,用法基本一样,第1个参数为关联的url地址,第2个为缺省参数,作用是为地址的别名。

import allure


@allure.feature("评论模块")
class TestComment:

    @allure.story("填写所有信息,点击提交,预期评论成功")
    @allure.description("用例描述...这样...那样...")
    @allure.issue("http://www.baidu.com")
    @allure.testcase("http://www.baidu.com", "百度一下")
    def test_001(self):
        with allure.step("1、填写信息"):
            print("
填写信息")
            assert 1 == 1
        with allure.step("2、点击提交"):
            print("
点击提交")
            assert 2 == 1

    @allure.story("不输入任何信息,点击提交,预期提示填写必填项")
    def test_002(self):
        """用例描述...也可以...这样...那样"""
        print("
点击提交")

报告样式如下:

attach

在报告中添加一些附加内容,内容可以为文本信息、图片、文件等。

import allure


@allure.feature("评论模块")
class TestComment:

    @allure.story("填写所有信息,点击提交,预期评论成功")
    @allure.description("用例描述...这样...那样...")
    @allure.issue("http://www.baidu.com")
    @allure.testcase("http://www.baidu.com", "百度一下")
    def test_001(self):
        with allure.step("1、填写信息"):
            print("
填写信息")
            assert 1 == 1
        with allure.step("2、点击提交"):
            print("
点击提交")

        # attach 添加文本信息
        allure.attach("文本信息标注信息...", "别名")
        # attach 可以添加图片
        allure.attach.file(r"D:UsersUserDesktop图片管理60-60.jpg", "图片", attachment_type=allure.attachment_type.JPG)
        # attach 可以添加html文件
        allure.attach.file(r"D:UsersUserDesktop图片管理	est.html", "html文件", attachment_type=allure.attachment_type.HTML)

    @allure.story("不输入任何信息,点击提交,预期提示填写必填项")
    def test_002(self):
        """用例描述...也可以...这样...那样"""
        print("
点击提交")

报告样式如下:

severity

为测试用例的划分重要级别,包含blocker, critical, normal, minor, trivial 5个不同的等级。默认是normal级别。

import allure


@allure.feature("评论模块")
class TestComment:

    @allure.story("填写所有信息,点击提交,预期评论成功")
    @allure.description("用例描述...这样...那样...")
    @allure.issue("http://www.baidu.com")
    @allure.testcase("http://www.baidu.com", "百度一下")
    def test_001(self):
        with allure.step("1、填写信息"):
            print("
填写信息")
            assert 1 == 1
        with allure.step("2、点击提交"):
            print("
点击提交")

        # attach 添加文本信息
        allure.attach("文本信息标注信息...", "别名")
        # attach 可以添加图片
        allure.attach.file(r"D:UsersUserDesktop图片管理60-60.jpg", "图片", attachment_type=allure.attachment_type.JPG)
        # attach 可以添加html文件
        allure.attach.file(r"D:UsersUserDesktop图片管理	est.html", "html文件", attachment_type=allure.attachment_type.HTML)

    @allure.severity("blocker")
    def test_002(self):
        pass

    @allure.severity("critical")
    def test_003(self):
        pass

    @allure.severity("minor")
    def test_004(self):
        assert 1 == 2

报告样式如下:

环境配置信息

在概览中查看环境配置默认是没有的。

若要在报告中增加环境信息需要在第一步生成的json文件中,增加一个environment.properties文件,文件内容如下样式:

systemVersion=win10
pythonVersion=3.8.5
allureVersion=2.13.9
baseUrl=http://192.168.1.x:8080
projectName=test

然后再执行并生成报告,报告样式如下:

在allure 1.X的中,可以通过一个以test开头的py文件来配置,该方法在 2.X已弃用,仅供了解:

报告样式如下:

原文地址:https://www.cnblogs.com/testlearn/p/14739138.html