Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extra fixture #269

Merged
merged 1 commit into from
Feb 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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