allure测试报告如何使用

本文详细介绍了如何安装和配置Allure测试报告,包括在pytest中集成Allure,将测试结果从JSON转换为HTML,以及利用Allure的特性如步骤展示、截图和优先级标注来丰富测试报告的内容。通过添加@allure.step装饰器记录测试步骤,并使用allure.attach进行截图,有助于提升测试报告的可读性和问题定位效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

allure测试报告如何使用
  • 安装插件 pip install allure-pytest

  • 安装将allure测试报告从json格式转换成html格式

    • 下载地址:https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/
    • allure-commandline-2.13.8.zip
    • 将文件进行解压
    • 解压完之后找到bin目录,我的目录如下:H:\allure-html\allure-2.13.8\bin
    • 将目录进行配置在path环境变量中
  • 在pytest.ini配置文件中

    # 添加
    --allure report
    
    # 具体如下
    [pytest]
    addopts = -s --allure report
    testpaths = ./test01
    pythonfiles = Test_*
    pythonclasses = Test*
    pythonfunction = test_*
    
    
  • 配置完以上两项后,即可在命令行执行测试操作

  • 这样会生成一个json格式的测试报告

  • 将json格式的测试报告进行转换。如下

    # 命令如下
    allure generate report/ -o report/html --clean
    
    # 参数解析
    report/       json格式测试报告的目录
    report/html   在report目录下生成一个html目录文件,这个目录文件包含着html测试报告
    
    
allure测试报告的附加功能
  • 展现测试报告步骤

    1. 在操作层中的方法上添加一个装饰器,@allure.step(title=“标题”)

      # page/login_page.py
      class LoginHandle(BaseHandle):
      def __init__(self):
      self.login_page = LoginPage()
      @allure.step(title="输入手机号")
      def input_mobile(self, mobile):
      self.input_text(self.login_page.find_mobile(), mobile)
      @allure.step(title="输入验证码")
      def input_code(self, code):
      self.input_text(self.login_page.find_code(), code)
      @allure.step(title="点击登录按钮")
      def click_login_btn(self):
      self.login_page.find_login_btn().click()
      
  • 截图

    • 当需要截取测试中的结果时使用

      allure.attach(driver.get_screenshot_as_png(), "截图", allure.attachment_type.PNG)
      
      # 具体事例
      # script/test_login.py
      @pytest.mark.parametrize("mobile,code,username", build_data())
      def test_login(self, mobile, code, username):
      logging.info("mobile={} code={} username={}".format(mobile, code, username))
      # 登录
      self.login_proxy.login(mobile, code)
      # 截图
      allure.attach(self.driver.get_screenshot_as_png(), "截图", allure.attachment_type.P
      NG)
      # 断言
      is_exist = utils.exist_text(DriverUtil.get_mp_driver(), username)
      assert is_exist
      
  • 优先级

    • 在测试报告中可以更加客观的看出测试结果数据优先级的情况

    • 具体使用如下:

      @allure.severity(allure.severity_level.BLOCKER)
      
      # 具体事例
      # script/test_login.py
      @allure.severity(allure.severity_level.BLOCKER)
      @pytest.mark.parametrize("mobile,code,username", build_data())
      def test_login(self, mobile, code, username):
      logging.info("mobile={} code={} username={}".format(mobile, code, username))
      # 登录
      self.login_proxy.login(mobile, code)
      # 截图
      allure.attach(self.driver.get_screenshot_as_png(), "截图", allure.attachment_type.P
      NG)
      # 断言
      is_exist = utils.exist_text(DriverUtil.get_mp_driver(), username)
      assert is_exist
      
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值