一、前言

前面我们简单介绍了如何快速生成一个allure测试报告,其实allure也是可以自定义展示的。allure报告可以自定义展示很多详细的信息描述测试用例,包括epic、feature、story、title、issue、testcase、severity等。

二、学习目标

1.allure装饰器介绍

2.allure装饰器应用

三、知识点

1.【allure装饰器介绍】

使用方法 参数值 参数说明
@allure.epic() epic描述 敏捷里面的概念,定义史诗,往下是feature
@allure.feature() 模块名称 功能点的描述,往下是story
@allure.story() 用户故事 用户故事,往下是title
@allure.title(用例的标题) 用例的标题 重命名html报告名称
@allure.testcase() 测试用例的链接地址 对应功能测试用例系统里面的case
@allure.issue() 缺陷 对应缺陷管理系统里面的链接
@allure.description() 用例描述 测试用例的描述
@allure.step() 操作步骤 测试用例的步骤
@allure.severity() 用例等级 blocker,critical,normal,minor,trivial
@allure.link() 链接 定义一个链接,在测试报告展现
@allure.attachment() 附件 报告添加附件

2.【allure装饰器应用】

测试用例代码:

import allure

def input_username(username):
    print("输入用户名:{}".format(username))

def input_password(password):
    print("输入密码:{}".format(password))

class TestCase():

    def test_login_case01(self):
        '''测试用例:正确用户名,正确密码'''
        input_username("okname")
        input_password("okpsw")
        assert 1 == 1

    def test_login_case02(self):
        '''测试用例:正确用户名,错误密码'''
        input_username("okname")
        input_password("errpsw")
        assert 1 == 1

    def test_login_case03(self):
        '''测试用例:错误用户名,正确密码'''
        input_username("errname")
        input_password("okpsw")
        assert 1 == 1

以上是不做修改的报告的用例展示。下面我们通过上面示例代码的基础上,增加自定义内容。

  • step——测试用例的步骤

    用来给报告上的测试用例添加步骤描述。

    • 语法:

      @allure.step("step:步骤")             #方式一,写在函数的上方
      with allure.step("step:步骤")         #方式二,写在函数的内部
      
    • 参数:

      传入步骤描述信息,字符串类型

    • 代码示例:

      import allure
      
      @allure.step("step:用户名输入")   #<<<<<<<修改处>>>>>>>
      def input_username(username):
          print("输入用户名:{}".format(username))
      
      @allure.step("step:密码输入")     #<<<<<<<修改处>>>>>>>
      def input_password(password):
          print("输入密码:{}".format(password))
      
  • feature——测试用例特性(主要功能模块)

    修改Behaviors菜单的功能描述,用来给功能模块增加描述信息,如登录模块,用户管理模块等。

    • 语法:

      @allure.feature("功能模块")
      
    • 参数:

      字符串

    • 代码示例:

      @allure.feature("登录功能")   #<<<<<<<修改处>>>>>>>
      class TestCase():
      
      
  • story——feature功能模块下的分支功能

    修改Behaviors菜单的功能描述,功能模块下的子分支。

    • 语法:

      @allure.story("子功能描述")
      
    • 参数:

      字符串

    • 代码示例:

          @allure.story("正确用户名,正确密码")   #<<<<<<<修改处>>>>>>>
          def test_login_case01(self):
              '''测试用例:正确用户名,正确密码'''
              input_username("okname")
              input_password("okpsw")
              assert 1 == 1
      
          @allure.story("正确用户名,错误密码")    #<<<<<<<修改处>>>>>>>
          def test_login_case02(self):
              '''测试用例:正确用户名,错误密码'''
              input_username("okname")
              input_password("errpsw")
              assert 1 == 1
      
          @allure.story("错误用户名,正确密码")    #<<<<<<<修改处>>>>>>>
          def test_login_case03(self):
              '''测试用例:错误用户名,正确密码'''
              input_username("errname")
              input_password("okpsw")
              assert 1 == 1
      
      
  • link/issue/testcase——链接

    • 语法:

      @allure.link(url,name=None)
      @allure.issue(url,name=None)
      @allure.testcase(url,name=None)
      
    • 参数:

      字符串,链接地址,链接名

    • 代码示例:

          @allure.link(url='https://www.baidu.com/',name='link_url')             #<<<<<<<修改处>>>>>>>
          @allure.issue(url='https://www.baidu.com/',name='issue_url')           #<<<<<<<修改处>>>>>>>
          @allure.testcase(url='https://www.baidu.com/',name='testcase_url')     #<<<<<<<修改处>>>>>>>
          def test_login_case01(self):
              '''测试用例:正确用户名,正确密码'''
              input_username("okname")
              input_password("okpsw")
              assert 1 == 1
      
  • description——用例描述

    • 语法:

      @allure.description("用例描述")  提供描述字符串的装饰器
      @allure.description_html("用例描述")  提供一些HTML在测试用例的描述部分
      
    • 参数:

      字符串

    • 代码示例:

          @allure.description("这是用例描述信息!")  #<<<<<<<修改处>>>>>>>
          def test_login_case01(self):
              '''测试用例:正确用户名,正确密码'''
              input_username("okname")
              input_password("okpsw")
              assert 1 == 1
      
  • severity——测试用例的严重级别

    Allure中对严重级别的定义:

    blocker级别:中断缺陷(客户端程序无响应,无法执行下一步操作)

    critical级别:临界缺陷( 功能点缺失)

    normal级别:普通缺陷(数值计算错误)

    minor级别:次要缺陷(界面错误与UI需求不符)

    trivial级别:轻微缺陷(必输项无提示,或者提示不规范)

    如果不指定用例级别则默认为normal

    • 语法:

      @allure.severity(allure.severity_level.CRITICAL) 或者 @allure.severity('critical')
      
    • 参数:

      字符串

    • 代码示例:

          @allure.severity('trivial')  #<<<<<<<修改处>>>>>>>
          def test_login_case02(self):
              '''测试用例:正确用户名,错误密码'''
              input_username("okname")
              input_password("errpsw")
              assert 1 == 1
      
          @allure.severity('minor')   #<<<<<<<修改处>>>>>>>
          def test_login_case03(self):
              '''测试用例:错误用户名,正确密码'''
              input_username("errname")
              input_password("okpsw")
              assert 1 == 1
      
  • attach——用于向测试报告中输入一些附加的信息,通常是一些测试数据信息

    • 语法:

      allure.attach(body, name, attachment_type, extension)
      allure.attach.file(source, name, attachment_type, extension)
      
    • 参数:

      body :要写入文件的原始内容
      name -:包含文件名的字符串
      attachment_type -:其中一个allure.attachment_type值
      extension :提供的将用作创建文件的扩展名

      source:文件路径

      #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")
      
    • 测试用例中添加文本、图片、html

      代码示例:

          def test_login_case03(self):
              '''测试用例:错误用户名,正确密码'''
              input_username("errname")
              input_password("okpsw")
              allure.attach(body="这是一段文本!", name="test文本", attachment_type=allure.attachment_type.TEXT) #添加文本
              allure.attach.file("./baidu.png",name='截图', attachment_type=allure.attachment_type.PNG)       #添加图片
      
              allure.attach("<html><body><font color='red'>这是一段html</font></body></html>",name='网页',     #添加html
                            attachment_type=allure.attachment_type.HTML)
              assert 1 == 1