Pytest+Allure2.X定制测试报告
Allure常用的注解
Feature: 标注主要功能模块
Story: 标注Features功能模块下的分支功能
Severity: 标注测试用例的重要级别
Step: 标注测试用例的重要步骤
Issue和TestCase: 标注Issue、Case,可加入URL
1、Features定制详解
执行命令:
1.pytest -s -q
2.pytest -s -q --alluredir allure-xml
3.allure generate allure-xml -o allure-report --clean
参考代码:
# -*- coding: utf-8 -*- #@Time : 2020/6/21 17:21 #@Auth : Ann #@File :test_case.py #@IDE :PyCharm import pytest import allure @allure.feature("test_module_01") def test_case_01(): assert 0 @allure.feature("test_module_02") def test_case_02(): ''' 用例描述 :return: ''' assert 0==0
2、Story定制详解
参考代码:
# -*- coding: utf-8 -*- #@Time : 2020/6/21 23:19 #@Auth : Ann #@File :test_story.py #@IDE :PyCharm #导入模块 import allure import pytest @allure.feature("test_module_01") @allure.story("test_story_01") def test_case_01(): ''' 用例描述:Test case 01 :return: ''' assert 0 @allure.feature("test_module_02") @allure.story("test_story_02") def test_case_02(): ''' 用例描述:Test case 02 :return: ''' assert 0==0
(3)添加story,Report展示见下图。
3、用例标题和用例描述定制详解
参考代码:
# -*- coding: utf-8 -*- #@Time : 2020/6/21 23:33 #@Auth : Ann #@File :test_title.py #@IDE :PyCharm #导入模块 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 :return: ''' #注释为用例 assert 0
效果:
4 、Severity定制详解
Allure中对严重级别的定义:
1、 Blocker级别:中断缺陷(客户端程序无响应,无法执行下一步操作)
2、 Critical级别:临界缺陷( 功能点缺失)
3、 Normal级别:普通缺陷(数值计算错误)
4、 Minor级别:次要缺陷(界面错误与UI需求不符)
5、 Trivial级别:轻微缺陷(必输项无提示,或者提示不规范)
参考代码:
# -*- coding: utf-8 -*- #@Time : 2020/6/21 23:48 #@Auth : Ann #@File :test_severity.py #@IDE :PyCharm import allure import pytest @allure.feature("test_module_01") @allure.story("test_story_01") @allure.severity("blocker") def test_case_01(): ''' 用例描述 Test case 01 :return: ''' assert 0 @allure.feature("test_module_01") @allure.story("test_story_01") @allure.severity("critical") def test_case_02(): ''' 用例描述:Test case 02 :return: ''' assert 0==0 @allure.feature("test_module_01") @allure.story("test_story_02") @allure.severity("normal") def test_case_03(): ''' 用例描述 Test case 03 :return: ''' assert 0 @allure.feature("test_module_01") @allure.story("test_story_02") @allure.severity("minor") def test_case_04(): ''' 用例描述:Test case 04 :return: ''' assert 0 == 0
效果:
5、Step定制详解
参考代码:
# -*- coding: utf-8 -*- #@Time : 2020/6/22 21:22 #@Auth : Ann #@File :Step定制详解.py #@IDE :PyCharm #导入模块 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') def test_case(): str1 = 'hello' str2 = 'word' assert str_add(str1, str2) == 'helloword'
添加Step,Report展示见下图。
6、Issue和TestCase定制详解
参考代码:
# -*- coding: utf-8 -*- #@Time : 2020/6/22 21:22 #@Auth : Ann #@File :Step定制详解.py #@IDE :PyCharm #导入模块 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 = 'word' assert str_add(str1, str2) == 'helloword'
添加Issue和TestCase,Report展示见下图。
7、attach定制详解
(1)代码实现
# -*- coding: utf-8 -*- #@Time : 2020/6/22 21:50 #@Auth : Ann #@File :test_attach.py #@IDE :PyCharm #3.导入模块 import allure import pytest @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('D:\PycharmProjects\AllureTest\image\kebi.jpg', attachment_type=allure.attachment_type.PNG) allure.attach('<head></head><body> a page </body>', 'Attach with HTML type', allure.attachment_type.HTML)