简体   繁体   English

如何使用 pytest-html 将测试步骤写入生成的 html 报告?

[英]How to write test steps to the generated html report with pytest-html?

I can't find a way to add string to my HTML report that represent my test steps.我找不到将字符串添加到代表我的测试步骤的 HTML 报告的方法。

I saw in https://pypi.org/project/pytest-html/ that I should try with:我在https://pypi.org/project/pytest-html/中看到我应该尝试使用:

extra.text('Add some simple Text')

but seems that it doesn't work.但似乎它不起作用。

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

I expect that each test will contains test steps represented as string with iformative description.我希望每个测试都包含测试步骤,这些测试步骤表示为带有信息性描述的字符串。

is there any way to do so?有什么办法吗?

I would suggest you to try allure instead of pytest-html in this case.在这种情况下,我建议您尝试allure而不是 pytest-html。 it has easy integration with pytest and the api is really comfortable to use.它可以轻松与 pytest 集成,并且 api 使用起来非常舒适。

check out this repository for more information.查看此存储库以获取更多信息。

and this one for usage examples .这一个用于使用示例

Allure is certainly better suited for your needs as it has all the test reporting features (steps, feature, stories, etc.) for pytest. Allure 肯定更适合您的需求,因为它具有 pytest 的所有测试报告功能(步骤、功能、故事等)。 But it is written in java and you need to install pytest allure as well as allure binary locally for it to work.但它是用 java 编写的,您需要在本地安装 pytest allure 和 allure 二进制文件才能工作。 And you may have to jump few hoops to make it work in your existing pytest codebase.你可能需要跳几圈才能让它在你现有的 pytest 代码库中工作。

If you want to use pytest-html for generating test steps, this is a possible solution:如果您想使用 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

I have not run the code locally but this is what final code for adding step should look like.我没有在本地运行代码,但这是添加步骤的最终代码应该是什么样子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM