简体   繁体   English

pytest-html 添加自定义测试结果报告

[英]pytest-html add custom test results to report

I'm using pytest0html to generate my html report.我正在使用 pytest0html 生成我的 html 报告。 my test record tested values and I need to show a table of this values with prettytable in case of success.我的测试记录测试了值,如果成功,我需要用漂亮的表格显示这些值的表格。 I think that i need to implement this hook:我认为我需要实现这个钩子:

@pytest.mark.optionalhook
def pytest_html_results_table_html(report, data):
    if report.passed:
        del data[:]
        data.append(my_pretty_table_string)
        # or data.append(report.extra.text)

But how to pass my_pretty_table_string to the hook function or how to edit report.extra.text from my test function ?但是如何将 my_pretty_table_string 传递给钩子函数或如何从我的测试函数中编辑 report.extra.text ? Thank you for your help感谢您的帮助

Where is your my_pretty_table_string stored and generated? 您的my_pretty_table_string在哪里存储和生成?

Please give more details to your question so we can help :) 请为您的问题提供更多详细信息,以便我们为您提供帮助:)

pytest_html_results_table_html gets report object from pytest_runtest_makereport hook. pytest_html_results_table_htmlpytest_runtest_makereport挂钩获取report对象。 So you need to implement pytest_runtest_makereport to add your data to the result at 'call' step. 因此,您需要实现pytest_runtest_makereport ,以便在“调用”步骤将数据添加到结果中。

@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
   outcome = yield # get makereport outcome
   if call.when == 'call':
      outcome.result.my_data = <your data object goes here>

then you can access report.my_data in pytest_html_results_table_html hook. 那么您可以在pytest_html_results_table_html挂钩中访问report.my_data

Alex 亚历克斯

Hey @Alexper I wanna do something like what @user210267 but in my case I have a wrapper around he logger so when failure it's logging out (to a .txt file as well) a DataFrame with the difference after performed the assertion.嘿@Alexper 我想做一些类似于@user210267 的事情,但在我的情况下,我在他的记录器周围有一个包装器,所以当失败时,它会在执行断言后注销(也到 .txt 文件)一个具有差异的 DataFrame。 My pytest_runtest_makereport looks like:我的 pytest_runtest_makereport 看起来像:

@pytest.hookimpl(hookwrapper=True)
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":
      # always add url to report
      extra.append(pytest_html.extras.url("http://www.myreport.com/"))
      xfail = hasattr(report, "wasxfail")

      report_dir = config.named_folders.test_reports
      now = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
      report_dir = report_dir.replace("[TIMESTAMP]", now)
    
      if (report.skipped and xfail) or (report.failed and not xfail):
         screenshot = report.nodeid.replace("::", "_") + ".png" 
         screenshot = "screenshot_" + now + ".png"
         dest_file = os.path.join(os.path.dirname(__file__), screenshot)

         feature_request = item.funcargs['request']
         driver = feature_request.getfixturevalue('setup')
         driver.save_screenshot(dest_file)
         browser_logs = driver.get_log('browser')

        if browser_logs:
            print(browser_logs)
            extra.append(pytest_html.extras.html(f"<div>{browser_logs}</div>"))

        extra.append(pytest_html.extras.image(dest_file))
        
    report.extra = extra

So I wanna add something like this:所以我想添加这样的东西:

+----+----------+---------+
|    | Value 1  | Value 2 |
|----+----------+---------|
|  0 | title    | Title   |
+----+----------+---------+

that is basically what i'm erroring out to the logs.这基本上就是我在日志中出错的内容。

I have the function that returns and print out that dataframe but how can I get that and inject it to the report?我有返回并打印出该数据帧的函数,但是如何获取它并将其注入报告中?

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

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