diff --git a/.gitignore b/.gitignore index 7cf62bcd..a05244ee 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ __pycache__ .DS_Store .cache +.eggs .tox build dist diff --git a/README.rst b/README.rst index 3cb9cbc3..128e18fe 100644 --- a/README.rst +++ b/README.rst @@ -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) `_, @@ -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('
Additional HTML
')`` 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: ============ ==================== diff --git a/pytest_html/plugin.py b/pytest_html/plugin.py index afc6275a..c530ae63 100644 --- a/pytest_html/plugin.py +++ b/pytest_html/plugin.py @@ -13,6 +13,7 @@ import time import bisect import hashlib +import warnings try: from ansi2html import Ansi2HTMLConverter, style @@ -157,14 +158,20 @@ 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: @@ -172,9 +179,8 @@ def append_extra_html(self, extra, extra_index, test_index): 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( diff --git a/testing/test_pytest_html.py b/testing/test_pytest_html.py index 547aeed5..e4b8b929 100644 --- a/testing/test_pytest_html.py +++ b/testing/test_pytest_html.py @@ -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 ''.format(content) in html + def test_no_environment(self, testdir): testdir.makeconftest(""" def pytest_configure(config):