Create jira ticket with error message from failed test in PyTest

228 Views Asked by At

I have a piece of code that generates a report and adds a screenshot to it when test fails:

def pytest_runtest_makereport(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):
        file_name = report.nodeid.replace("::", "_") + ".png"
        _capture_screenshot(file_name)
        if file_name:
            create_jira_issue(report.captlog, file_name)
            html = '<div><img src="%s" alt="screenshot" style="width:304px;height:228px;" ' \
                   'onclick="window.open(this.src)" align="right"/></div>' % file_name
            extra.append(pytest_html.extras.html(html))
    report.extra = extra

It also creates Jira ticket with a description and screenshot. What I would like to pass as "Description" is basically the log (which I already have) plus the assertion failure information.

Is there a way of getting the info here and passing it?

Is there, perhaps, a better way to create the ticket for a failed test?

1

There are 1 best solutions below

1
On

To add output and assertion failure information as test output, just add following argument:

python3 -m pytest (...) --capture=sys (...)

And basically, that's all :)

If that will be contained in your jira, it depends highly how you create this ticket from HTML report. That way for sure you get it in report.