Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion allure-pytest/src/listener.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pytest
import re
import html
import allure_commons
from allure_commons.utils import escape_non_unicode_symbols
from allure_commons.utils import now
Expand Down Expand Up @@ -187,12 +189,41 @@ def pytest_runtest_makereport(self, item, call):
if self.config.option.attach_capture:
# Capture at teardown contains data from whole test (setup, call, teardown)
self.attach_data(report.caplog, "log", AttachmentType.TEXT, None)
self.attach_data(report.capstdout, "stdout", AttachmentType.TEXT, None)
stdout_contents = self._convert_to_html(report.capstdout)
self.attach_data(stdout_contents, "stdout", AttachmentType.HTML, None)
self.attach_data(report.capstderr, "stderr", AttachmentType.TEXT, None)

uuid = self._cache.pop(item.nodeid)
self.allure_logger.close_test(uuid)

def _convert_to_html(self, contents):
"""
Converts log to HTML format. Adds different colors based on log prefix
"""
ansi_escape = re.compile(r'\x1b[^m]*m')

result = "<html><body style=\"font-family:'Courier New', Courier, monospace;font-size:14px\">"
for raw_line in contents.splitlines():
line = ansi_escape.sub('', raw_line)
if line.startswith('-Warning'):
result += "<div style=\"color:orange;\">"
elif line.startswith('-Debug'):
result += "<div style=\"color:blue;\">"
elif line.startswith('-Error'):
result += "<div style=\"color:red;font-weight:bold\">"
elif line.startswith('-Liberr'):
result += "<div style=\"color:red;font-weight:bold\">"
elif line.startswith('-Fail'):
result += "<div style=\"color:red;font-weight:bold\">"
elif line.startswith('-Success'):
result += "<div style=\"color:green;\">"
else:
result += "<div>"
result += html.escape(line)
result += "</div>"
result += "</body></html>"
return result

@allure_commons.hookimpl
def attach_data(self, body, name, attachment_type, extension):
self.allure_logger.attach_data(uuid4(), body, name=name, attachment_type=attachment_type, extension=extension)
Expand Down