Skip to content

Commit

Permalink
fix: Revert report generation to full run (#754)
Browse files Browse the repository at this point in the history
* fix: Revert report generation to full run

* fix: Remove the unserializable cleanup
  • Loading branch information
BeyondEvil authored Oct 31, 2023
1 parent 263c1c6 commit bf19498
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
12 changes: 12 additions & 0 deletions docs/user_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ of a less permissive license, this package is not included as a dependency. If
you have this package installed, then ANSI codes will be converted to HTML in
your report.

Report streaming
----------------

In order to stream the result, basically generating the report for each finished test
instead of waiting until the full run is finished, you can set the ``generate_report_on_test``
ini-value:

.. code-block:: ini
[pytest]
generate_report_on_test = True
Creating a self-contained report
--------------------------------

Expand Down
6 changes: 3 additions & 3 deletions src/pytest_html/basereport.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

from pytest_html import __version__
from pytest_html import extras
from pytest_html.util import cleanup_unserializable


class BaseReport:
Expand Down Expand Up @@ -49,7 +48,7 @@ def _asset_filename(self, test_id, extra_index, test_index, file_extension):

def _generate_report(self, self_contained=False):
generated = datetime.datetime.now()
test_data = cleanup_unserializable(self._report.data)
test_data = self._report.data
test_data = json.dumps(test_data)
rendered_report = self._template.render(
title=self._report.title,
Expand Down Expand Up @@ -239,7 +238,8 @@ def pytest_runtest_logreport(self, report):
dur = test_duration if when == "call" else each.duration
self._process_report(each, dur)

self._generate_report()
if self._config.getini("generate_report_on_test"):
self._generate_report()

def _process_report(self, report, duration):
outcome = _process_outcome(report)
Expand Down
7 changes: 7 additions & 0 deletions src/pytest_html/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ def pytest_addoption(parser):
default="result",
help="column to initially sort on.",
)
parser.addini(
"generate_report_on_test",
type="bool",
default=False,
help="the HTML report will be generated after each test "
"instead of at the end of the run.",
)


def pytest_configure(config):
Expand Down
15 changes: 0 additions & 15 deletions src/pytest_html/util.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import json
from functools import partial
from typing import Any
from typing import Dict

from jinja2 import Environment
from jinja2 import FileSystemLoader
Expand All @@ -23,18 +20,6 @@
_ansi_styles = []


def cleanup_unserializable(d: Dict[str, Any]) -> Dict[str, Any]:
"""Return new dict with entries that are not json serializable by their str()."""
result = {}
for k, v in d.items():
try:
json.dumps({k: v})
except TypeError:
v = str(v)
result[k] = v
return result


def _read_template(search_paths, template_name="index.jinja2"):
env = Environment(
loader=FileSystemLoader(search_paths),
Expand Down

0 comments on commit bf19498

Please sign in to comment.