Add extra fixture

This commit is contained in:
Christian Sandberg 2020-02-23 21:58:39 +01:00
parent ae0ca62ad5
commit 145c746a07
3 changed files with 49 additions and 0 deletions

View File

@ -192,6 +192,17 @@ created hyper link:
extra.append(pytest_html.extras.text('some string', name='Different title'))
It is also possible to use the fixture :code:`extra` to add content directly
in a test function without implementing hooks. These will generally end up
before any extras added by plugins.
.. code-block:: python
from pytest_html import extras
def test_extra(extra):
extra.append(extras.text('some string'))
Modifying the results table
~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -15,6 +15,7 @@ import warnings
import re
from html import escape
import pytest
try:
from ansi2html import Ansi2HTMLConverter, style
@ -89,6 +90,29 @@ def pytest_unconfigure(config):
config.pluginmanager.unregister(html)
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
report = outcome.get_result()
if report.when == "call":
fixture_extras = item.funcargs.get("extra", [])
plugin_extras = getattr(report, "extra", [])
report.extra = fixture_extras + plugin_extras
@pytest.fixture
def extra():
"""Add details to the HTML reports.
.. code-block:: python
import pytest_html
def test_foo(extra):
extra.append(pytest_html.extras.url('http://www.example.com/'))
"""
return []
def data_uri(content, mime_type="text/plain", charset="utf-8"):
data = b64encode(content.encode(charset)).decode("ascii")
return f"data:{mime_type};charset={charset};base64,{data}"

View File

@ -583,6 +583,20 @@ class TestHTML:
assert link in html
assert os.path.exists(src)
def test_extra_fixture(self, testdir):
content = b64encode(b"foo").decode("ascii")
testdir.makepyfile(
f"""
def test_pass(extra):
from pytest_html import extras
extra.append(extras.png('{content}'))
"""
)
result, html = run(testdir, "report.html", "--self-contained-html")
assert result.ret == 0
src = f"data:image/png;base64,{content}"
assert f'<img src="{src}"/>' in html
def test_no_invalid_characters_in_filename(self, testdir):
testdir.makeconftest(
"""