From 4e08e44b8f6ce2dc0feefbc108ac5b107d3e57be Mon Sep 17 00:00:00 2001 From: Zoya Date: Wed, 26 Feb 2020 09:36:17 -0800 Subject: [PATCH] color coding stdout --- allure-pytest/src/listener.py | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/allure-pytest/src/listener.py b/allure-pytest/src/listener.py index 894b57a2..532dfba4 100644 --- a/allure-pytest/src/listener.py +++ b/allure-pytest/src/listener.py @@ -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 @@ -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 = "" + for raw_line in contents.splitlines(): + line = ansi_escape.sub('', raw_line) + if line.startswith('-Warning'): + result += "
" + elif line.startswith('-Debug'): + result += "
" + elif line.startswith('-Error'): + result += "
" + elif line.startswith('-Liberr'): + result += "
" + elif line.startswith('-Fail'): + result += "
" + elif line.startswith('-Success'): + result += "
" + else: + result += "
" + result += html.escape(line) + result += "
" + result += "" + 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)