Skip to content

Commit

Permalink
Add ability to specify images as file or URL
Browse files Browse the repository at this point in the history
  • Loading branch information
BeyondEvil authored and davehunt committed Jun 6, 2017
1 parent 0e20601 commit f19a86d
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
__pycache__
.DS_Store
.cache
.eggs
.tox
build
dist
Expand Down
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Release Notes
-------------

**1.15.0 (unreleased)**

* Add ability to specify images as file or URL

* Thanks to `@BeyondEvil <https://github.com/BeyondEvil>`_ for the PR

**1.14.2 (2017-03-10)**

* Always encode content for data URI
Expand Down
17 changes: 16 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ you have this package installed, then ANSI codes will be converted to HTML in
your report.

Creating a self-contained report
----------------------------------
--------------------------------

In order to respect the `Content Security Policy (CSP)
<https://developer.mozilla.org/docs/Web/Security/CSP>`_,
Expand All @@ -65,6 +65,12 @@ convenient when sharing your results. This can be done in the following way:
$ pytest --html=report.html --self-contained-html
Images added as files or links are going to be linked as external resources,
meaning that the standalone report HTML-file may not display these images
as expected.

The plugin will issue a warning when adding files or links to the standalone report.

Enhancing reports
-----------------

Expand Down Expand Up @@ -94,8 +100,17 @@ Raw HTML ``extra.html('<div>Additional HTML</div>')``
Plain text ``extra.text('Add some simple Text')``
URL ``extra.url('http://www.example.com/')``
Image ``extra.image(image, mime_type='image/gif', extension='gif')``
Image ``extra.image('/path/to/file.png')``
Image ``extra.image('http://some_image.png')``
========== ============================================

**Note**: When adding an image from file, the path can be either absolute
or relative.

**Note**: When using ``--self-contained-html``, images added as files or links
may not work as expected, see section `Creating a self-contained report`_ for
more info.

There are also convenient types for several image formats:

============ ====================
Expand Down
22 changes: 14 additions & 8 deletions pytest_html/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import time
import bisect
import hashlib
import warnings

try:
from ansi2html import Ansi2HTMLConverter, style
Expand Down Expand Up @@ -157,24 +158,29 @@ def create_asset(self, content, extra_index,
def append_extra_html(self, extra, extra_index, test_index):
href = None
if extra.get('format') == extras.FORMAT_IMAGE:
if self.self_contained:
content = extra.get('content')
if content.startswith(('file', 'http')) or \
os.path.isfile(content):
if self.self_contained:
warnings.warn('Self-contained HTML report '
'includes link to external '
'resource: {}'.format(content))
html_div = html.a(html.img(src=content), href=content)
elif self.self_contained:
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'))
content)
html_div = html.img(src=src)
else:
content = extra.get('content')
if PY3:
content = b64decode(content.encode('utf-8'))
else:
content = b64decode(content)
href = src = self.create_asset(
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'))
html_div = html.a(html.img(src=src), href=href)
self.additional_html.append(html.div(html_div, class_='image'))

elif extra.get('format') == extras.FORMAT_HTML:
self.additional_html.append(html.div(
Expand Down
20 changes: 20 additions & 0 deletions testing/test_pytest_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,26 @@ def test_fail():
assert link in html
assert os.path.exists(src)

@pytest.mark.parametrize('src_type', ["https://", "file://", "image.png"])
def test_extra_image_non_b64(self, testdir, src_type):
content = src_type
testdir.makeconftest("""
import pytest
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
outcome = yield
report = outcome.get_result()
if report.when == 'call':
from pytest_html import extras
report.extra = [extras.image('{0}')]
""".format(content))
testdir.makepyfile('def test_pass(): pass')
if src_type == "image.png":
testdir.makefile('.png', image='pretty picture')
result, html = run(testdir, 'report.html')
assert result.ret == 0
assert '<a href="{0}"><img src="{0}"/>'.format(content) in html

def test_no_environment(self, testdir):
testdir.makeconftest("""
def pytest_configure(config):
Expand Down

0 comments on commit f19a86d

Please sign in to comment.