Skip to content
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
22 changes: 22 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,28 @@ additional HTML and log output with a notice that the log is empty:
del data[:]
data.append(html.div('No log output captured.', class_='empty log'))

Modifying the whole test document
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Finally, for major customizations, you may manipulate and update the resulting
html document prior to finalization by hooking :code:`pytest_html_report_final_dom`.

.. code-block:: python

import pytest

def pytest_html_report_final_dom(session, document):
# session is a `_pytest.main.Session` object
from importlib.resources import read_text
js = read_text(".", "fancy.js")
for node in list(iter(document)):
if isinstance(node, html.head):
node.extend([html.script(raw(js), type="text/javascript")])





Display options
---------------

Expand Down
4 changes: 4 additions & 0 deletions pytest_html/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ def pytest_html_results_table_row(report, cells):

def pytest_html_results_table_html(report, data):
""" Called after building results table additional HTML. """


def pytest_html_report_final_dom(session, document):
""" Called after the entire document has been created. """
43 changes: 27 additions & 16 deletions pytest_html/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,31 +544,42 @@ def generate_summary_item(self):

session.config.hook.pytest_html_report_title(report=self)

summary_prefix, summary_postfix = [], []
session.config.hook.pytest_html_results_summary(
prefix=summary_prefix, summary=summary, postfix=summary_postfix
)
summary = [html.h2("Summary"), *summary_prefix, *summary, *summary_postfix]

body = html.body(
html.script(raw(main_js)),
html.h1(self.title),
html.p(
"Report generated on {} at {} by ".format(
generated.strftime("%d-%b-%Y"), generated.strftime("%H:%M:%S")
html.div(
html.div(
html.h1(self.title),
html.p(
"Report generated on {} at {} by ".format(
generated.strftime("%d-%b-%Y"),
generated.strftime("%H:%M:%S"),
),
html.a("pytest-html", href=__pypi_url__),
f" v{__version__}",
),
html.div(
*self._generate_environment(session.config),
class_="content-environment",
),
html.div(*summary, class_="content-summary"),
class_="content-header",
),
html.a("pytest-html", href=__pypi_url__),
f" v{__version__}",
html.div(*results, class_="content-results", id="results-container"),
class_="content",
),
onLoad="init()",
)

body.extend(self._generate_environment(session.config))

summary_prefix, summary_postfix = [], []
session.config.hook.pytest_html_results_summary(
prefix=summary_prefix, summary=summary, postfix=summary_postfix
)
body.extend([html.h2("Summary")] + summary_prefix + summary + summary_postfix)

body.extend(results)

doc = html.html(head, body)

session.config.hook.pytest_html_report_final_dom(session=session, document=doc)

unicode_doc = "<!DOCTYPE html>\n{}".format(doc.unicode(indent=2))

# Fix encoding issues, e.g. with surrogates
Expand Down
2 changes: 1 addition & 1 deletion pytest_html/resources/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function sort_table(clicked, key_func) {
sorted_rows.forEach(function(elem) {
parent.appendChild(elem);
});
document.getElementsByTagName("BODY")[0].appendChild(parent);
document.getElementById("results-container").appendChild(parent);
}

function sort(items, key_func, reversed) {
Expand Down
94 changes: 48 additions & 46 deletions testing/js_test_report.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,51 +14,53 @@
<script src="test.js"></script>
<script src="../pytest_html/resources/main.js" data-cover></script>
<div id="qunit-fixture">
<table id="results-table">
<thead id="results-table-head">
<tr>
<th class="sortable result initial-sort" col="result">Result</th>
<th class="sortable" col="name">Test</th>
<th class="sortable numeric" col="duration">Duration</th>
<th class="sortable links" col="links">Links</th></tr>
<tr hidden="true" id="not-found-message">
<th colspan="5">No results found. Try to check the filters</th>
</tr>
</thead>
<tbody class="rerun results-table-row">
<tr>
<td class="col-result">Rerun</td>
<td class="test-1 col-name">rerun.py::test_rexample_1</td>
<td class="col-duration">1.00</td>
<td class="col-links"><a class="url" href="http://www.google.com/" target="_blank">URL</a> </td></tr>
<tr>
<td class="extra" colspan="5">
<div class="log">@pytest.mark.flaky(reruns=5)<br/> def test_example():<br/> import random<br/>&gt; assert random.choice([True, False])<br/><span class="error">E assert False</span><br/><span class="error">E + where False = &lt;bound method Random.choice of &lt;random.Random object at 0x7fe80b85f420&gt;&gt;([True, False])</span><br/><span class="error">E + where &lt;bound method Random.choice of &lt;random.Random object at 0x7fe80b85f420&gt;&gt; = &lt;module 'random' from '/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/random.pyc'&gt;.choice</span><br/><br/>rerun.py:6: AssertionError<br/></div></td></tr></tbody>
<tbody class="passed results-table-row">
<tr>
<td class="col-result">Passed</td>
<td class="test-2 col-name">rerun.py::test_example_2</td>
<td class="col-duration">0.00</td>
<td class="col-links"></td></tr>
<tr>
<td class="extra" colspan="5">
<div class="empty log">No log output captured.</div>
</td>
</tr>
</tbody>
<tbody class="passed results-table-row">
<tr>
<td class="col-result">Passed</td>
<td class="test-3 col-name">rerun.py::test_example_3</td>
<td class="col-duration">0.00</td>
<td class="col-links"></td></tr>
<tr>
<td class="extra" colspan="5">
<div class="empty log">No log output captured.</div>
</td>
</tr>
</tbody>
</table>
</div>
<div id="results-container">
<table id="results-table">
<thead id="results-table-head">
<tr>
<th class="sortable result initial-sort" col="result">Result</th>
<th class="sortable" col="name">Test</th>
<th class="sortable numeric" col="duration">Duration</th>
<th>Links</th></tr>
<tr hidden="true" id="not-found-message">
<th colspan="5">No results found. Try to check the filters</th>
</tr>
</thead>
<tbody class="rerun results-table-row">
<tr>
<td class="col-result">Rerun</td>
<td class="test-1 col-name">rerun.py::test_rexample_1</td>
<td class="col-duration">1.00</td>
<td class="col-links"></td></tr>
<tr>
<td class="extra" colspan="5">
<div class="log">@pytest.mark.flaky(reruns=5)<br/> def test_example():<br/> import random<br/>&gt; assert random.choice([True, False])<br/><span class="error">E assert False</span><br/><span class="error">E + where False = &lt;bound method Random.choice of &lt;random.Random object at 0x7fe80b85f420&gt;&gt;([True, False])</span><br/><span class="error">E + where &lt;bound method Random.choice of &lt;random.Random object at 0x7fe80b85f420&gt;&gt; = &lt;module 'random' from '/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/random.pyc'&gt;.choice</span><br/><br/>rerun.py:6: AssertionError<br/></div></td></tr></tbody>
<tbody class="passed results-table-row">
<tr>
<td class="col-result">Passed</td>
<td class="test-2 col-name">rerun.py::test_example_2</td>
<td class="col-duration">0.00</td>
<td class="col-links"></td></tr>
<tr>
<td class="extra" colspan="5">
<div class="empty log">No log output captured.</div>
</td>
</tr>
</tbody>
<tbody class="passed results-table-row">
<tr>
<td class="col-result">Passed</td>
<td class="test-3 col-name">rerun.py::test_example_3</td>
<td class="col-duration">0.00</td>
<td class="col-links"></td></tr>
<tr>
<td class="extra" colspan="5">
<div class="empty log">No log output captured.</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>