手把手教你Pytest+Allure2.X定制报告详细教程,给本身的项目量身打造一套测试报告-02(很是详细,很是实用)

简介

俗话说“人靠衣服马靠鞍”一个项目作的在好,没有一分的漂亮的测试报告有时候也是很难在客户那边验收的,今天宏哥就带大家解决这一难题。html

前边一篇文章是分享如何搭建pytest+Allure的环境,从而生成一份精美的、让人耳目一新的测试报告,可是有的小伙伴或者童鞋们可能会问,我能不能按照本身的想法为个人项目测试结果量身打造一份属于我本身的测试报告了,固然能够了。函数

Allure经常使用注解

Allure提供了如下经常使用注解(未列出部分请访问官网了解),具体用法以下。学习

Feature: 标注主要功能模块
Story: 标注Features功能模块下的分支功能
Severity: 标注测试用例的重要级别
Step: 标注测试用例的重要步骤
Issue和TestCase: 标注Issue、Case,可加入URL测试

一、Features定制详解

(1)代码实现

(2)参考代码

# coding=utf-8 # 1.先设置编码,utf-8可支持中英文,如上,通常放在第一行 # 2.注释:包括记录建立时间,建立人,项目名称。 ''' Created on 2019-9-30 @author: 北京-宏哥   QQ交流群:707699217 Project:手把手教你Pytest+Allure2.X定制报告详细教程,给本身的项目量身打造一套测试报告 ''' # 3.导入模块 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'])

(3)添加feature,Report展现见下图。

二、Story定制详解

(1)代码实现

(2)参考代码

# coding=utf-8 # 1.先设置编码,utf-8可支持中英文,如上,通常放在第一行 # 2.注释:包括记录建立时间,建立人,项目名称。 ''' Created on 2019-9-29 @author: 北京-宏哥   QQ交流群:707699217 Project:手把手教你Pytest+Allure2.X定制报告详细教程,给本身的项目量身打造一套测试报告 ''' # 3.导入模块 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_02') @allure.story('test_story_02') def test_case_02(): """     用例描述:Test case 02
    """     assert 0 == 0


if __name__ == '__main__': pytest.main(['-s', '-q', '--alluredir', './report'])

(3)添加story,Report展现见下图。

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

(1)代码实现

(2)参考代码

# coding=utf-8 # 1.先设置编码,utf-8可支持中英文,如上,通常放在第一行 # 2.注释:包括记录建立时间,建立人,项目名称。 ''' Created on 2019-9-29 @author: 北京-宏哥   QQ交流群:707699217 Project:手把手教你Pytest+Allure2.X定制报告详细教程,给本身的项目量身打造一套测试报告 ''' # 3.导入模块 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'])

(3)添加用例标题和用例描述,Report展现见下图。

4 、Severity定制详解

Allure中对严重级别的定义:
一、 Blocker级别:中断缺陷(客户端程序无响应,没法执行下一步操做)
二、 Critical级别:临界缺陷( 功能点缺失)
三、 Normal级别:普通缺陷(数值计算错误)
四、 Minor级别:次要缺陷(界面错误与UI需求不符)
五、 Trivial级别:轻微缺陷(必输项无提示,或者提示不规范)编码

 

(1)代码实现

(2)参考代码

# coding=utf-8 # 1.先设置编码,utf-8可支持中英文,如上,通常放在第一行 # 2.注释:包括记录建立时间,建立人,项目名称。 ''' Created on 2019-9-29 @author: 北京-宏哥   QQ交流群:707699217 Project:手把手教你Pytest+Allure2.X定制报告详细教程,给本身的项目量身打造一套测试报告 ''' # 3.导入模块 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'])

(3)添加Severity,Report展现见下图。

五、Step定制详解

(1)代码实现

(2)参考代码

# coding=utf-8 # 1.先设置编码,utf-8可支持中英文,如上,通常放在第一行 # 2.注释:包括记录建立时间,建立人,项目名称。 ''' Created on 2019-9-29 @author: 北京-宏哥   QQ交流群:707699217 Project:手把手教你Pytest+Allure2.X定制报告详细教程,给本身的项目量身打造一套测试报告 ''' # 3.导入模块 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 = '宏哥' assert str_add(str1, str2) == 'hello宏哥'


if __name__ == '__main__': pytest.main(['-s', '-q', '--alluredir', './report'])

(3)添加Step,Report展现见下图。 

六、Issue和TestCase定制详解

(1)代码实现

(2)参考代码

# coding=utf-8 # 1.先设置编码,utf-8可支持中英文,如上,通常放在第一行 # 2.注释:包括记录建立时间,建立人,项目名称。 ''' Created on 2019-9-29 @author: 北京-宏哥   QQ交流群:707699217 Project:手把手教你Pytest+Allure2.X定制报告详细教程,给本身的项目量身打造一套测试报告 ''' # 3.导入模块 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 = '宏哥' assert str_add(str1, str2) == 'hello宏哥'


if __name__ == '__main__': pytest.main(['-s', '-q', '--alluredir', './report'])

(3)添加Issue和TestCase,Report展现见下图。

七、attach定制详解

(1)代码实现

(2)参考代码

# coding=utf-8 # 1.先设置编码,utf-8可支持中英文,如上,通常放在第一行 # 2.注释:包括记录建立时间,建立人,项目名称。 ''' Created on 2019-9-29 @author: 北京-宏哥   QQ交流群:707699217 Project:手把手教你Pytest+Allure2.X定制报告详细教程,给本身的项目量身打造一套测试报告 ''' # 3.导入模块 import allure import pytest # file = open('C:\\Users\\DELL\\Desktop\\duhong\\test.png', 'rb').read() # allure.attach('test_img', file, allure.attachment_type.PNG)PNG @pytest.fixture def attach_file_in_module_scope_fixture_with_finalizer(request): allure.attach('A text attacment in module scope fixture', 'blah blah blah', allure.attachment_type.TEXT) def finalizer_module_scope_fixture(): allure.attach('A text attacment in module scope finalizer', 'blah blah blah blah', allure.attachment_type.TEXT) request.addfinalizer(finalizer_module_scope_fixture) def test_with_attacments_in_fixture_and_finalizer(attach_file_in_module_scope_finalizer): pass def test_multiple_attachments(): allure.attach.file('C:\\Users\\DELL\\Desktop\\duhong\\test.png', attachment_type=allure.attachment_type.PNG) allure.attach('<head></head><body> a page </body>', 'Attach with HTML type', allure.attachment_type.HTML)

(3)添加attach参数,Report展现见下图。

 

 

 

 

小结

此外,Allure还支持Jenkins Plugin,后面我会专门写一篇博文给小伙伴们继续介绍和分享,感兴趣的话请关注个人博客。spa

其实上面说的你们均可以到官网来学习allure的用法和使用。code

好了,明天就是国庆节了,宏哥这里提早祝你们国庆节快乐,吃好玩好喝好。orm

您的确定就是我进步的动力。若是你感受还不错,就请鼓励一下吧!记得点波 推荐 哦!!!(点击右边的小球便可!(^__^) 嘻嘻……)htm

原文出处:https://www.cnblogs.com/du-hong/p/11607926.htmlblog

相关文章
相关标签/搜索