地址
allure版本的下载地址,同时按照python插件
pip3 install allure-pytest==2.7.0
allure主要用例
| @allure.epic() | epic描述 | 敏捷里面的概念,对用例或用例集进行描述分类 |
|---|---|---|
| @allure.feature() | 模块名称 | 与epic类似,只是比epic级别低 |
| @allure.story() | 用户故事 | 与epic类似,只是比feature级别低 |
| @allure.title(用例的标题) | 用例的标题 | 重命名html报告的用例名称 |
| @allure.testcase() | 测试用例的链接地址 | 与link类似 |
| @allure.issue() | 缺陷 | 与link类似 |
| @allure.description() | 用例描述 | 进行测试用例的描述 |
| @allure.step() | 操作步骤 | 进行测试用例的步骤 |
| @allure.severity() | 用例等级 | blocker,critical,normal,minor,trivial |
| @allure.link() | 链接 | 定义一个链接,在测试报告展现(推荐使用) |
| @allure.attachment() | 附件 | 报告添加附件 |
地址报告
Feature
标注主要的功能模块
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/xml'])
story
标注Feature功能模块下的分支功能
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_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'])
step
标注功能模块的具体步骤
# -*- coding: utf-8 -*-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 + str2if __name__ == '__main__':pytest.main(['-s', '-q', '--alluredir', './report/xml'])
allure.description;allure.title
import allureimport pytest@allure.title("用例标题0")@allure.description("这里是对test_0用例的一些详细说明")def test_0():pass@allure.title("用例标题1")def test_1():pass
allure.link()、allure.issue()、allure.testcase()
主要是为了将allure报告和测试管理系统集成,可以更快速的跳转到公司内部地址
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("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'])
allure动态参数
@allure.title和@allure.description都是装饰器,给测试用例提供标题和描述,其实allure还提供了在测试用例执行过程中动态指定标题和描述等标签的方法,如allure.dynamic.description allure.dynamic.title
dynamic的源码如下:
class Dynamic(object):@staticmethoddef title(test_title):plugin_manager.hook.add_title(test_title=test_title)@staticmethoddef description(test_description):plugin_manager.hook.add_description(test_description=test_description)@staticmethoddef description_html(test_description_html):plugin_manager.hook.add_description_html(test_description_html=test_description_html)@staticmethoddef label(label_type, *labels):plugin_manager.hook.add_label(label_type=label_type, labels=labels)@staticmethoddef severity(severity_level):Dynamic.label(LabelType.SEVERITY, severity_level)@staticmethoddef feature(*features):Dynamic.label(LabelType.FEATURE, *features)@staticmethoddef story(*stories):Dynamic.label(LabelType.STORY, *stories)@staticmethoddef tag(*tags):Dynamic.label(LabelType.TAG, *tags)@staticmethoddef link(url, link_type=LinkType.LINK, name=None):plugin_manager.hook.add_link(url=url, link_type=link_type, name=name)@staticmethoddef issue(url, name=None):Dynamic.link(url, link_type=LinkType.ISSUE, name=name)@staticmethoddef testcase(url, name=None):Dynamic.link(url, link_type=LinkType.TEST_CASE, name=name)
allure测试报告中为每条测试用动态添加属性allure.dynamic.feature()allure.dynamic.link()allure.dynamic.issue()allure.dynamic.testcase()allure.dynamic.story()allure.dynamic.title()allure.dynamic.description()举例:import allure@allure.title("装饰器标题")def test_1():print(123)allure.dynamic.title("动态标题")def test_1():"""动态设置描述"""print(123)allure.dynamic.description("动态描述")allure.dynamic.title("动态标题")
attach
在报告中增加附件:allure.attach(’arg1’,’arg2’,’arg3’):
arg1:是在报告中显示的附件名称
arg2:表示添加附件的内容
arg3:表示添加的类型(支持:HTML,JPG,PNG,JSON,OTHER,TEXTXML)
import allureimport pytestfile = open('../test.png', 'rb').read()@allure.feature('test_module_01')@allure.story('test_story_01')@allure.severity('blocker')@allure.attach('test_img', file, allure.attach_type.PNG)def test_case():str1 = 'hello'str2 = 'world'assert str_add(str1, str2) == 'helloworld'if __name__ == '__main__':pytest.main(['-s', '-q', '--alluredir', './report/xml'])
在conftest.py中添加如下代码使用截图
from selenium import webdriverfrom selenium.webdriver.chrome.options import Options@pytest.hookimpl(tryfirst=True, hookwrapper=True)def pytest_runtest_makereport(item, call):outcome = yieldreport = outcome.get_result()report.description = str(item.function.__doc__)report.nodeid = report.nodeid.encode("utf-8").decode("unicode_escape")if report.when == 'call' and report.failed:mode = 'a' if os.path.exists("failures") else "w"with open('failures',mode) as f:if 'tmpdir' in item.fixturenames:extra = ' (%s)' % item.funcagrs['tmpdir']else:extra = ''f.write(report.nodeid + extra +'\n')with allure.step('添加失败截图...'):allure.attach(driver.get_screenshot_as_png(),'失败截图',allure.attachment_type.PNG)@pytest.fixture(scope='session',autouse=True)def browser():global driverchrome_options = Options()chrome_options.add_argument('--no-sandbox')chrome_options.add_argument('--disable-dev-shm-usage')chrome_options.add_argument('--headless')driver = webdriver.Chrome(chrome_options=chrome_options)return driver
严重级别定义
Allure中对严重级别的定义:
1、 Blocker级别:中断缺陷(客户端程序无响应,无法执行下一步操作)
2、 Critical级别:临界缺陷( 功能点缺失)
3、 Normal级别:普通缺陷(数值计算错误)
4、 Minor级别:次要缺陷(界面错误与UI需求不符)
5、 Trivial级别:轻微缺陷(必输项无提示,或者提示不规范)
import allureimport pytest@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 == 0if __name__ == '__main__':pytest.main(['-s', '-q', '--alluredir', './report/xml'])
通过bug等级可以筛选用例进行测试
@allure.severity(allure.severity_level.TRIVIAL)来标记
执行时pytest test.py —allure-severities=trivial,blocker
