Skip to content

Commit a6eaa34

Browse files
Add option to avoid capturing logs for passed tests (#41)
Fix #39
1 parent 9e35bbd commit a6eaa34

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
Next (tbd)
22
-------------------
33

4-
* `#16 <https://github.com/pytest-dev/pytest-reportlog/issues/15>`_: switch recommended file extension to `jsonl <https://jsonlines.org/>`__.
4+
* `#15 <https://github.com/pytest-dev/pytest-reportlog/issues/15>`_: switch recommended file extension to `jsonl <https://jsonlines.org/>`__.
55
* `#17 <https://github.com/pytest-dev/pytest-reportlog/issues/17>`_: add compression support for ``.bz2``/``.gz``/``.xz`` log file extensions.
6+
* `#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.
67

78

89
0.3.0 (2023-04-26)

src/pytest_reportlog/plugin.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ def pytest_addoption(parser):
1515
default=None,
1616
help="Path to line-based json objects of test session events.",
1717
)
18+
group.addoption(
19+
"--report-log-exclude-logs-on-passed-tests",
20+
action="store_true",
21+
default=False,
22+
help="Don't capture logs for passing tests",
23+
)
1824

1925

2026
def pytest_configure(config):
@@ -83,6 +89,21 @@ def pytest_runtest_logreport(self, report):
8389
data = self._config.hook.pytest_report_to_serializable(
8490
config=self._config, report=report
8591
)
92+
if (
93+
self._config.option.report_log_exclude_logs_on_passed_tests
94+
and data.get("outcome", "") == "passed"
95+
):
96+
data["sections"] = [
97+
s
98+
for s in data["sections"]
99+
if s[0]
100+
not in [
101+
"Captured log setup",
102+
"Captured log call",
103+
"Captured log teardown",
104+
]
105+
]
106+
86107
self._write_json_data(data)
87108

88109
def pytest_warning_recorded(self, warning_message, when, nodeid, location):

tests/test_reportlog.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,43 @@ def test_warning():
105105
assert isinstance(rep, BaseReport)
106106

107107

108+
@pytest.mark.parametrize(
109+
"exclude", [True, False], ids=["exclude on pass", "include logs on pass"]
110+
)
111+
def test_exclude_logs_for_passing_tests(testdir, tmp_path, exclude):
112+
passing_log_entry = "THIS TEST PASSED!"
113+
failing_log_entry = "THIS TEST FAILED!"
114+
testdir.makepyfile(
115+
f"""
116+
import logging
117+
118+
logger = logging.getLogger(__name__)
119+
120+
def test_ok():
121+
logger.warning("{passing_log_entry}")
122+
123+
def test_fail():
124+
logger.warning("{failing_log_entry}")
125+
assert 0
126+
"""
127+
)
128+
fn = tmp_path / "result.log"
129+
if exclude:
130+
result = testdir.runpytest(
131+
f"--report-log={fn}", "--report-log-exclude-logs-on-passed-tests"
132+
)
133+
else:
134+
result = testdir.runpytest(f"--report-log={fn}")
135+
result.stdout.fnmatch_lines("*1 failed, 1 passed*")
136+
137+
log = fn.read_text("UTF-8")
138+
if exclude:
139+
assert passing_log_entry not in log
140+
else:
141+
assert passing_log_entry in log
142+
assert failing_log_entry in log
143+
144+
108145
def test_xdist_integration(testdir, tmp_path):
109146
pytest.importorskip("xdist")
110147
testdir.makepyfile(

0 commit comments

Comments
 (0)