• conftest.py中的钩子函数无法运行


    关注 码龄 粉丝数 原力等级 -- 被采纳 被点赞 采纳率 m0_60568905 2024-05-05 17:07 采纳率: 0% 浏览 7 首页/ 编程语言 / conftest.py中的钩子函数无法运行 pythonselenium conftest.py中的钩子函数无法运行: # 用于收集用例的执行信息(成功/失败)记录在日志中 @pytest.hookimpl(hookwrapper=True) def pytest_runtest_markereport(item, call): """ 用框架执行用例过程中判断执行结果,记录到日志中 """ outcome = yield report = outcome.get_result() path = f"images/{time.time()}.png" if report.when == "call": o = report.outcome s = f"用例执行结果:【{report.outcome}】" if o == "failed": logger.error(s) elif o == "skip": logger.warning(s) else: logger.info(s) if "driver" in item.fixturenames: driver = item.funcargs['driver'] driver.get_screenshot_as_file(path) logger.info(f"页面截图:{path}") INTERNALERROR> pluggy._manager.PluginValidationError: unknown hook 'pytest_runtest_markereport' in plugin 展开全部 收起 写回答 好问题 0 提建议 追加酬金 关注问题 微信扫一扫 点击复制链接 分享 邀请回答 编辑 收藏 删除 结题 收藏 举报 追加酬金 (90%的用户在追加酬金后获得了解决方案) 当前问题酬金 ¥ 0 (可追加 ¥500) ¥ 15¥ 20¥ 50¥ 100¥ 200 支付方式 扫码支付 二维码出错 点击刷新 支付金额 15 元 提供问题酬金的用户不参与问题酬金结算和分配 支付即为同意 《付费问题酬金结算规则》 结题 再想想 删除 再等等

    5条回答 默认 最新

    • 专家-赤兔[在线] 全栈领域优质创作者 2024-05-05 17:07
      关注

      引自免费微信小程序:皆我百晓生

      看起来你遇到的问题是pytest插件验证错误,提示找不到名为pytest_runtest_markereport的钩子。这个钩子函数是用来处理测试标记报告的,但在较新的pytest版本中已经被弃用,取而代之的是pytest_runtest_protocol

      为了修复这个问题,你需要将你的钩子函数更改为pytest_runtest_protocol,并相应地调整内部逻辑。这是一个修改后的版本:

      import time
      import logging
      
      # 设置日志配置
      logging.basicConfig(filename='test_log.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
      
      def pytest_configure(config):
          config.addinivalue_line("markers", "screenshot: Take a screenshot on test failure")
      
      @pytest.hookimpl(hookwrapper=True)
      def pytest_runtest_protocol(item, nextitem):
          """
          用框架执行用例过程中判断执行结果,记录到日志中,并在失败时捕获屏幕截图。
          """
          outcome = yield
          report = outcome.get_result()
      
          # 获取测试标记
          marker = item.get_closest_marker('screenshot')
          
          path = f"images/{time.time()}.png"
          
          if report.when == "call":
              o = report.outcome
              s = f"用例执行结果:【{report.outcome}】"
      
              if o == "failed" and marker is not None:
                  logger.error(s)
                  if "driver" in item.fixturenames:
                      driver = item.funcargs['driver']
                      driver.get_screenshot_as_file(path)
      
                      logger.info(f"页面截图:{path}")
              elif o == "skip":
                  logger.warning(s)
              else:
                  logger.info(s)
      
      # 如果你的项目中没有包含driver fixture,你需要添加一个
      @pytest.fixture
      def driver(request):
          # 这里是一个示例,你需要根据实际情况创建浏览器实例
          browser = webdriver.Firefox()  # 或者其他浏览器驱动
          request.addfinalizer(browser.quit)  # 确保浏览器在测试后关闭
          return browser
      

      这段代码首先设置了日志的基本配置,然后定义了一个名为screenshot的测试标记。在pytest_runtest_protocol钩子函数中,我们检查了当前测试是否有screenshot标记,如果有且测试失败,那么就获取屏幕截图并保存。同时,确保在每个测试结束后正确关闭浏览器。

      注意,你需要安装适当的Webdriver(例如geckodriver对于Firefox,chromedriver对于Chrome等),并将它们添加到系统PATH中,以便webdriver.Firefox()能正常工作。

      如果你的项目中还没有包含driver fixture,你需要添加一个,如上面所示,这只是一个创建浏览器实例的示例,你需要根据实际使用的浏览器进行调整。

      展开全部

      评论 编辑记录
    • 笑面虎 2024-05-05 17:10
      关注

      以下内容由CHATGPT及笑面虎共同生成、有用望采纳:


      【问题概括】:这个问题的目的是解决在使用pytest框架进行自动化测试时,自定义的钩子函数pytest_runtest_markereport无法正常运行的问题。

      【问题出现原因】:根据提供的错误信息INTERNALERROR> pluggy._manager.PluginValidationError: unknown hook 'pytest_runtest_markereport' in plugin ,问题出现的原因是pytest无法识别pytest_runtest_markereport这个钩子函数。这通常是因为钩子函数的名称拼写错误或者没有正确注册。

      【问题解决方案】:要解决这个问题,需要确保钩子函数的名称正确,并且已经被pytest框架正确注册。根据pytest文档,正确的钩子函数名称应该是pytest_runtest_makereport,而不是pytest_runtest_markereport

      【提供代码】:以下是修改后的conftest.py中的钩子函数代码:

      # 用于收集用例的执行信息(成功/失败)记录在日志中
      import time
      import pytest
      
      @pytest.hookimpl(hookwrapper=True)
      def pytest_runtest_makereport(item, call):
          """
          用框架执行用例过程中判断执行结果,记录到日志中
          """
          outcome = yield
          report = outcome.get_result()
          path = f"images/{time.time()}.png"  
          if report.when == "call":
              o = report.outcome
              s = f"用例执行结果:【{report.outcome}】"
      
              if o == "failed":
                  print(s)  # 使用print而不是logger,因为logger可能尚未配置
              elif o == "skip":
                  print(s)
              else:
                  print(s)
      
              if "driver" in item.fixturenames:
                  driver = item.funcargs['driver']
                  driver.get_screenshot_as_file(path)
                  print(f"页面截图:{path}")
      

      【代码运行方式】:要运行这段代码,你需要有一个pytest测试环境,并且已经安装了pytest插件。你需要将这段代码保存在项目的conftest.py文件中,然后运行pytest命令来执行测试。

      【代码预期运行结果】:当你运行pytest命令执行测试用例时,如果测试用例失败,控制台将打印出用例执行结果的信息,并且会保存一个页面截图到指定的路径下。

      【推荐相关链接】:

      展开全部

      评论 编辑记录
    • GISer Liu 2024-05-05 17:11
      关注

      该回答引用自GPT-3.5,由博主GISer Liu编写:

      问题分析:
      根据提供的问题描述,报错信息显示了一个未知的钩子函数 pytest_runtest_markereport,这表明pytest无法识别这个钩子函数。通常情况下,pytest能够自动识别 pytest_runtest_protocol
      这个钩子,但如果你想要自定义钩子,需要遵循pytest的规则来定义和注册。
      解决方案:

      1. 正确注册钩子函数 :确保正确地注册了自定义的钩子函数,以使pytest能够识别。可以通过在 conftest.py 中添加 pytest_runtest_protocol 钩子来实现。
      2. 钩子函数命名规范 :确保自定义的钩子函数名符合pytest的命名规范,通常是以 pytest_ 开头,后接要监听的事件名称。
      3. 检查导入 :确保在 conftest.py 文件中正确导入了pytest模块,即 import pytest
        下面是修改后的 conftest.py 文件示例:
        python
        Copy code
        import pytest
        import time
        import logging
        logger = logging.getLogger(__name__)
        # 用于收集用例的执行信息(成功/失败)记录在日志中
        @pytest.hookimpl(hookwrapper=True)
        def pytest_runtest_protocol(item, nextitem):
        """用框架执行用例过程中判断执行结果,记录到日志中"""
        outcome = yield
        report = outcome.get_result()
        path = f"images/{time.time()}.png"
        if report.when == "call":
        o = report.outcome
        s = f"用例执行结果:【{report.outcome}】"
        if o == "failed":
        logger.error(s)
        elif o == "skip":
        logger.warning(s)
        else:
        logger.info(s)
        if "driver" in item.fixturenames:
        driver = item.funcargs['driver']
        driver.get_screenshot_as_file(path)
        logger.info(f"页面截图:{path}")
        
        修改后的代码中,将 pytest_runtest_markereport 改为了 pytest_runtest_protocol,并且添加了正确的导入语句 import pytest
        这样修改后,应该就能够正确识别并执行自定义的钩子函数了。

      如果该回答解决了您的问题,请采纳!如果没有,请私信联系或评论您的疑惑

      展开全部

      评论
    • Kwan的解忧杂货铺 Java领域优质创作者 2024-05-06 14:11
      关注

      下午好🌅🌅🌅
      本答案参考ChatGPT-3.5

      问题描述:

      在 conftest.py 文件中的 pytest_runtest_markereport 钩子函数无法运行。

      报错信息如下:

      INTERNALERROR> pluggy._manager.PluginValidationError: unknown hook 'pytest_runtest_markereport' in plugin 
      

      问题原因:

      pytest_runtest_markereport 钩子函数拼写错误。

      解决方案:

      1. 检查钩子函数拼写是否正确,确保在 conftest.py 文件中的钩子函数拼写无误。

        正确写法应为 pytest_runtest_makereport。

      2. 确认 pytest 版本是否过低,最好保持 pytest 版本最新,避免一些已解决的问题。

      3. 确认已正确安装 pytest-html 插件,因为 pytest_runtest_makereport 钩子函数是在此插件中定义的。

      4. 如果按照上述方法仍然不能解决问题,可以将 pytest-html 插件卸载并重新安装,然后再次运行测试。

      参考代码:

      将原来的 conftest.py 文件中的 pytest_runtest_markereport 钩子函数修改为 pytest_runtest_makereport,代码如下:

      # 用于收集用例的执行信息(成功/失败)记录在日志中
      @pytest.hookimpl(hookwrapper=True)
      def pytest_runtest_makereport(item, call):
          """
          用框架执行用例过程中判断执行结果,记录到日志中
          """
          outcome = yield
          report = outcome.get_result()
          path = f"images/{time.time()}.png"  
          if report.when == "call":
      
              o = report.outcome
              s = f"用例执行结果:【{report.outcome}】"
      
              if o == "failed":
                  logger.error(s)
              elif o == "skip":
                  logger.warning(s)
              else:
                  logger.info(s)
      
              if "driver" in item.fixturenames:
                  driver = item.funcargs['driver']
                  driver.get_screenshot_as_file(path)
      
                  logger.info(f"页面截图:{path}")
      

      这样就可以解决 pytest_runtest_makereport 钩子函数无法运行的问题了。

      展开全部

      评论
    • DTcode7 2024-05-18 18:01
      关注

      答案引用自chatgpt生成,望对题主有所帮助/启发;若有帮助,还望采纳!

      conftest.py 是用于存放 pytest 测试框架的钩子函数(hook functions)的文件。钩子函数是在特定事件发生时自动执行的函数,例如在测试开始前或结束后执行一些操作。

      如果你发现 conftest.py 中的钩子函数无法运行,可能有以下几种原因:

      1. 文件位置不正确:确保 conftest.py 文件位于测试目录的正确位置。pytest 会自动搜索当前目录及其子目录中的 conftest.py 文件。如果文件位置不正确,pytest 将无法找到并执行其中的钩子函数。

      2. 语法错误:检查 conftest.py 文件中的代码是否存在语法错误。任何语法错误都会导致钩子函数无法正常执行。请确保代码没有拼写错误、缩进问题或其他常见的语法错误。

      3. 导入错误:如果 conftest.py 文件中使用了其他模块或库,确保这些模块或库已经正确安装,并且可以在当前环境中导入。如果导入错误,钩子函数将无法正常运行。

      4. 钩子函数名称不正确:确保你在 conftest.py 中定义的钩子函数名称与 pytest 文档中列出的名称一致。不同的钩子函数有不同的名称和作用,如果名称不正确,pytest 将无法识别并执行相应的钩子函数。

      下面是一个示例的 conftest.py 文件,其中包含了一个简单的钩子函数:

      # conftest.py
      
      import pytest
      
      def pytest_sessionstart(session):
          print("Session started")
      
      def pytest_sessionfinish(session, exitstatus):
          print("Session finished")
      

      在这个例子中,我们定义了两个钩子函数:pytest_sessionstartpytest_sessionfinish。当测试会话开始时,pytest_sessionstart 函数会被调用并打印 "Session started";当测试会话结束时,pytest_sessionfinish 函数会被调用并打印 "Session finished"。

      请注意,这只是一个简单的示例,实际的 conftest.py 文件可能会包含更复杂的逻辑和多个钩子函数。

      希望以上信息能够帮助你解决 conftest.py 中的钩子函数无法运行的问题!如有更多疑问,请随时提问。

      展开全部

      评论
    编辑
    预览

    报告相同问题?

  • 相关阅读:
    秋招面经第五弹:一家上市小公司二面-大数据开发工程师
    Golang vs Rust 为后端选择哪种语言?
    JVM虚拟机栈:局部变量表
    【k8s管理操作】
    Flutter:WebSocket封装-实现心跳、重连机制
    C++头文件 库函数 以及 vector相关问题
    XUI - 一个简洁而优雅的Android原生UI框架
    杭州亚运会实现核心系统100%上云、云上转播7200+小时
    巧用TXT文档导入所有快递单号查询物流详情
    docker资源控制
  • 原文地址:https://ask.csdn.net/questions/8098974