0

我想做一些类似于pytest-html 添加自定义测试结果以报告的事情,但在我的情况下,我在记录器周围有一个包装器,所以当测试失败时,它会注销(到 .txt 文件)一个 DataFrame执行断言后的差异。我的 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

所以我想添加这样的东西:

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

这基本上就是我在日志中出错的原因。

我有返回并打印出该数据框的函数,但是我怎样才能得到它并将其注入到报告中呢?

4

0 回答 0