【问题标题】:How to add additional variable to pytest html report如何向 pytest html 报告添加其他变量
【发布时间】:2017-08-31 16:26:12
【问题描述】:

我正在为我的硒测试使用pytest HTML 报告插件。 只需通过test.py --html==report.htmlin 命令行即可很好地工作并生成出色的报告。

我还需要为每个测试用例显示实现额外的字符串/变量。不管是通过还是失败,都应该只显示“票号”。这个ticket id我可以在每个测试场景中返回。

我可以在测试名称中添加票号,但它看起来很难看。

请告知最好的方法。

谢谢。

【问题讨论】:

    标签: python html selenium selenium-webdriver pytest


    【解决方案1】:

    您可以通过将 html 内容添加到每个测试的“显示详细信息”部分,或自定义结果表(例如添加工单列)来为每个测试插入自定义 html。

    第一种可能性是最简单的,您只需将以下内容添加到您的 conftest.py

    @pytest.mark.hookwrapper
    def pytest_runtest_makereport(item, call):
        pytest_html = item.config.pluginmanager.getplugin('html')
        outcome = yield
        report = outcome.get_result()
        extra = getattr(report, 'extra', [])
        if report.when == 'call':
            extra.append(pytest_html.extras.html('<p>some html</p>'))
            report.extra = extra
    

    您可以将&lt;p&gt;some html&lt;/p&gt; 替换为您的内容。

    第二种解决方案是:

    @pytest.mark.optionalhook
    def pytest_html_results_table_header(cells):
        cells.insert(1, html.th('Ticket'))
    
    
    @pytest.mark.optionalhook
    def pytest_html_results_table_row(report, cells):
        cells.insert(1, html.td(report.ticket))
    
    
    @pytest.mark.hookwrapper
    def pytest_runtest_makereport(item, call):
        outcome = yield
        report = outcome.get_result()
        report.ticket = some_function_that_gets_your_ticket_number()
    

    请记住,您始终可以使用 item 对象访问当前测试,这可能有助于检索您需要的信息。

    【讨论】:

    • 您好,应该为“html”导入哪个模块?
    • @user1181979 from py.xml import html
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    相关资源
    最近更新 更多