Skip to content

Commit ca9efc9

Browse files
committed
Break out environment generation into its own class
1 parent 42054a5 commit ca9efc9

File tree

3 files changed

+62
-25
lines changed

3 files changed

+62
-25
lines changed

README.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,21 @@ via the :code:`pytest_configure` hook:
115115
The generated table will be sorted alphabetically unless the metadata is a
116116
:code:`collections.OrderedDict`.
117117

118+
You can edit the environment table header and row by using the :code:`pytest_html_environment_table_header` and `pytest_html_environment_table_row` hooks:
119+
120+
.. code-block:: python
121+
122+
import pytest
123+
124+
DESCRIPTION_DICT = {"Packages": "Version of pytest packages", "Python": "Version of Python"}
125+
126+
def pytest_html_environment_table_header(cells):
127+
cells.insert(2, html.th('Description'))
128+
129+
def pytest_html_environment_table_row(cells):
130+
cells.insert(2, html.td(description_dict.get(cells[0], '')))
131+
132+
118133
Additional summary information
119134
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
120135

pytest_html/hooks.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ def pytest_html_report_title(report):
77
""" Called before adding the title to the report """
88

99

10+
def pytest_html_environment_table_header(cells):
11+
""" Called after building environment table header. """
12+
13+
14+
def pytest_html_environment_table_row(cells):
15+
""" Called after building environment table row. """
16+
17+
1018
def pytest_html_results_summary(prefix, summary, postfix):
1119
""" Called before adding the summary section to the report """
1220

pytest_html/plugin.py

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# ansi2html is not installed
2626
ANSI = False
2727

28-
from py.xml import html, raw, Tag
28+
from py.xml import html, raw
2929

3030
from . import extras
3131
from . import __version__, __pypi_url__
@@ -336,6 +336,38 @@ def _append_video(self, extra, extra_index, test_index):
336336
)
337337
self.additional_html.append(html.div(html_div, class_="video"))
338338

339+
class EnvironmentTable:
340+
def __init__(self, config):
341+
self.metadata = getattr(config, "_metadata", [])
342+
self.config = config
343+
self.environment_table = []
344+
345+
rows = []
346+
347+
header_cells = [html.th("Key"), html.th("Value")]
348+
self.config.hook.pytest_html_environment_table_header(cells=header_cells)
349+
rows.append(header_cells)
350+
if self.metadata:
351+
keys = [k for k in self.metadata.keys()]
352+
if not isinstance(self.metadata, OrderedDict):
353+
keys.sort()
354+
355+
for key in keys:
356+
value = self.metadata[key]
357+
if isinstance(value, str) and value.startswith("http"):
358+
value = html.a(value, href=value, target="_blank")
359+
elif isinstance(value, (list, tuple, set)):
360+
value = ", ".join(str(i) for i in sorted(map(str, value)))
361+
elif isinstance(value, dict):
362+
sorted_dict = {k: value[k] for k in sorted(value)}
363+
value = json.dumps(sorted_dict)
364+
raw_value_string = raw(str(value))
365+
row_cells = html.tr(html.td(key), html.td(raw_value_string))
366+
self.config.hook.pytest_html_environment_table_row(cells=row_cells)
367+
rows.append(row_cells)
368+
369+
self.environment_table.append(html.table(rows, id="environment"))
370+
339371
def _appendrow(self, outcome, report):
340372
result = self.TestResult(outcome, report, self.logfile, self.config)
341373
if result.row_table is not None:
@@ -551,30 +583,12 @@ def generate_summary_item(self):
551583
return unicode_doc.decode("utf-8")
552584

553585
def _generate_environment(self, config):
554-
if not hasattr(config, "_metadata") or config._metadata is None:
555-
return []
556-
557-
metadata = config._metadata
558-
environment = [html.h2("Environment")]
559-
rows = []
560-
561-
keys = [k for k in metadata.keys()]
562-
if not isinstance(metadata, OrderedDict):
563-
keys.sort()
564-
565-
for key in keys:
566-
value = metadata[key]
567-
if isinstance(value, str) and value.startswith("http"):
568-
value = html.a(value, href=value, target="_blank")
569-
elif isinstance(value, (list, tuple, set)) and not isinstance(value, Tag):
570-
value = ", ".join(str(i) for i in sorted(map(str, value)))
571-
elif isinstance(value, dict):
572-
sorted_dict = {k: value[k] for k in sorted(value)}
573-
value = json.dumps(sorted_dict)
574-
raw_value_string = raw(str(value))
575-
rows.append(html.tr(html.td(key), html.td(raw_value_string)))
576-
577-
environment.append(html.table(rows, id="environment"))
586+
environment_table = self.EnvironmentTable(config).environment_table
587+
if environment_table:
588+
environment = [html.h2("Environment")]
589+
environment.append(environment_table)
590+
else:
591+
environment = []
578592
return environment
579593

580594
def _save_report(self, report_content):

0 commit comments

Comments
 (0)