【问题标题】:Collecting and Reporting pytest Results收集和报告 pytest 结果
【发布时间】:2015-06-18 07:54:25
【问题描述】:

我正在通过 pytest 进行一些 Selenium 测试。下一步是开始做一些报告。我想写一些东西来让我运行测试、收集结果并发送电子邮件。到目前为止,我发现的最接近的是将测试结果写入结果日志,然后使用插件检查存在状态并从那里发送电子邮件。这可行,但有点麻烦,我希望有一种更优雅的方式来做到这一点。虽然整体 pytest 文档很棒,但插件文档却很差——我什至在任何地方都找不到 pytest_sessionfinish,尽管它似乎可以工作。

import pytest

class MyPlugin:
    def pytest_sessionfinish(self, exitstatus):
        if exitstatus == 0:
            #Send success email
            pass
        else: 
            #Read output.txt
            #Add output.txt to email body
            #Send email
            pass

pytest.main("--resultlog=output.txt", plugins=[MyPlugin()])

问:从 pytest 运行和收集结果的最佳方式是什么?


【问题讨论】:

    标签: python selenium-webdriver pytest


    【解决方案1】:

    如果您想实现报告,pytest 有很多现成的简单解决方案。以下是其中的一些:

    【讨论】:

      【解决方案2】:

      另一种创建报告的方法是首先将结果收集为字典或数据框,然后用它做任何你喜欢的事情(写一个 csv 等)。

      如果你想走这条路,你可以使用pytest-harvest。只需安装它,就可以直接使用预定义的fixture:

      import pytest
      import time
      
      @pytest.mark.parametrize('p', ['world', 'self'], ids=str)
      def test_foo(p):
          """
          A dummy test, parametrized so that it is executed twice
          """
          print('\n   hello, ' + p + ' !')
          time.sleep(len(p) / 10)
      
      def test_synthesis(module_results_df):
          """
          Shows that the `module_results_df` fixture already contains what you need
          """
          # drop the 'pytest_obj' column
          module_results_df.drop('pytest_obj', axis=1, inplace=True)
      
          print("\n   `module_results_df` dataframe:\n")
          print(module_results_df)
      

      产量

      >>> pytest -s -v
      
      ============================= test session starts =============================
      ...
      collecting ... collected 3 items
      test_basic.py::test_foo[world] 
         hello, world !
      PASSED
      test_basic.py::test_foo[self] 
         hello, self !
      PASSED
      test_basic.py::test_synthesis 
         `module_results_df` dataframe:
      
                       status  duration_ms      p
      test_id                                    
      test_foo[world]  passed   500.028610  world
      test_foo[self]   passed   400.022745   self
      PASSED
      
      ========================== 3 passed in 0.05 seconds ===========================
      

      您还可以从 'dict' 夹具开始,其中包含有关设置/拆卸时间的更多详细信息,并使用提供的辅助方法将其转换为数据帧。 有关详细信息,请参阅文档。

      最后,如果你还想使用参数、夹具、步骤……你不妨看看这个datascience benchmark example

      顺便说一句,我是作者;)

      【讨论】:

      • 我对使用这个库很感兴趣。我在理解如何使用它时遇到了一些麻烦。我已经安装但无法使用,例如 module_results_dct 没有导入东西。这些例子表明这应该是不必要的。我做错了什么。很抱歉以这种方式发信息,但我试图私信给你,但没有成功。
      • @AndrewFalanga 的问题顺便解决了:github.com/smarie/python-pytest-harvest/issues/37
      【解决方案3】:

      安装pytest-html,然后使用--html=pytest_report.html 选项运行测试。

      【讨论】:

      • 当您使用它时,使用“--self-contained-html”选项生成一个 HTML 文件,您可以将其加载到浏览器中查看结果。
      【解决方案4】:

      生成结果报告的一种简单方法是在运行测试时使用 pytest 选项--junitxml。 pytest 会生成 JUnit 格式的测试报告。

      由于 JUnit 被广泛使用,很容易找到工具来解析报告并生成一些好看的输出,例如 HTML 报告。据我所知,Jenkins 上有一些插件可以很好地解析 JUnit 报告并提供不错的报告。

      访问 https://pytest.org/latest/usage.html 并参考“创建 JUnitXML 格式文件”部分。

      不仅如此,当您有权访问 pytest 对象请求或配置时,pytest 还提供了一种扩展 JUnit XML 报告的方法:

      if hasattr(request.config, "_xml"):
          request.config._xml.add_custom_property(name, value)
      

      如果在测试用例中,pytest 提供了一个夹具来做到这一点:

      def test_function(record_xml_property):
          record_xml_property("key", "value")
          assert 0
      

      这将为 JUnit XML 报告添加一个自定义属性。

      【讨论】:

      • 您如何/在哪里访问request 对象?你是在pytest_runtest_logreport 做的吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-11
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      相关资源
      最近更新 更多