Pytest单元测试框架-allure测试报告

发表于:2021-7-22 09:50

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:柒意    来源:掘金

  pytest与allure中间的火花
  1.安装配置allure:github.com/allure-fram…
  下载后解压文件并添加到对应操作系统的环境变量中。
  2.安装allure-pytest插件:pip install allure-pytest
  3.添加allure测试结果报告存放路径pytest --alluredir=./report/allure_report_01(此方法会运行测试文件,并得到结果汇总到报告中,你还需执行下一步)。
  4.查看实际的报告(在浏览器显示,这样会在本地启用个web服务):allure serve ./report/allure_report_01
  5.生成测试报告(每次生成前清空测试报告目录):allure generate ./report/allure_report_01/ -o ./report01/ --clean
  6.注意事项
  如果直接执行pytest --alluredir=report,将会直接在当前目录下新建一个report的目录,并会运行pytest测试。
  定制allure报告
  1.给allure报告中的测试方法添加描述: 只需要在测试函数/方法下加上"""描述的内容"""
  def test_sum(self):
  """ 这部分内容将显示在 allure报告中"""
      # assert进行断言
      assert 1 == 2
  2.测试步骤描述:在测试方法之前添加@allure.step('步骤描述')内容。
  #!/usr/bin/env/python3
  # -*- coding:utf-8 -*-
  """
  @project: pytest_study
  @author: zy7y
  @file: test_09.py
  @ide: PyCharm
  @time: 2020/7/27
  """
  import allure
  import pytest
  @allure.step('第一步')
  def passing_step():
      pass
  @allure.step('第二步')
  def step_with_nested_steps():
      nested_step()
  @allure.step('第二.1步')
  def nested_step():
      nested_step_with_arguments(1, 'abc')
  @allure.step('第二.2步')
  def nested_step_with_arguments(arg1, arg2):
      pass
  def test_with_imported_step():
      passing_step()
  def test_with_nested_steps():
      passing_step()
      step_with_nested_steps()

  步骤可以有一个描述行,该描述行支持传递的位置参数和关键字参数的占位符。关键字参数的默认参数也将被捕获。
  import allure
  @allure.step('测试步骤标题中的占位符, 第一个参数: "{0}", 第二个参数keyword: "{key}"')
  def step_with_title_placeholders(arg1, key=None):
      pass
  def test_steps_with_placeholders():
      step_with_title_placeholders(1, key='something')
      step_with_title_placeholders(2)
      step_with_title_placeholders(3, 'anything')
  3.ids测试用例标题: 每条用例ids关键字参数的内容将被填充到allure报告中。
  @pytest.mark.parametrize('username, password, expect',
                           [('admin', '123456', '登录成功'),
                            ('admin', '111111', '密码错误')], ids=["正常登录测试用例标题", "密码错误登录测试用例"])
  def test_login(username, password, expect):
      if username == 'admin' and password == '123456':
          assert expect == '登录成功'
      else:
          assert expect == '密码错误'

  4.向allure某个测试用例/函数/方法中添加附件:arllure.attcah(body, name, attachment_type, extension)或者添加本地附件allure.attach.file(source, name, attachment_type, extension)
  · body - 需要写入的文件内容
  · name - 文件名称
  · attachment_type - 文件类型 allure.attachment_type
  · extension - 文件后缀名
  · source - 文件所在路径
  import allure
  import pytest
  @pytest.fixture
  def attach_file_in_module_scope_fixture_with_finalizer(request):
      # 添加一个txt文件,文件名称为 file_name, 文件里面的内容为:allure文本附件内容
      allure.attach('allure文本附件内容', 'file_name', allure.attachment_type.TEXT)
  def test_multiple_attachments(attach_file_in_module_scope_fixture_with_finalizer):
      # 本地上传一个图片文件给allure,
      allure.attach.file('./Snipaste_2020-07-27_23-29-48.png', name='allure专用', attachment_type=allure.attachment_type.PNG)
      allure.attach('<head></head><body> a page </body>', 'html附件', allure.attachment_type.HTML)

  5.定制测试标题(测试函数在allure中的显示方式)内容:@allure.title()
  @allure.title('测试标题:测试打开blog')
  @allure.step('3. 点击回车')
  def test_open_blog(browse_driver):
      browse_driver.find_element_by_id('su').click()
      #allure.dynamic.title('在成功完成测试后,标题被替换为这一行。')

  6.添加链接:@allure.link或者@allure.issue或者@allure.testcase
  # 显示链接
  @allure.link('https://www.baidu.com/')
  def test_with_link():
      pass
  @allure.link('https://www.youtube.com/watch?v=Su5p2TqZxKU', name='allure.link 实现的,传递了一个url,一个name')
  def test_with_named_link():
      pass
  # 可以用来放禅道bug地址,它的图标是个虫子挺好的
  @allure.issue('https://www.baidu.com', '这就是显示图标,点击可以访问链接的 allure.issue')
  def test_with_issue_link():
      pass
  # 链接栏下方显示 测试用例标题, 点击实现跳转
  @allure.testcase(TEST_CASE_LINK, '测试用例标题')
  def test_with_testcase_link():
      pass

  7.按标签分组执行:@allure.feature and @allure.story
  @allure.story('epic_1')
  def test_with_epic_1():
      pass
  @allure.story('story_1')
  def test_with_story_1():
      pass
  @allure.story('story_2')
  def test_with_story_2():
      pass
  @allure.feature('feature_2')
  @allure.story('story_2')
  def test_with_story_2_and_feature_2():
      pass
  # pytest test_09.py --allure-stories story_1,story_2 只执行 含这两个标签的
  # pytest test_09.py --allure-features feature2 --allure-stories story2
  8.按严重度(优先级)分组执行:@allure.severity
  # test.py
  import allure
  @allure.severity('blocker')
  def test_with_no_severity_label():
      pass
  @allure.severity(allure.severity_level.TRIVIAL)
  def test_with_trivial_severity():
      pass
  @allure.severity(allure.severity_level.NORMAL)
  def test_with_normal_severity():
      pass
  @allure.severity(allure.severity_level.NORMAL)
  class TestClassWithNormalSeverity(object):
      def test_inside_the_normal_severity_test_class(self):
          pass
      @allure.severity(allure.severity_level.CRITICAL)
      def test_inside_the_normal_severity_test_class_with_overriding_critical_severity(self):
          pass
   
  # 将执行严重性为NORML、CRITICAL的方法/类/函数
  # pytest tests.py --allure-severities normal,critical

      本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号