为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

忽然笑 2021-09-24 15:11:40
我找不到将字符串添加到代表我的测试步骤的 HTML 报告的方法。我在https://pypi.org/project/pytest-html/ 看到我应该尝试:extra.text('Add some simple Text')但似乎它不起作用。from pytest_html import extrasclass 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我希望每个测试都包含表示为带有信息描述的字符串的测试步骤。有什么办法吗?
查看完整描述

2 回答

?
泛舟湖上清波郎朗

TA贡献1818条经验 获得超3个赞

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

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


查看完整回答
反对 回复 2021-09-24
?
宝慕林4294392

TA贡献2021条经验 获得超8个赞

在这种情况下,我建议您尝试allure而不是 pytest-html 。它很容易与 pytest 集成,而且 api 使用起来非常舒服。

查看此存储库以获取更多信息。

这一个是使用示例


查看完整回答
反对 回复 2021-09-24
  • 2 回答
  • 0 关注
  • 438 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号