【问题标题】:How to write test steps to the generated html report with pytest-html?如何使用 pytest-html 将测试步骤写入生成的 html 报告?
【发布时间】:2019-01-03 08:40:17
【问题描述】:

我找不到将字符串添加到代表我的测试步骤的 HTML 报告的方法。

我看到https://pypi.org/project/pytest-html/ 我应该尝试:

extra.text('Add some simple Text')

但是好像不行。

from pytest_html import extras

class FirstFeatureTests:

    def setup_class(self):
        print("\n------------ in setup_class ------------")

    def teardown_class(self):
        print("------------ in teardown_class ------------")

    @mark.example
    def test_1_first_feature(self):
        print("starting test !!!!!")
        extras.text("step 1: ")
        extras.text('step 2: ')
        assert True

我希望每个测试都包含以字符串表示的测试步骤,并带有信息性描述。

有什么办法吗?

【问题讨论】:

  • extras.text() 不会在报告中添加任何内容,只会准备要添加的文本。您需要在pytest_runtest_makereport钩子的自定义impl中使用report.extra.append(),如文档中的示例所示。
  • 谢谢,它成功了。我还有一个问题,如何将 costum 消息从测试方法传递给 pytest_runtest_makereport 钩子?
  • 我可能会使用会话对象,例如def test_spam(request): request.session.foo = 'bar'def pytest_runtest_makereport(item, call): foo = item.session.foo

标签: python pytest pytest-html


【解决方案1】:

在这种情况下,我建议您尝试 allure 而不是 pytest-html。它可以轻松与 pytest 集成,并且 api 使用起来非常舒适。

查看this repository了解更多信息。

这个是usage examples

【讨论】:

    【解决方案2】:

    Allure 当然更适合您的需求,因为它具有 pytest 的所有测试报告功能(步骤、功能、故事等)。但它是用 java 编写的,您需要在本地安装 pytest allure 和 allure 二进制文件才能工作。而且您可能需要跳几圈才能使其在您现有的 pytest 代码库中工作。

    如果你想使用 pytest-html 生成测试步骤,这是一个可能的解决方案:

    # Contents of top level conftest.py
    import pytest
    
    @pytest.fixture(scope='session', autouse=True)
    def step(request):
        @pytest.mark.optionalhook
        def pytest_html_results_table_html(request, report, data):
            data.append(html.div(str(request.param), class_='Step'))
    
    #Contents of fixture or test code
    def test_a(step):
        step("abc")
        assert True
    

    我没有在本地运行代码,但这是添加步骤的最终代码应该是什么样子的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-05
      • 2013-09-03
      • 1970-01-01
      • 1970-01-01
      • 2020-10-27
      • 2020-12-18
      • 2022-11-16
      相关资源
      最近更新 更多