Skip to content

Commit 47c2a8e

Browse files
authored
Merge pull request #269 from christiansandberg/extra-fixture
Add extra fixture
2 parents ae0ca62 + 145c746 commit 47c2a8e

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

README.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,17 @@ created hyper link:
192192
193193
extra.append(pytest_html.extras.text('some string', name='Different title'))
194194
195+
It is also possible to use the fixture :code:`extra` to add content directly
196+
in a test function without implementing hooks. These will generally end up
197+
before any extras added by plugins.
198+
199+
.. code-block:: python
200+
201+
from pytest_html import extras
202+
203+
def test_extra(extra):
204+
extra.append(extras.text('some string'))
205+
195206
196207
Modifying the results table
197208
~~~~~~~~~~~~~~~~~~~~~~~~~~~

pytest_html/plugin.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import re
1616

1717
from html import escape
18+
import pytest
1819

1920
try:
2021
from ansi2html import Ansi2HTMLConverter, style
@@ -89,6 +90,29 @@ def pytest_unconfigure(config):
8990
config.pluginmanager.unregister(html)
9091

9192

93+
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
94+
def pytest_runtest_makereport(item, call):
95+
outcome = yield
96+
report = outcome.get_result()
97+
if report.when == "call":
98+
fixture_extras = item.funcargs.get("extra", [])
99+
plugin_extras = getattr(report, "extra", [])
100+
report.extra = fixture_extras + plugin_extras
101+
102+
103+
@pytest.fixture
104+
def extra():
105+
"""Add details to the HTML reports.
106+
107+
.. code-block:: python
108+
109+
import pytest_html
110+
def test_foo(extra):
111+
extra.append(pytest_html.extras.url('http://www.example.com/'))
112+
"""
113+
return []
114+
115+
92116
def data_uri(content, mime_type="text/plain", charset="utf-8"):
93117
data = b64encode(content.encode(charset)).decode("ascii")
94118
return f"data:{mime_type};charset={charset};base64,{data}"

testing/test_pytest_html.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,20 @@ def {test_name}():
583583
assert link in html
584584
assert os.path.exists(src)
585585

586+
def test_extra_fixture(self, testdir):
587+
content = b64encode(b"foo").decode("ascii")
588+
testdir.makepyfile(
589+
f"""
590+
def test_pass(extra):
591+
from pytest_html import extras
592+
extra.append(extras.png('{content}'))
593+
"""
594+
)
595+
result, html = run(testdir, "report.html", "--self-contained-html")
596+
assert result.ret == 0
597+
src = f"data:image/png;base64,{content}"
598+
assert f'<img src="{src}"/>' in html
599+
586600
def test_no_invalid_characters_in_filename(self, testdir):
587601
testdir.makeconftest(
588602
"""

0 commit comments

Comments
 (0)