Add ANSI support to report

ANSI codes used for color logging is not retained in html report
This patch adds that support. Fixes/implements issue#74.

Signed-off-by: Prem Karat <pkarat@mvista.com>
This commit is contained in:
Prem Karat 2016-11-02 11:51:09 +05:30 committed by Dave Hunt
parent a59ca3106c
commit 24e169d9a4
No known key found for this signature in database
GPG Key ID: 4000D32ABB02F959
3 changed files with 29 additions and 2 deletions

View File

@ -15,6 +15,7 @@ import time
import bisect
import hashlib
from ansi2html import Ansi2HTMLConverter, style
import pytest
from py.xml import html, raw
@ -226,7 +227,9 @@ class HTMLReport(object):
for header, content in report.sections:
log.append(' {0} '.format(header).center(80, '-'))
log.append(html.br())
log.append(content)
content = Ansi2HTMLConverter(inline=False).convert(content,
full=False)
log.append(raw(content))
if len(log) == 0:
log = html.div(class_='empty log')
@ -288,6 +291,13 @@ class HTMLReport(object):
if PY3:
self.style_css = self.style_css.decode('utf-8')
ansi_css = [
'\n/******************************',
' * ANSI2HTML STYLES',
' ******************************/\n']
ansi_css.extend([str(r) for r in style.get_styles()])
self.style_css += '\n'.join(ansi_css)
css_href = '{0}/{1}'.format('assets', 'style.css')
html_css = html.link(href=css_href, rel='stylesheet',
type='text/css')

View File

@ -11,7 +11,8 @@ setup(name='pytest-html',
package_data={'pytest_html': ['resources/*']},
entry_points={'pytest11': ['html = pytest_html.plugin']},
setup_requires=['setuptools_scm'],
install_requires=['pytest>=2.3'],
install_requires=['ansi2html>=1.1.1',
'pytest>=2.3'],
license='Mozilla Public License 2.0 (MPL 2.0)',
keywords='py.test pytest html report',
classifiers=[

View File

@ -475,3 +475,19 @@ class TestHTML:
result, html = run(testdir)
assert result.ret == 0
assert_results(html, passed=1)
def test_ansi_color(self, testdir):
pass_content = ["<span class=\"ansi31\">RCOLOR",
"<span class=\"ansi32\">GCOLOR",
"<span class=\"ansi33\">YCOLOR"]
testdir.makepyfile(r"""
def test_ansi():
colors = ['\033[31mRCOLOR\033[0m', '\033[32mGCOLOR\033[0m',
'\033[33mYCOLOR\033[0m']
for color in colors:
print(color)
""")
result, html = run(testdir, 'report.html', '--self-contained-html')
assert result.ret == 0
for content in pass_content:
assert content in html