Skip to content

Disable sort on environment table when metadata is ordered #162

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

Merged
merged 1 commit into from
May 22, 2018
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
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ via the :code:`pytest_configure` hook:
def pytest_configure(config):
config._metadata['foo'] = 'bar'

The generated table will be sorted alphabetically unless the metadata is a
:code:`collections.OrderedDict`.

Additional summary information
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
7 changes: 6 additions & 1 deletion pytest_html/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import absolute_import

from base64 import b64encode, b64decode
from collections import OrderedDict
from os.path import isfile
import datetime
import json
Expand Down Expand Up @@ -465,7 +466,11 @@ def _generate_environment(self, config):
environment = [html.h2('Environment')]
rows = []

for key in [k for k in sorted(metadata.keys()) if metadata[k]]:
keys = [k for k in metadata.keys() if metadata[k]]
if not isinstance(metadata, OrderedDict):
keys.sort()

for key in keys:
value = metadata[key]
if isinstance(value, basestring) and value.startswith('http'):
value = html.a(value, href=value, target='_blank')
Expand Down
12 changes: 12 additions & 0 deletions testing/test_pytest_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,18 @@ def pytest_configure(config):
assert 'Environment' in html
assert len(re.findall(expected_html_re, html)) == 1

def test_environment_ordered(self, testdir):
testdir.makeconftest("""
from collections import OrderedDict
def pytest_configure(config):
config._metadata = OrderedDict([('ZZZ', 1), ('AAA', 2)])
""")
testdir.makepyfile('def test_pass(): pass')
result, html = run(testdir)
assert result.ret == 0
assert 'Environment' in html
assert len(re.findall('ZZZ.+AAA', html, re.DOTALL)) == 1

@pytest.mark.xfail(
sys.version_info < (3, 2) and
LooseVersion(pytest.__version__) >= LooseVersion('2.8.0'),
Expand Down