地址

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

标注主要的功能模块

  1. import allure
  2. import pytest
  3. @allure.feature('test_module_01')
  4. def test_case_01():
  5. """
  6. 用例描述:Test case 01
  7. """
  8. assert 0
  9. @allure.feature('test_module_02')
  10. def test_case_02():
  11. """
  12. 用例描述:Test case 02
  13. """
  14. assert 0 == 0
  15. if __name__ == '__main__':
  16. pytest.main(['-s', '-q', '--alluredir', './report/xml'])

story

标注Feature功能模块下的分支功能

  1. import allure
  2. import pytest
  3. @allure.feature('test_module_01')
  4. @allure.story('test_story_01')
  5. def test_case_01():
  6. """
  7. 用例描述:Test case 01
  8. """
  9. assert 0
  10. @allure.feature('test_module_01')
  11. @allure.story('test_story_02')
  12. def test_case_02():
  13. """
  14. 用例描述:Test case 02
  15. """
  16. assert 0 == 0
  17. if __name__ == '__main__':
  18. pytest.main(['-s', '-q', '--alluredir', './report/xml'])

step

标注功能模块的具体步骤

  1. # -*- coding: utf-8 -*-
  2. import allure
  3. import pytest
  4. @allure.step("字符串相加:{0},{1}")
  5. # 测试步骤,可通过format机制自动获取函数参数
  6. def str_add(str1, str2):
  7. if not isinstance(str1, str):
  8. return "%s is not a string" % str1
  9. if not isinstance(str2, str):
  10. return "%s is not a string" % str2
  11. return str1 + str2
  12. if __name__ == '__main__':
  13. pytest.main(['-s', '-q', '--alluredir', './report/xml'])

allure.description;allure.title

  1. import allure
  2. import pytest
  3. @allure.title("用例标题0")
  4. @allure.description("这里是对test_0用例的一些详细说明")
  5. def test_0():
  6. pass
  7. @allure.title("用例标题1")
  8. def test_1():
  9. pass

allure.link()、allure.issue()、allure.testcase()

主要是为了将allure报告和测试管理系统集成,可以更快速的跳转到公司内部地址

  1. import allure
  2. import pytest
  3. @allure.step("字符串相加:{0},{1}") # 测试步骤,可通过format机制自动获取函数参数
  4. def str_add(str1, str2):
  5. print('hello')
  6. if not isinstance(str1, str):
  7. return "%s is not a string" % str1
  8. if not isinstance(str2, str):
  9. return "%s is not a string" % str2
  10. return str1 + str2
  11. @allure.feature('test_module_01')
  12. @allure.story('test_story_01')
  13. @allure.severity('blocker')
  14. @allure.issue("http://www.baidu.com")
  15. @allure.testcase("http://www.testlink.com")
  16. def test_case():
  17. str1 = 'hello'
  18. str2 = 'world'
  19. assert str_add(str1, str2) == 'helloworld'
  20. if __name__ == '__main__':
  21. pytest.main(['-s', '-q', '--alluredir', './report/xml'])

allure动态参数

@allure.title和@allure.description都是装饰器,给测试用例提供标题和描述,其实allure还提供了在测试用例执行过程中动态指定标题和描述等标签的方法,如allure.dynamic.description allure.dynamic.title
dynamic的源码如下:

  1. class Dynamic(object):
  2. @staticmethod
  3. def title(test_title):
  4. plugin_manager.hook.add_title(test_title=test_title)
  5. @staticmethod
  6. def description(test_description):
  7. plugin_manager.hook.add_description(test_description=test_description)
  8. @staticmethod
  9. def description_html(test_description_html):
  10. plugin_manager.hook.add_description_html(test_description_html=test_description_html)
  11. @staticmethod
  12. def label(label_type, *labels):
  13. plugin_manager.hook.add_label(label_type=label_type, labels=labels)
  14. @staticmethod
  15. def severity(severity_level):
  16. Dynamic.label(LabelType.SEVERITY, severity_level)
  17. @staticmethod
  18. def feature(*features):
  19. Dynamic.label(LabelType.FEATURE, *features)
  20. @staticmethod
  21. def story(*stories):
  22. Dynamic.label(LabelType.STORY, *stories)
  23. @staticmethod
  24. def tag(*tags):
  25. Dynamic.label(LabelType.TAG, *tags)
  26. @staticmethod
  27. def link(url, link_type=LinkType.LINK, name=None):
  28. plugin_manager.hook.add_link(url=url, link_type=link_type, name=name)
  29. @staticmethod
  30. def issue(url, name=None):
  31. Dynamic.link(url, link_type=LinkType.ISSUE, name=name)
  32. @staticmethod
  33. def testcase(url, name=None):
  34. Dynamic.link(url, link_type=LinkType.TEST_CASE, name=name)
  1. allure测试报告中为每条测试用动态添加属性
  2. allure.dynamic.feature()
  3. allure.dynamic.link()
  4. allure.dynamic.issue()
  5. allure.dynamic.testcase()
  6. allure.dynamic.story()
  7. allure.dynamic.title()
  8. allure.dynamic.description()
  9. 举例:
  10. import allure
  11. @allure.title("装饰器标题")
  12. def test_1():
  13. print(123)
  14. allure.dynamic.title("动态标题")
  15. def test_1():
  16. """
  17. 动态设置描述
  18. """
  19. print(123)
  20. allure.dynamic.description("动态描述")
  21. allure.dynamic.title("动态标题")

attach

在报告中增加附件:allure.attach(’arg1’,’arg2’,’arg3’):
arg1:是在报告中显示的附件名称
arg2:表示添加附件的内容
arg3:表示添加的类型(支持:HTML,JPG,PNG,JSON,OTHER,TEXTXML)

  1. import allure
  2. import pytest
  3. file = open('../test.png', 'rb').read()
  4. @allure.feature('test_module_01')
  5. @allure.story('test_story_01')
  6. @allure.severity('blocker')
  7. @allure.attach('test_img', file, allure.attach_type.PNG)
  8. def test_case():
  9. str1 = 'hello'
  10. str2 = 'world'
  11. assert str_add(str1, str2) == 'helloworld'
  12. if __name__ == '__main__':
  13. pytest.main(['-s', '-q', '--alluredir', './report/xml'])

在conftest.py中添加如下代码使用截图

  1. from selenium import webdriver
  2. from selenium.webdriver.chrome.options import Options
  3. @pytest.hookimpl(tryfirst=True, hookwrapper=True)
  4. def pytest_runtest_makereport(item, call):
  5. outcome = yield
  6. report = outcome.get_result()
  7. report.description = str(item.function.__doc__)
  8. report.nodeid = report.nodeid.encode("utf-8").decode("unicode_escape")
  9. if report.when == 'call' and report.failed:
  10. mode = 'a' if os.path.exists("failures") else "w"
  11. with open('failures',mode) as f:
  12. if 'tmpdir' in item.fixturenames:
  13. extra = ' (%s)' % item.funcagrs['tmpdir']
  14. else:
  15. extra = ''
  16. f.write(report.nodeid + extra +'\n')
  17. with allure.step('添加失败截图...'):
  18. allure.attach(driver.get_screenshot_as_png(),'失败截图',allure.attachment_type.PNG)
  19. @pytest.fixture(scope='session',autouse=True)
  20. def browser():
  21. global driver
  22. chrome_options = Options()
  23. chrome_options.add_argument('--no-sandbox')
  24. chrome_options.add_argument('--disable-dev-shm-usage')
  25. chrome_options.add_argument('--headless')
  26. driver = webdriver.Chrome(chrome_options=chrome_options)
  27. return driver

严重级别定义

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

  1. import allure
  2. import pytest
  3. @allure.severity('blocker')
  4. def test_case_01():
  5. """
  6. 用例描述:Test case 01
  7. """
  8. assert 0
  9. @allure.feature('test_module_01')
  10. @allure.story('test_story_01')
  11. @allure.severity('critical')
  12. def test_case_02():
  13. """
  14. 用例描述:Test case 02
  15. """
  16. assert 0 == 0
  17. if __name__ == '__main__':
  18. pytest.main(['-s', '-q', '--alluredir', './report/xml'])

通过bug等级可以筛选用例进行测试
@allure.severity(allure.severity_level.TRIVIAL)来标记
执行时pytest test.py —allure-severities=trivial,blocker