Add support for JPG and SVG (#87)

Fixes #89
This commit is contained in:
Nicolas 2016-11-30 10:23:34 +01:00 committed by Dave Hunt
parent 733d42c9f0
commit 98e9abb4cf
3 changed files with 60 additions and 29 deletions

View File

@ -9,24 +9,37 @@ FORMAT_TEXT = 'text'
FORMAT_URL = 'url'
def extra(content, format, name=None):
return {'name': name, 'format': format, 'content': content}
def extra(content, format, name=None, mime_type=None, extension=None):
return {'name': name, 'format': format, 'content': content,
'mime_type': mime_type, 'extension': extension}
def html(content):
return extra(content, FORMAT_HTML)
def image(content, name='Image'):
return extra(content, FORMAT_IMAGE, name)
def image(content, name='Image', mime_type='image/png', extension='png'):
return extra(content, FORMAT_IMAGE, name, mime_type, extension)
def png(content, name='Image'):
return image(content, name, mime_type='image/png', extension='png')
def jpg(content, name='Image'):
return image(content, name, mime_type='image/jpeg', extension='jpg')
def svg(content, name='Image'):
return image(content, name, mime_type='image/svg+xml', extension='svg')
def json(content, name='JSON'):
return extra(content, FORMAT_JSON, name)
return extra(content, FORMAT_JSON, name, 'application/json', 'json')
def text(content, name='Text'):
return extra(content, FORMAT_TEXT, name)
return extra(content, FORMAT_TEXT, name, 'text/plain', 'txt')
def url(content, name='URL'):

View File

@ -160,8 +160,9 @@ class HTMLReport(object):
href = None
if extra.get('format') == extras.FORMAT_IMAGE:
if self.self_contained:
src = 'data:image/png;base64,{0}'.format(
extra.get('content'))
src = 'data:{0};base64,{1}'.format(
extra.get('mime_type'),
extra.get('content'))
self.additional_html.append(html.div(
html.img(src=src), class_='image'))
else:
@ -171,7 +172,8 @@ class HTMLReport(object):
else:
content = b64decode(content)
href = src = self.create_asset(
content, extra_index, test_index, 'png', 'wb')
content, extra_index, test_index,
extra.get('extension'), 'wb')
self.additional_html.append(html.div(
html.a(html.img(src=src), href=href),
class_='image'))
@ -183,10 +185,12 @@ class HTMLReport(object):
elif extra.get('format') == extras.FORMAT_JSON:
content = json.dumps(extra.get('content'))
if self.self_contained:
href = data_uri(content, mime_type='application/json')
href = data_uri(content,
mime_type=extra.get('mime_type'))
else:
href = self.create_asset(content, extra_index,
test_index, 'json')
test_index,
extra.get('extension'))
elif extra.get('format') == extras.FORMAT_TEXT:
content = extra.get('content')
@ -194,7 +198,8 @@ class HTMLReport(object):
href = data_uri(content)
else:
href = self.create_asset(content, extra_index,
test_index, 'txt')
test_index,
extra.get('extension'))
elif extra.get('format') == extras.FORMAT_URL:
href = extra.get('content')
@ -382,7 +387,7 @@ class HTMLReport(object):
colspan='5')],
id='not-found-message', hidden='true'),
id='results-table-head'),
self.test_logs], id='results-table')]
self.test_logs], id='results-table')]
main_js = pkg_resources.resource_string(
__name__, os.path.join('resources', 'main.js'))

View File

@ -305,7 +305,12 @@ class TestHTML:
content)
assert link in html
def test_extra_image(self, testdir):
@pytest.mark.parametrize('mime_type, extension',
[('image/png', 'png'),
('image/png', 'image'),
('image/jpeg', 'jpg'),
('image/svg+xml', 'svg')])
def test_extra_image(self, testdir, mime_type, extension):
content = str(random.random())
testdir.makeconftest("""
import pytest
@ -315,19 +320,23 @@ class TestHTML:
report = outcome.get_result()
if report.when == 'call':
from pytest_html import extras
report.extra = [extras.image('{0}')]
""".format(content))
report.extra = [extras.{0}('{1}')]
""".format(extension, content))
testdir.makepyfile('def test_pass(): pass')
result, html = run(testdir, 'report.html', '--self-contained-html')
assert result.ret == 0
src = 'data:image/png;base64,{0}'.format(content)
src = 'data:{0};base64,{1}'.format(mime_type, content)
assert '<img src="{0}"/>'.format(src) in html
@pytest.mark.parametrize('file_extension,file_type',
[('png', 'image'),
('json', 'json'),
('txt', 'text')])
def test_extra_separated(self, testdir, file_extension, file_type):
@pytest.mark.parametrize('file_extension, extra_type, file_type',
[('png', 'image', 'image'),
('png', 'png', 'image'),
('svg', 'svg', 'image'),
('jpg', 'jpg', 'image'),
('json', 'json', 'json'),
('txt', 'text', 'text')])
def test_extra_separated(self, testdir, file_extension,
extra_type, file_type):
content = b64encode(str(random.random())
.encode('utf-8')).decode('ascii')
testdir.makeconftest("""
@ -339,7 +348,7 @@ class TestHTML:
if report.when == 'call':
from pytest_html import extras
report.extra = [extras.{0}('{1}')]
""".format(file_type, content))
""".format(extra_type, content))
testdir.makepyfile('def test_pass(): pass')
result, html = run(testdir)
hash_key = ('test_extra_separated.py::'
@ -355,11 +364,15 @@ class TestHTML:
assert link in html
assert os.path.exists(src)
@pytest.mark.parametrize('file_extension,file_type',
[('png', 'image'),
('json', 'json'),
('txt', 'text')])
def test_extra_separated_rerun(self, testdir, file_extension, file_type):
@pytest.mark.parametrize('file_extension, extra_type, file_type',
[('png', 'png', 'image'),
('png', 'image', 'image'),
('svg', 'svg', 'image'),
('jpg', 'jpg', 'image'),
('json', 'json', 'json'),
('txt', 'text', 'text')])
def test_extra_separated_rerun(self, testdir, file_extension,
extra_type, file_type):
content = b64encode(str(random.random())
.encode('utf-8')).decode('ascii')
testdir.makeconftest("""
@ -371,7 +384,7 @@ class TestHTML:
if report.when == 'call':
from pytest_html import extras
report.extra = [extras.{0}('{1}')]
""".format(file_type, content))
""".format(extra_type, content))
testdir.makepyfile("""
import pytest
@pytest.mark.flaky(reruns=2)