Add ability to specify images as file or URL

Fixes #77
This commit is contained in:
Jim Brännlund 2017-03-07 21:16:12 +01:00 committed by Dave Hunt
parent 0e20601f11
commit f19a86d1e3
No known key found for this signature in database
GPG Key ID: 4000D32ABB02F959
5 changed files with 57 additions and 9 deletions

1
.gitignore vendored
View File

@ -1,6 +1,7 @@
__pycache__
.DS_Store
.cache
.eggs
.tox
build
dist

View File

@ -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

View File

@ -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>`_,
@ -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
-----------------
@ -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:
============ ====================

View File

@ -13,6 +13,7 @@ import sys
import time
import bisect
import hashlib
import warnings
try:
from ansi2html import Ansi2HTMLConverter, style
@ -157,14 +158,20 @@ class HTMLReport(object):
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:
@ -172,9 +179,8 @@ class HTMLReport(object):
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(

View File

@ -418,6 +418,26 @@ class TestHTML:
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):