我目前正在使用 pytest-html 和 selenium 制作一个独立的 html 报告。
这是方法顺便说一句:
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item):
"""
Extends the PyTest Plugin to take and embed screenshot in html report, whenever test fails.
:param item:
"""
pytest_html = item.config.pluginmanager.getplugin('html')
outcome = yield
report = outcome.get_result()
extra = getattr(report, 'extra', [])
if report.when == 'call' or report.when == "setup":
xfail = hasattr(report, 'wasxfail')
if (report.skipped and xfail) or (report.failed and not xfail):
clean_nodeid = report.nodeid.replace("tests/", "")
file_name = ("./reports/screenshots/" + clean_nodeid.replace("::", "_") + ".png")
driver.get_screenshot_as_file(file_name)
screenshot = driver.get_screenshot_as_base64()
extra.append(pytest_html.extras.image(screenshot, ""))
# if file_name:
# html = '<div><img src="%s" alt="screenshot" style="width:304px;height:228px;" ' \
# 'onclick="window.open(this.src)" align="right"/></div>' % file_name
report.extra = extra
将装饰器从
@pytest.mark.hookwrapper
更改为 @pytest.hookimpl(tryfirst=True)
会导致 AttributeError: generator object has no attribute skipped
异常。据我了解,新的装饰器没有跳过的属性。
如有任何帮助,我们将不胜感激!
您需要添加
hookwrapper=True
@pytest.hookimpl(hookwrapper=True, tryfirst=True)