博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Pytest+Allure定制报告
阅读量:7137 次
发布时间:2019-06-28

本文共 5107 字,大约阅读时间需要 17 分钟。

前言:

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

就是它,就是它:!!!

先上一张报告效果图: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.pyimport allureimport 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.pyimport allureimport 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 == 0if __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.pyimport allureimport pytest@allure.feature('test_module_01')@allure.story('test_story_01')#test_case_01为用例titledef test_case_01():    """    用例描述:这是用例描述,Test case 01,描述本人    """    #注释为用例描述    assert 0if __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.pyimport allureimport 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定制详解
# -*- coding: utf-8 -*-# @Time    : 2018/8/17 上午10:10# @Author  : WangJuan# @File    : test_case.pyimport allureimport 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展示见下图。 image.png

6、Issue和TestCase定制详解
# -*- coding: utf-8 -*-# @Time    : 2018/8/17 上午10:10# @Author  : WangJuan# @File    : test_case.pyimport allureimport 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

此外,Allure还支持Jenkins Plugin,后面我会专门写一篇博文介绍,感兴趣的话请关注我的。


以上,对你有帮助的话,点赞吧️~~

作者:

出处:
如果对您有帮助,请关注我的同名简书:
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

你可能感兴趣的文章
上传图片 以及做成缩略图
查看>>
封装和多态
查看>>
POJ - 3041 Asteroids 【二分图匹配】
查看>>
luogu P4198 楼房重建——线段树
查看>>
使用property为类中的数据添加行为
查看>>
程序设计基础知识
查看>>
复变函数与积分变换
查看>>
12. 断点续传的原理
查看>>
C#基础:多态:基类可以定义并实现虚(virtual)方法,派生类可以重写(override)这些方法...
查看>>
Visifire图表
查看>>
python常用模块之paramiko与ssh
查看>>
AES算法在Python中的使用
查看>>
动手动脑3
查看>>
HDU 3397 Sequence operation(线段树区间染色加区间合并)
查看>>
【随笔】写下现在所想的,开始写博客
查看>>
linux命令之vi文本编辑器
查看>>
WPF-WrapPanel
查看>>
大家好
查看>>
iOS 疑难杂症 — — UITableView 添加 tableFooterView 旋转屏幕后收不到点击事件!!!...
查看>>
asp.net 8 Request,Response,Server
查看>>