目录


一、Allure的安装及快速入门

1、Allure介绍

2、Allure安装

(1)安装python插件

(2)安装allure

3、allure使用

(1)示例代码

二、Allure 详解

1、title 标题

(1)示例

2、description 描述

(1)示例

3、标签 @allure.feature

(1)示例

4、标签@allure.story

(1)示例

5、 标签 @allure.severity

(1)示例

6、 动态生成allure.dynamic

(1)示例

三、Allure应用接口

四、Allure报告自动生成

(1)配置文件pytest.ini

(2)生成html格式报告

(3)设置程序主运行函数


一、Allure的安装及快速入门

1、Allure介绍

Allure是一款非常轻量级并且非常灵活的开源测试报告生成框架。 Allure 是一个独立的报告插件,生成美观易读的报 告,它支持绝大多数测试框架, 例如TestNG、Pytest、JUint等。它简单易用,易于集成。

官网:http://allure.qatools.ru

帮助文档:https://docs.qameta.io/allure/

2、Allure安装

(1)安装python插件

(2)安装allure

  • 下载: https://bintray.com/qameta/generic/allure2
  • 前置条件:已部署java环境
  • 解压缩到一个目录(不经常动的目录)
  • 将压缩包内的 bin 目录配置到 path 系统环境变量
  • 在命令行中敲 allure 命令,如果提示有这个命令,即为成功

3、allure使用

(1)示例代码

pytest.ini

[pytest]

# 添加行参数

addopts = -s --alluredir ./report/result

# 文件搜索路径

testpaths = ./scripts

# 文件名称 python_files = test_*.py

# 类名称

python_classes = Test*

# 方法名称 python_functions = test_*

allure_test1.py

import pytest

@pytest.fixture()

def sample():

   print('打印输出sample')

def test_01(sample):

  print('test1')

def test_02():

  print('test2----')

def test_03(sample):

  print('test3----')

if __name__ == '__main__':

  pytest.main(['allure_test1.py'])

运行结果,查看report/result目录

生成html,运行命令: $ allure generate report/result -o report/html --clean

使用火狐浏览器访问index.html,不要使用谷歌(谷歌是loading)

生成在线 allure serve report/result

二、Allure 详解

1、title 标题

可以自定义用例标题,标题默认为函数名。@allure.title

(1)示例

import allure

import pytest

@allure.title("用例标题0")

def test_0():

   pass

@allure.title("用例标题1")

def test_1():
   pass

2、description 描述

可以添加测试的详细说明

(1)示例

@allure.title("用例标题0")

@allure.description("这里是对test_0用例的一些详细说明")

def test_0():

  pass

@allure.title("用例标题1")

def test_1():

"""

  test_1的描述

"""

  pass

@allure.title("用例标题2")

def test_2():

   pass

3、标签 @allure.feature

(1)示例

@allure.feature('这里是一级标签:test')

class TestAllure:

  @allure.title("用例标题0")

  @allure.description("这里是对test_0用例的一些详细说明")

  def test_0(self):

    pass

  @allure.title("用例标题1")

   def test_1(self):

    pass

  @allure.title("用例标题2")

  def test_2(self):

    pass

4、标签@allure.story

(1)示例

 @allure.feature('这里是一级标签:test')

class TestAllure:

  @allure.title("用例标题0")

  @allure.description("这里是对test_0用例的一些详细说明")

  @allure.story("这里是二级标签:test_0")

  def test_0(self):

    pass

  @allure.story("这里是二级标签:test_1")

  @allure.title("用例标题1")

  def test_1(self):

     pass

  @allure.story("这里是二级标签:test_2")

  @allure.title("用例标题2")

  def test_2(self):

     pass

5、 标签 @allure.severity

定义用例的级别,Graphs主要有BLOCKER,CRITICAL,MINOR,NORMAL,TRIVIAL等几种类型,默认是NORMAL。

(1)示例

@allure.feature('这里是一级标签:test')

class TestAllure:

  @allure.severity(allure.severity_level.BLOCKER)

  @allure.title("用例标题0")

  @allure.description("这里是对test_0用例的一些详细说明")

  @allure.story("这里是二级标签:test_0")

  def test_0(self):

    pass

  @allure.severity(allure.severity_level.CRITICAL)

  @allure.story("这里是二级标签:test_1")

  @allure.title("用例标题1")

  def test_1(self):

    pass

  @allure.severity(allure.severity_level.NORMAL)

  @allure.story("这里是二级标签:test_2")

   @allure.title("用例标题2")

  def test_2(self):

    pass

只运行指定级别的用例 --allure_severities=critical,blocker

6、 动态生成allure.dynamic

可以使用函数体内的数据动态生成

(1)示例

params_1={"name":"动态获取test1","method":"post","url":"http://www.baidu.com"}

params_2={"name":"动态获取test2","method":"get","url":"http://www.baidu.com"}

@allure.title("用例标题0")

@allure.severity(severity_level=allure.severity_level.CRITICAL)

def test_0():

  pass

@allure.title("用例标题1")

def test_1():

  pass

@pytest.mark.parametrize("params",[params_1,params_2])

def test_2(params):

  allure.dynamic.title(params["name"]) 

三、Allure应用接口

#allure

  #sheet名称 feature 一级标签

  allure.dynamic.feature(sheet_name)

  #模块 story 二级标签

  allure.dynamic.story(case_model)

  #用例ID+接口名称  title

  allure.dynamic.title(case_id+case_name)

  #请求URL 请求类型 期望结果 实际结果描述

  

四、Allure报告自动生成

(1)配置文件pytest.ini

增加生成allure属性 addopts = -s --alluredir ./report/result

(2)生成html格式报告

代码实现自动生成html报告


import os
import subprocess
def init_report():
    cmd = "allure generate report/result -o report/html --clean"
    subprocess.call(cmd, shell=True)
    res_path= os.path.abspath(os.path.dirname(__file__))
    report_path = res_path + "/report/" + "index.html"


(3)设置程序主运行函数

run.py
 
if __name__ == '__main__':
  report_path = Conf.get_report_path()+os.sep+"result"
  report_html_path = Conf.get_report_path()+os.sep+"html"
  pytest.main(["-s","--alluredir",report_path])