Skip to content

Added support for bytes objects in Python 3.x #28

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Release Notes

**1.8 (unreleased)**

* Added support for ``bytes`` objects from Python 3.x
* Dropped support for Python 3.2
* Indicated setup and teardown in report
* Fixed colour of errors in report
Expand Down
4 changes: 3 additions & 1 deletion pytest_html/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ def pytest_unconfigure(config):

def data_uri(content, mime_type='text/plain', charset='utf-8'):
if PY3:
data = b64encode(content.encode(charset)).decode('ascii')
if not isinstance(content, bytes):
content = content.encode(charset)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will work, but is generally frowned upon - I'm not familiar enough with the context though to say if it'd make more sense to do it like this, or e.g. add a data_uri_bytes.

data = b64encode(content).decode('ascii')
else:
data = b64encode(content)
return 'data:{0};charset={1};base64,{2}'.format(mime_type, charset, data)
Expand Down
25 changes: 25 additions & 0 deletions test_pytest_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,31 @@ def pytest_runtest_makereport(item, call):
href)
assert link in html

@pytest.mark.xfail(
sys.version_info < (3, 2),
reason='Only affects Python versions later than 3.2',
run=False)
def test_extra_bytes(self, testdir):
content = bytes(str(random.random()), 'utf-8')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will give you a bytes-object with a random number in it - not sure how useful that is? 😉

Also, the more idiomatic way to do this would be str(random.random()).encode('utf-8') - you'll get the same thing, but .encode()/.decode() are more common than the two-argument form of bytes/str

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.text({0})]
""".format(content))
testdir.makepyfile('def test_pass(): pass')
result, html = run(testdir)
assert result.ret == 0
data = b64encode(content).decode('ascii')
href = 'data:text/plain;charset=utf-8;base64,{0}'.format(data)
link = '<a class="text" href="{0}" target="_blank">Text</a>'.format(
href)
assert link in html

def test_extra_url(self, testdir):
content = str(random.random())
testdir.makeconftest("""
Expand Down