接口测试 Pytest+Allure 定制报告

小喜_xx · 2018年08月17日 · 最后由 QMDX 回复于 2019年05月28日 · 6646 次阅读

前言:

最近在研究接口自动化的框架,好的测试报告在整个测试框架起到至关重要的部分。终于被我发现一个超好用的报告框架,不仅报告美观,而且方便 CI 集成。
就是它,就是它:Allure Test Report!!!

先上一张报告效果图:Allure Test Report.png


python 版本及必要库

python 3.5
pytest 3.3.3
pytest-allure-adaptor 1.7.9


一、环境配置

安装 Python 依赖库:
pip3 install pytest
pip3 install pytest-allure-adaptor

安装 Command Tool:
brew tap qatools/formulas
brew install allure-commandline

官方参考文档:https://pypi.org/project/pytest-allure-adaptor/


二、生成 html 报告命令

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

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

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

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

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

打开 index.html,之前写的 case 报告就会呈现在你面前html.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 展示见下图。feature.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 展示见下图。image.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 展示见下图。image.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 展示见下图。image.png

5、Step 定制详解
@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 展示见下图。 image.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 展示见下图。image.png

7、Environment 定制详解
# 具体Environment参数可自行设置
allure.environment(app_package='com.mobile.fm')
allure.environment(app_activity='com.mobile.fm.activity')
allure.environment(device_name='aad464')
allure.environment(platform_name='Android')

添加 Environment 参数,Report 展示见下图。Environment.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 展示见下图。image.png


此外,Allure 还支持 Jenkins Plugin~
感兴趣的话,请移步Jenkins 构建 Allure Report


以上,对你有帮助的话,点赞吧❤️~~
欢迎关注我的简书,博客,TesterHome,Github~~~

共收到 13 条回复 时间 点赞

这个报告看起来确实比 pytest-html 的报告舒服很多。楼主能说明一下定制报告一般那些好处吗?感觉已有的用例报告已经能很好的记录日志和抛出异常了


楼主知道如何把 ids 的值改变成中文不乱码吗?

也就是这里,直接输入中文,allure 显示乱码

rushtang 回复

1、肯定是报告更加美观
2、可以直观看到运行结果的图表分析,包括:状态,响应时间,优先级
3、可以集成 Jenkins,看报告会更加方便快捷

皆非 回复

我觉得这个部分应该不支持中文吧~

皆非 回复

我记得中文显示\u 什么什么的,我是这么做的,先用 open() 打开结果文件,就那个 xml 文件,然后给他转码后生成\\u 什么的,然后用\u 替换掉那个\\u,再给他转回 utf-8,就可以显示中文了

小喜_xx Jenkins 构建 Allure Report 中提及了此贴 08月29日 21:54

如何增加截图?

recall1951 回复

alallure.attach('fb_login_error.png', file, allure.attach_type.PNG)
arg1:是在报告中显示的名称
arg2:表示添加的内容
arg3:表示添加的类型 (支持类型:HTML,JPG,PNG,JSON,OTHER,TEXTXML)

楼主大大这种问题碰到过吗?怎么解决的?

阿廉 Allure 报告生成踩坑与持续集成方法 中提及了此贴 11月23日 17:18

楼主大大,我加了 ids,为何 allure 上不显示哇

windows 怎么安装 Command Tool?

lizzyzd 回复

卸载 allure 相关模块,重新安装 allure-pytest 和 allure-python-commons

请问下,其中 link 和 issue 的值是要写死的吗?不支持{} 传参吗?

需要 登录 後方可回應,如果你還沒有帳號按這裡 注册