python pytest allure_【转载】Python—Pytest+Allure定制报告

Allure Test Report

一款测试报告框架,不仅报告美观,而且方便CI集成。

一、环境配置

安装Python依赖库:

pip3 install pytest

pip3 install pytest-allure-adaptor

安装 Command Tool:

brew tap qatools/formulas

brew install allure-commandline

二、生成html报告命令

1、pytest命令基础上加–alluredir,生成xml报告。

pytest -s -q --alluredir [xml_report_path]

//[xml_report_path]根据自己需要定义文件夹,作者定义为:/report/xml

用例执行完成之后会在[xml_report_path]目录下生成了一堆xml的report文件,当然这不是我们最终想要的美观报告。

95d417c1181056482a866c539cd2b5ba.png

2、需要使用 Command Tool 来生成我们需要的美观报告。

allure generate [xml_report_path] -o [html_report_path] --clean

//[html_report_path]根据自己需要定义文件夹,作者定义为:/report/html

3、通过下面的命令打开测试报告:

allure open -h 127.0.0.1 -p 8083[html_report_path]

打开 index.html,之前写的 case 报告就会呈现在你面前,如下:

c740769d9171840a896bf95bb8dd85d0.png

注:直接用chrome浏览器打开报告,报告可能会是空白页面。

解决办法:

1、在pycharm中右击index.html选择打开方式Open in Browser就可以了。

2、使用Firefox直接打开index.html。

三、定制报告

Feature: 标注主要功能模块

Story: 标注Features功能模块下的分支功能

Severity: 标注测试用例的重要级别

Step: 标注测试用例的重要步骤

Issue和TestCase: 标注Issue、Case,可加入URL

1、Features定制详解

# -*- coding: utf-8 -*-

# @Time : 2018/8/17 上午10:10

# @Author : WangJuan

# @File : test_case.py

import allure

import pytest

@allure.feature('test_module_01')

def test_case_01():

"""

用例描述:Test case 01

"""

assert 0

@allure.feature('test_module_02')

def test_case_02():

"""

用例描述:Test case 02

"""

assert 0 == 0

if __name__ == '__main__':

pytest.main(['-s', '-q', '--alluredir', './report/xml'])

添加feature,Report展示见下图:

874295fe527295ad90a4e8d87bfe0479.png

2、Story定制详解

# -*- coding: utf-8 -*-

# @Time : 2018/8/17 上午10:10

# @Author : WangJuan

# @File : test_case.py

import allure

import pytest

@allure.feature('test_module_01')

@allure.story('test_story_01')

def test_case_01():

"""

用例描述:Test case 01

"""

assert 0

@allure.feature('test_module_01')

@allure.story('test_story_02')

def test_case_02():

"""

用例描述:Test case 02

"""

assert 0 == 0

if __name__ == '__main__':

pytest.main(['-s', '-q', '--alluredir', './report/xml'])

添加story,Report展示见下图:

e8c41579549fdd543c72d239f9752cd1.png

3、用例标题和用例描述定制详解

# -*- coding: utf-8 -*-

# @Time : 2018/8/17 上午10:10

# @Author : WangJuan

# @File : test_case.py

import allure

import pytest

@allure.feature('test_module_01')

@allure.story('test_story_01')

#test_case_01为用例title

def test_case_01():

"""

用例描述:这是用例描述,Test case 01,描述本人

"""

#注释为用例描述

assert 0

if __name__ == '__main__':

pytest.main(['-s', '-q', '--alluredir', './report/xml'])

添加用例标题和用例描述,Report展示见下图:

935f3bbd53f458f6c77e82f4b98fbfb6.png

4 、Severity定制详解

Allure中对严重级别的定义:

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

2、 Critical级别:临界缺陷( 功能点缺失)

3、 Normal级别:普通缺陷(数值计算错误)

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

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

# -*- coding: utf-8 -*-

# @Time : 2018/8/17 上午10:10

# @Author : WangJuan

# @File : test_case.py

import allure

import pytest

@allure.feature('test_module_01')

@allure.story('test_story_01')

@allure.severity('blocker')

def test_case_01():

"""

用例描述:Test case 01

"""

assert 0

@allure.feature('test_module_01')

@allure.story('test_story_01')

@allure.severity('critical')

def test_case_02():

"""

用例描述:Test case 02

"""

assert 0 == 0

@allure.feature('test_module_01')

@allure.story('test_story_02')

@allure.severity('normal')

def test_case_03():

"""

用例描述:Test case 03

"""

assert 0

@allure.feature('test_module_01')

@allure.story('test_story_02')

@allure.severity('minor')

def test_case_04():

"""

用例描述:Test case 04

"""

assert 0 == 0

if __name__ == '__main__':

pytest.main(['-s', '-q', '--alluredir', './report/xml'])

添加Severity,Report展示见下图:

82a281da0728104b0a8d8b19498ee067.png

5、Step定制详解

# -*- coding: utf-8 -*-

# @Time : 2018/8/17 上午10:10

# @Author : WangJuan

# @File : test_case.py

import allure

import pytest

@allure.step("字符串相加:{0},{1}")

# 测试步骤,可通过format机制自动获取函数参数

def str_add(str1, str2):

if not isinstance(str1, str):

return "%s is not a string" % str1

if not isinstance(str2, str):

return "%s is not a string" % str2

return str1 + str2

@allure.feature('test_module_01')

@allure.story('test_story_01')

@allure.severity('blocker')

def test_case():

str1 = 'hello'

str2 = 'world'

assert str_add(str1, str2) == 'helloworld'

if __name__ == '__main__':

pytest.main(['-s', '-q', '--alluredir', './report/xml'])

添加Step,Report展示见下图:

94d1cc804bcfdba72248b1cdd38bded8.png

6、Issue和TestCase定制详解

# -*- coding: utf-8 -*-

# @Time : 2018/8/17 上午10:10

# @Author : WangJuan

# @File : test_case.py

import allure

import pytest

@allure.step("字符串相加:{0},{1}") # 测试步骤,可通过format机制自动获取函数参数

def str_add(str1, str2):

print('hello')

if not isinstance(str1, str):

return "%s is not a string" % str1

if not isinstance(str2, str):

return "%s is not a string" % str2

return str1 + str2

@allure.feature('test_module_01')

@allure.story('test_story_01')

@allure.severity('blocker')

@allure.issue("http://www.baidu.com")

@allure.testcase("http://www.testlink.com")

def test_case():

str1 = 'hello'

str2 = 'world'

assert str_add(str1, str2) == 'helloworld'

if __name__ == '__main__':

pytest.main(['-s', '-q', '--alluredir', './report/xml'])

添加Issue和TestCase,Report展示见下图:

2e00a41a4c38cc1b9bd7606b76b59d3b.png

8、attach定制详解

file = open('../test.png', 'rb').read()

allure.attach('test_img', file, allure.attach_type.PNG)

在报告中增加附件:allure.attach(’arg1’,’arg2’,’arg3’):

arg1:是在报告中显示的附件名称

arg2:表示添加附件的内容

arg3:表示添加的类型(支持:HTML,JPG,PNG,JSON,OTHER,TEXTXML)

添加attach参数,Report展示见下图:

19b51e1bf016068a81bb121c508b488f.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值