如何向pytest html报告中添加其他变量 [英] How to add additional variable to pytest html report

查看:641
本文介绍了如何向pytest html报告中添加其他变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using pytest HTML report plugin for my selenium tests. It works great with just passing test.py --html==report.htmlin command line and generates a great report.

我还需要为每个测试用例显示实现附加的字符串/变量.无论通过还是失败都没有关系,它应该只显示票号".我可以在每个测试场景中返回的票证ID.

I also need to implement additional string/variable to each test case display. It doesn't matter if it's pass or failed it should just show "ticket number". This ticket id I can return in each test scenario.

我可以在测试名称中添加票证号码,但这看起来很难看.

I could just add ticket number to test name, but it will look ugly.

请告知最佳方法是什么.

Please advise what's the best way to do it.

谢谢.

推荐答案

您可以为每个测试插入自定义html,方法是将html内容添加到每个测试的显示详细信息"部分,或者自定义结果表(例如,添加一个票证栏).

You can insert custom html for each test by either adding html content to the "show details" section of each test, or customizing the result table (e.g. add a ticket column).

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

The first possibility is the simplest, you can just add the following to your 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

您可以在其中用内容替换<p>some html</p>的地方.

Where you can replace <p>some html</p> with your content.

第二种解决方法是:

@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对象访问当前测试,这可能有助于检索所需的信息.

Remember that you can always access the current test with the item object, this might help retrieving the information you need.

这篇关于如何向pytest html报告中添加其他变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆