Skip to content

Commit

Permalink
Add extra fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
christiansandberg committed Feb 29, 2020
1 parent ae0ca62 commit 145c746
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
24 changes: 24 additions & 0 deletions pytest_html/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import re

from html import escape
import pytest

try:
from ansi2html import Ansi2HTMLConverter, style
Expand Down Expand Up @@ -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}"
Expand Down
14 changes: 14 additions & 0 deletions testing/test_pytest_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,20 @@ def {test_name}():
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(
"""
Expand Down

0 comments on commit 145c746

Please sign in to comment.