Skip to content

Commit 6731791

Browse files
werdeilVincent VERDEIL
authored andcommitted
Break out environment generation into its own class
1 parent 4552a23 commit 6731791

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__
@@ -341,6 +341,38 @@ def _append_video(self, extra, extra_index, test_index):
341341
)
342342
self.additional_html.append(html.div(html_div, class_="video"))
343343

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

558590
def _generate_environment(self, config):
559-
if not hasattr(config, "_metadata") or config._metadata is None:
560-
return []
561-
562-
metadata = config._metadata
563-
environment = [html.h2("Environment")]
564-
rows = []
565-
566-
keys = [k for k in metadata.keys()]
567-
if not isinstance(metadata, OrderedDict):
568-
keys.sort()
569-
570-
for key in keys:
571-
value = metadata[key]
572-
if isinstance(value, str) and value.startswith("http"):
573-
value = html.a(value, href=value, target="_blank")
574-
elif isinstance(value, (list, tuple, set)) and not isinstance(value, Tag):
575-
value = ", ".join(str(i) for i in sorted(map(str, value)))
576-
elif isinstance(value, dict):
577-
sorted_dict = {k: value[k] for k in sorted(value)}
578-
value = json.dumps(sorted_dict)
579-
raw_value_string = raw(str(value))
580-
rows.append(html.tr(html.td(key), html.td(raw_value_string)))
581-
582-
environment.append(html.table(rows, id="environment"))
591+
environment_table = self.EnvironmentTable(config).environment_table
592+
if environment_table:
593+
environment = [html.h2("Environment")]
594+
environment.append(environment_table)
595+
else:
596+
environment = []
583597
return environment
584598

585599
def _save_report(self, report_content):

0 commit comments

Comments
 (0)