Skip to content

Commit

Permalink
Add support for JPG and SVG (pytest-dev#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
bhzunami authored and davehunt committed Nov 30, 2016
1 parent 733d42c commit 98e9abb
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 29 deletions.
25 changes: 19 additions & 6 deletions pytest_html/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,37 @@
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'):
Expand Down
19 changes: 12 additions & 7 deletions pytest_html/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,9 @@ def append_extra_html(self, extra, extra_index, test_index):
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:
Expand All @@ -171,7 +172,8 @@ def append_extra_html(self, extra, extra_index, test_index):
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'))
Expand All @@ -183,18 +185,21 @@ def append_extra_html(self, extra, extra_index, test_index):
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')
if self.self_contained:
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')
Expand Down Expand Up @@ -382,7 +387,7 @@ def generate_summary_item(self):
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'))
Expand Down
45 changes: 29 additions & 16 deletions testing/test_pytest_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,12 @@ def pytest_runtest_makereport(item, call):
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
Expand All @@ -315,19 +320,23 @@ def pytest_runtest_makereport(item, call):
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("""
Expand All @@ -339,7 +348,7 @@ def pytest_runtest_makereport(item, call):
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::'
Expand All @@ -355,11 +364,15 @@ def pytest_runtest_makereport(item, call):
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("""
Expand All @@ -371,7 +384,7 @@ def pytest_runtest_makereport(item, call):
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)
Expand Down

0 comments on commit 98e9abb

Please sign in to comment.