简介
1.俗话说“人靠衣服马靠鞍”一个项目做的在好,没有一分的漂亮的测试报告有时候也是很难在客户那边验收的,今天就带你们解决这一难题。
2.前边一篇文章是分享如何搭建pytest+Allure的环境,从而生成一份精美的、让人耳目一新的测试报告,但是有的小伙伴或者童鞋们可能会问,我能不能按照自己的想法为我的项目测试结果量身打造一份属于我自己的测试报告了,当然可以了。
3.Allure常用注解
Feature::标注主要功能模块
Story:标注Feature功能模块下的分支功能
Severity:标注测试用例的重要级别
Step:标注测试用例的重要步骤
Issue和TestCase:标注Issue、Case,可加入URL
4.feature定制详解
test_allure_feature.py
import 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 == 0if __name__ == '__main__':pytest.main(['-s', '-q', '--alluredir', './report'])
4.1.添加feature,Report展示见下图。
5.story定制详解
test_allyre_story.py
import 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_02')@allure.story('test_story_02')def test_case_02():"""用例描述:Test case 02"""assert 0 == 0if __name__ == '__main__':pytest.main(['-s', '-q', '--alluredir', './report'])
5.1.添加story,Report展示见下图。
6.用例标题和用例描述定制详解
test_allure_title_describe.py
import 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'])
6.1.添加用例标题和用例描述,Report展示见下图。
7.severity定制详解
Allure中对严重级别的定义:
7.1.Blocker级别:中断缺陷(客户端程序无响应,无法执行下一步操作)
7.2.Critical级别:临界缺陷( 功能点缺失)
7.3.Normal级别:普通缺陷(数值计算错误)
7.4.Minor级别:次要缺陷(界面错误与UI需求不符)
7.5.Trivial级别:轻微缺陷(必输项无提示,或者提示不规范)
test_allure_severity.py
import 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_02')@allure.story('test_story_02')@allure.severity('critical')def test_case_02():"""用例描述:Test case 02"""assert 0 == 0@allure.feature('test_module_03')@allure.story('test_story_03')@allure.severity('normal')def test_case_03():"""用例描述:Test case 03"""assert 0@allure.feature('test_module_04')@allure.story('test_story_04')@allure.severity('minor')def test_case_04():"""用例描述:Test case 04"""assert 0 == 0if __name__ == '__main__':pytest.main(['-s', '-q', '--alluredir', './report'])
7.6.添加Severity,Report展示见下图。
8.step定制详解
test_allure_step.py
import allureimport pytest@allure.step("字符串相加:{0},{1}")#测试步骤,可通过format机制自动获取函数参数def str_add(str1, str2):if not isinstance(str1, str):return "%s is not a string" % str1if not isinstance(str2, str):return "%s is not a string" % str2return 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'])
8.1.添加Step,Report展示见下图。
9.issue和testcase定制详解
test_allure_issue_testcase.py
import 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" % str1if not isinstance(str2, str):return "%s is not a string" % str2return str1 + str2@allure.feature('test_module_01')@allure.story('test_story_01')@allure.severity('blocker')@allure.issue("")@allure.testcase("")def test_case():str1 = 'hello'str2 = '宏哥'assert str_add(str1, str2) == 'hello宏哥'if __name__ == '__main__':pytest.main(['-s', '-q', '--alluredir', './report'])
9.1.添加Issue和TestCase,Report展示见下图。
10.attach定制详解
test_allure_attach.py
import allureimport pytest#file = open('C:\\Users\\DELL\\Desktop\\duhong\\test.png', 'rb').read()#allure.attach('test_img', file, allure.attachment_type.PNG)PNG@pytest.fixturedef 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):passdef 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)
10.添加attach参数,Report展示见下图。