Search code examples
selenium-webdriverpytestpytest-html

Using the "@pytest.hookimpl(tryfirst=True)" results in the following AttributeError "generator object has no attribute skipped"


I am currently making a self contained html report using pytest-html and selenium.

This is the method btw:

@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

Changing the decorator from the @pytest.mark.hookwrapper to @pytest.hookimpl(tryfirst=True) results in the AttributeError: generator object has no attribute skipped exception. From what I understand the new decorator does not have the skipped attribute.

Any help would be appreciated!


Solution

  • You need to add hookwrapper=True

    @pytest.hookimpl(hookwrapper=True, tryfirst=True)