Repository: pytest-dev/pytest-reportlog (language: python)

Issue:
Support optionally dropping captured logs for passing tests
Analogous to the junit XML option for the same thing. Rationale being that captured logs can blow up the reportlog file size pretty significantly and are (I guess) only useful in the case where you have a transiently failing test and you want to compare logs for passes and fails.

Note, I have this fixed already locally and can PR if it's deemed worth including.


Discussion / hints:
This can be added as opt in, it should then note in the initial object that the log is lossy 
@nicoddemus wondering if we should elevate this to a general pytest level option 
@cj-timothy-sampson to understand better the use case, you want to still see logs for failing tests in the terminal, however you do not want the captured logs to go into the JSON file, correct?
yes. The logging to the terminal should be orthogonal to the logging in the reportlog. This would be analogous to the following option for the junit xml functionality: https://github.com/pytest-dev/pytest/blob/b8edacb8f1e79d6f5b37e46868d6b1b50c13ad3f/src/_pytest/junitxml.py#L412
Got it, thanks.

I think we can eventually elevate that to pytest, but for now having something similar to what we have for `junitxml` seems reasonable.

Feel free to open a PR @cj-timothy-sampson, glad to review it. :+1: 
https://github.com/pytest-dev/pytest-reportlog/pull/41

Fix patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index ebd4ca6..8b5a613 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -1,8 +1,9 @@
 Next (tbd)
 -------------------
 
-* `#16 <https://github.com/pytest-dev/pytest-reportlog/issues/15>`_: switch recommended file extension to `jsonl <https://jsonlines.org/>`__.
+* `#15 <https://github.com/pytest-dev/pytest-reportlog/issues/15>`_: switch recommended file extension to `jsonl <https://jsonlines.org/>`__.
 * `#17 <https://github.com/pytest-dev/pytest-reportlog/issues/17>`_: add compression support for ``.bz2``/``.gz``/``.xz`` log file extensions.
+* `#39 <https://github.com/pytest-dev/pytest-reportlog/issues/39>`_: new ``--report-log-exclude-logs-on-passed-tests`` option allows not capturing logs of passing tests.
 
 
 0.3.0 (2023-04-26)


Test patch:
diff --git a/src/pytest_reportlog/plugin.py b/src/pytest_reportlog/plugin.py
index ab9b9ec..8c2a672 100644
--- a/src/pytest_reportlog/plugin.py
+++ b/src/pytest_reportlog/plugin.py
@@ -15,6 +15,12 @@ def pytest_addoption(parser):
         default=None,
         help="Path to line-based json objects of test session events.",
     )
+    group.addoption(
+        "--report-log-exclude-logs-on-passed-tests",
+        action="store_true",
+        default=False,
+        help="Don't capture logs for passing tests",
+    )
 
 
 def pytest_configure(config):
@@ -83,6 +89,21 @@ def pytest_runtest_logreport(self, report):
         data = self._config.hook.pytest_report_to_serializable(
             config=self._config, report=report
         )
+        if (
+            self._config.option.report_log_exclude_logs_on_passed_tests
+            and data.get("outcome", "") == "passed"
+        ):
+            data["sections"] = [
+                s
+                for s in data["sections"]
+                if s[0]
+                not in [
+                    "Captured log setup",
+                    "Captured log call",
+                    "Captured log teardown",
+                ]
+            ]
+
         self._write_json_data(data)
 
     def pytest_warning_recorded(self, warning_message, when, nodeid, location):
diff --git a/tests/test_reportlog.py b/tests/test_reportlog.py
index 72d3813..bc81aa0 100644
--- a/tests/test_reportlog.py
+++ b/tests/test_reportlog.py
@@ -105,6 +105,43 @@ def test_warning():
         assert isinstance(rep, BaseReport)
 
 
+@pytest.mark.parametrize(
+    "exclude", [True, False], ids=["exclude on pass", "include logs on pass"]
+)
+def test_exclude_logs_for_passing_tests(testdir, tmp_path, exclude):
+    passing_log_entry = "THIS TEST PASSED!"
+    failing_log_entry = "THIS TEST FAILED!"
+    testdir.makepyfile(
+        f"""
+        import logging
+
+        logger = logging.getLogger(__name__)
+
+        def test_ok():
+            logger.warning("{passing_log_entry}")
+
+        def test_fail():
+            logger.warning("{failing_log_entry}")
+            assert 0
+        """
+    )
+    fn = tmp_path / "result.log"
+    if exclude:
+        result = testdir.runpytest(
+            f"--report-log={fn}", "--report-log-exclude-logs-on-passed-tests"
+        )
+    else:
+        result = testdir.runpytest(f"--report-log={fn}")
+    result.stdout.fnmatch_lines("*1 failed, 1 passed*")
+
+    log = fn.read_text("UTF-8")
+    if exclude:
+        assert passing_log_entry not in log
+    else:
+        assert passing_log_entry in log
+    assert failing_log_entry in log
+
+
 def test_xdist_integration(testdir, tmp_path):
     pytest.importorskip("xdist")
     testdir.makepyfile(